Types of SQL Joins


adapted from http://www.postgresql.org/docs/current/interactive/queries-table-expressions.html

Assume we are working with two tables:

Table t1
idname
1a
2b
3c

Table t2
idvalue
1xxx
3yyy
5zzz

Cross Join
A cross join will combine each row from t1 with each row from t2:

 SELECT * FROM t1 CROSS JOIN t2 

idnameidvalue
1a1xxx
1a3yyy
1a5zzz
2b1xxx
2b3yyy
2b5zzz
3c1xxx
3c3yyy
3c5zzz

Notice there are 9 rows: (3 rows from t1) x (3 rows from t2) = 9 rows total. You will rarely, if ever, need to use a cross join at PVS.

Inner Join
By default, all joins are inner joins unless you specify left, right, or full (see below). The word inner is optional, and is usually omitted.

An inner join satisfies the following: For each row r1 in t1, the resulting table has a row for each row in t2 that satisfies the join condition.

 SELECT * FROM t1 JOIN t2 ON t1.id = t2.id 

idnameidvalue
1a1xxx
3c3yyy

Left Join
Notice that in the previous example, rows that weren't matched were omitted from the results. There are times when you want all of the rows in the first table to show up in the results. A left join does an inner join first, then, for every row in t1 that isn't in the results, that row is added (with null values if necessary). It's called a left join because t1 show up on the left side of the LEFT JOIN statement.

 SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id 

idnameidvalue
1a1xxx
2b
3c3yyy

Right Join
Likewise, a right join does an inner join first, then for every row in *t2* that isn't in the results, that row is added (with null values if necessary). Notice that t2 is on the right side of the RIGHT JOIN statement.

 SELECT * FROM t1 RIGHT JOIN t2 ON t1.id = t2.id 

idnameidvalue
1a1xxx
3c3yyy
5zzz

Full Join
With a full join, an inner join is performed first, then for every row in t1 that isn't in the results, that row is added (with null values if necessary). For every row in t2 that isn't in the results, that row is added (with null values if necessary).

 SELECT * FROM t1 FULL JOIN t2 ON t1.id = t2.id 

idnameidvalue
1a1xxx
2b
3c3yyy
5zzz

ON vs. WHERE
Sometimes you might be tempted to combine conditions. For instance, let's say you wanted to do a left join between t1 and t2, and filter the results so that only records containing "xxx" were returned (for instance, all candidates from a certain state). Your first try might be something like this:

 SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id AND t2.value = 'xxx' 

idnameidvalue
1a1xxx
2b
3c

Probably not what you expected! The reason is that the LEFT JOIN will always contain rows in t1 that aren't in the results. So the first row matches your conditional, then the two last rows are added because they do not match the conditional.

What you really want to do here is use a WHERE clause, as this is evaluated after the join:

 SELECT * FROM t1 LEFT JOIN t2 ON t1.id = t2.id WHERE t2.value = 'xxx' 

idnameidvalue
1a1xxx


CategoryTechnicalSkills
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki