SQLJoins
SQLJoins
1
Types of Joins
PROC SQL supports two types of joins:
inner joins
outer joins
2
Types of Joins
Inner joins
return only matching rows
3
Types of Joins
Outer joins
return all matching rows, plus nonmatching rows
from one or both tables
can be performed on only two tables or views
at a time.
4
Cartesian Product
To understand how SQL processes a join, it is important
to understand the concept of the Cartesian product.
A query that lists multiple tables in the FROM clause
without a WHERE clause produces all possible
combinations of rows from all tables. This result is called
the Cartesian product.
select *
from one, two;
s105d01
5
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
s105d01
6 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
s105d01
7 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
s105d01
8 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
s105d01
9 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
s105d01
10 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
s105d01
11 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
s105d01
12 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
s105d01
13 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
s105d01
14 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
s105d01
15 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
17 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
18 ...
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 rows X 3 rows 3 y
2 b 5 v
Result Set
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y 9 rows
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
19
Inner Joins
Conceptually, when processing an inner join, PROC SQL
does the following:
1. builds the Cartesian product of all the tables listed
2. applies the WHERE clause to limit the rows returned
20
Inner Joins: Cartesian Product Built
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b select * 5 v
from one, two
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v s105d02
21 ...
Inner Joins: WHERE Clause Restricts Rows
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
select *
2 b from one, two 5 v
where one.x=two.x;
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v s105d02
22 ...
Inner Joins: Results Are Returned
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
select *
2 b from one, two 5 v
where one.x=two.x;
X A X B
2 b 2 x
s105d02
23
Outer Joins
Inner joins returned only matching rows. When you join
tables, you might want to include nonmatching rows as
well as matching rows.
24
Outer Joins
You can retrieve both nonmatching and matching rows
using an outer join.
Outer joins include left, full, and right outer joins. Outer
joins can process only two tables at a time.
25
Left Join
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
select *
from one left join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
4 d .
s105d07
26
Right Join
Table Two Table One
X B X A
2 x 1 a
3 y 4 d
5 v 2 b
select *
from two right join one
on one.x = two.x;
X B X A
. 1 a
2 x 2 b
. 4 d
s105d08
27
Full Join
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
select *
from one full join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
. 3 y
4 d .
. 5 v
s105d09
28