Sunday, September 7, 2014

Cartesian Join

The CROSS JOIN returns every row from one table crossed with every row from the second.

It is also called Cartesian Product, cross product, cross join or no join.

SELECT t.toy, b.boy
FROM toys AS t
CROSS JOIN
boys AS b;

or

SELECT toys.toy, boys.boy FROM toys, boys




Inner Join

An inner join is a cross join with some result rows removed by a condition in the query.
An inner join combines the records from two tables using comparison operators in a condition.

SELECT <some columns>
FROM table1
INNER JOIN
table2
ON <some conditions>

SELECT mc.last_name, mc.first_name, p.profession
FROM my_contacts AS mc
INNER JOIN
profession AS p
ON mc.contact_id = p.prof_id;

Equijoin

Inner joins test for equality

SELECT boys.boy, toys.toy
FROM boys
INNER JOIN
toys
ON boys.toy_id = toys.toy_id;

Non-Equijoin

Inner joins test for inequality.

SELECT boys.boy, toys.toy
FROM boys
INNER JOIN
toys
ON boys.toy_id <> toys.toy_id
ORDER BY boys.boy;

Natural Join

Natural join only work if the column you are joining by has the same name in both tables.

Natural join will recognize the same column name in each table and return matching rows.

SELECT boys.boy, toys.toy
FROM boys
NATURAL JOIN
toys;

No comments:

Post a Comment