SQL8 Chapter 3 Slides
SQL8 Chapter 3 Slides
Combining Tables
1
Section 3.1
Overview
2
Objectives
3
Combining Data from Multiple Tables
Table A Table B
4
Combining Data from Multiple Tables
Table A
Table B
5
Section 3.2
Joins
6
Objectives
7
Types of Joins
8
Types of Joins
Inner joins
return only matching rows
allow a maximum of 32 tables to be joined at
the same time.
9
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.
11
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
12
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
1 a 2 x
13
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
1 a 2 x
1 a 3 y
14
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
15
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
16
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
1 a 2 x
1 a 3 y
1 a 5 v
4 d 2 x
4 d 3 y
17
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
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
18
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
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
19
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
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 20
...
Cartesian Product
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
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 21
...
Cartesian Product
3X3= 9
23
Inner Joins
24
...
Inner Joins
25
...
Inner Joins
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
select *
1 a 2 x
1 a 3 y from one, two ...
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
26
...
Inner Joins
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
select *
1 a 2 x
1 a 3 y from one, two
1 a 5 v where one.x=two.x;
4 d 2 x
4 d 3 y
4 d 5 v
2 b 2 x
2 b 3 y
2 b 5 v
27
Inner Joins
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
X A X B
select *
2 b 2 x from one, two
where one.x=two.x;
28
Inner Joins
Display the X column only once.
Table ONE Table TWO
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
select one.x, a, b
from one, two
where one.x=two.x;
X A B
2 b x 29
...
Inner Joins
Display all combinations of rows with matching
keys, including duplicates.
Table THREE Table FOUR
X A X B
1 a1 2 x1
1 a2 2 x2
2 b1 3 y
2 b2 5 v
4 d
select *
from three, four
where three.x=four.x;
30
...
Inner Joins
Display all combinations of rows with matching
keys, including duplicates.
Table THREE Table FOUR
X A X B
1 a1 2 x1
1 a2 2 x2
2 b1 3 y
2 b2 5 v
4 d
select *
from three, four
where three.x=four.x;
31
Inner Joins
Display all combinations of rows with matching
keys, including duplicates.
Table THREE Table FOUR
X A X B
1 a1 2 x1
1 a2 2 x2
2 b1 3 y
2 b2 5 v
4 d X A X B
select * 2 b1 2 x1
2 b2 2 x1
from three, four 2 b1 2 x2
where three.x=four.x; 2 b2 2 x2
32
Inner Joins
33
Inner Joins
Partial Output
New York Employees
Job
Name Code Age
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
R. LONG BCK 30
L. GORDON BCK 42
J. PEARSON BCK 42
N. JONES BCK 35
T. BURNETTE BCK 34
R. VANDEUSEN BCK 41
J. MARKS BCK 35
D. WOOD FA1 30
35
Outer Joins
36
Outer Joins
37
Outer Joins
A left join retrieves matching rows from both tables,
plus nonmatching rows from the left table (the first
table in the FROM clause).
Table ONE Table TWO
X A X B
1 a 2 x
2 b 3 y
4 d 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 . 38
Outer Joins
A right join retrieves matching rows from both tables,
plus nonmatching rows from the right table (the
second table in the FROM clause).
Table ONE Table TWO
X A X B
1 a 2 x
2 b 3 y
4 d 5 v
select *
from one right join two
on one.x=two.x;
X A X B
2 b 2 x
. 3 y
. 5 v 39
Outer Joins
A full join retrieves matching rows and
nonmatching rows from both tables.
select * X A X B
from one full join two 1 a .
on one.x=two.x; 2 b 2 x
. 3 y
4 d .
. 5 v
40
Outer Joins
41
Outer Joins
title 'All March Flights';
select marchflights.date,
marchflights.flightnumber
label='Flight Number',
marchflights.destination
label='Left',
flightdelays.destination
label='Right',
delay
from airline.marchflights left join
airline.flightdelays
on marchflights.date=flightdelays.date
and marchflights.flightnumber=
flightdelays.flightnumber
order by delay; 42
Outer Joins
Partial Output
All March Flights
Flight DelayIn
Date Number Left Right Minutes
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
16MAR2000 622 FRA .
03MAR2000 416 WAS .
17MAR2000 182 YYZ .
14MAR2000 271 CDG .
11MAR2000 290 WAS .
08MAR2000 182 YYZ .
. 132 YYZ .
11MAR2000 202 ORD .
29MAR2000 829 WAS .
25MAR2000 872 LAX .
22MAR2000 183 WAS .
27MAR2000 982 DFW .
25MAR2000 829 WAS WAS -10
18MAR2000 219 LHR LHR -10
09MAR2000 821 LHR LHR -10 43
Using a Table Alias
44
Using a Table Alias
select l.date,
l.flightnumber
label='Flight Number',
l.destination label='Left',
r.destination label='Right',
delay
from airline.marchflights as l
left join
airline.flightdelays as r
on l.date=r.date and
l.flightnumber=r.flightnumber
order by delay;
45
SQL Join versus DATA Step Merge
A DATA step with MERGE and BY statements
combines rows differently from an outer join.
Table ONE Table TWO
X A X B
1 a 2 x
2 b 3 y
4 d 5 v Table MERGED
X A B
data merged; 1 a
2 b x
merge one two; 3 y
by x; 4 d
run; 5 v
46
SQL Join versus DATA Step Merge
A DATA step with MERGE and BY statements
combines rows differently from an outer join.
Table ONE Table TWO
X A X B
1 a 2 x
2 b 3 y
4 d 5 v
X A B
proc sql; 1 a
2 b x
select one.x, a, b y
from one full join two 4 d
on one.x=two.x; v
47
SQL Join versus DATA Step Merge
You can use the COALESCE function to overlay
two columns.
Table ONE Table TWO
X A X B
1 a 2 x
2 b 3 y
4 d 5 v
X A B
select coalesce(one.x,two.x) 1 a
2 b x
label='x', a, b 3 y
from one full join two 4 d
on one.x=two.x; 5 v
48
SQL Join versus DATA Step Merge
49
Internal Processing of Joins
50
Section 3.3
Complex Joins
51
Objectives
52
In-Line Views
An inline view is
a temporary table that exists only during
query execution
created when a FROM clause contains a query
expression in place of a table name.
53
In-Line Views
Output
Number Number
Average Maximum of of Early Probability
Destination Delay Delay Delays Arrivals of Delay
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
WAS 1 15 76 75 0.50
YYZ 2 14 36 24 0.60
DFW 3 20 38 23 0.62
ORD 3 19 51 41 0.55
LAX 5 27 82 41 0.67
LHR 6 30 39 19 0.67
CPH 6 26 16 11 0.59
FRA 6 34 14 12 0.54
CDG 9 39 21 5 0.81
55
In-Line Views
select destination,
summarized columns,
late / (late + early) as prob
format=5.2
label='Probability of Delay'
from summarized table
order by 2nd column;
56
In-Line Views
Output
Number Number
Average Maximum of of Early Probability
Destination Delay Delay Delays Arrivals of Delay
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
WAS 1 15 76 75 0.50
YYZ 2 14 36 24 0.60
DFW 3 20 38 23 0.62
ORD 3 19 51 41 0.55
LAX 5 27 82 41 0.67
LHR 6 30 39 19 0.67
CPH 6 26 16 11 0.59
FRA 6 34 14 12 0.54
CDG 9 39 21 5 0.81
59
Handling a Complex Query
60
...
select EmpID
from airline.flightschedule
where Date='04mar2000'd
and Destination='CPH';
62
Handling a Complex Query
Step 1 Output
Emp
ID
ƒƒƒƒ
1556
1830
1124
1135
1437
1839
63
Handling a Complex Query
Step 2 Output
JobCategory State
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
FA CT
FA NY
NA NY
PT NY
PT CT
FA NY
65
Handling a Complex Query
66
Handling a Complex Query
select EmpId
from airline.supervisors as m,
(select substr(JobCode,1,2) as JobCategory,
State
from airline.staffmaster as s,
airline.payrollmaster as p
where s.empid=p.empid and s.empid in
(select EmpID
from airline.flightschedule
where Date='04mar2000'd and
Destination='CPH')) as c
where m.jobcategory=c.jobcategory
and m.state=c.state; 67
Handling a Complex Query
Step 3 Output
Supervisor
Id
ƒƒƒƒƒƒƒƒƒƒ
1431
1983
1352
1118
1106
1983
68
Handling a Complex Query
69
Handling a Complex Query
Step 4 Output
FirstName LastName
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
SHARON DEAN
ROGER DENNIS
JASPER MARSHBURN
SIMON RIVERS
DEBORAH YOUNG
71
Handling a Complex Query
You can also solve this problem by using a
multiway join.
select distinct e.firstname, e.lastname
from airline.flightschedule as a,
airline.staffmaster as b,
airline.payrollmaster as c,
airline.supervisors as d,
airline.staffmaster as e
where a.date='04mar2000'd and
a.destination='CPH' and
a.empid=b.empid and
a.empid=c.empid and
d.jobcategory=substr(c.jobcode,1,2)
and d.state=b.state and 72
d.empid=e.empid;
Choosing Between SQL Joins and
DATA Step Merges
• DATA step merges are usually more efficient
than SQL joins in combining small tables.
SQL joins are usually more efficient than
DATA step merges in combining large,
unsorted tables.
SQL joins are usually more efficient than
DATA step merges in combining a large,
indexed table with a small table.
73
Choosing Between SQL Joins and
DATA Step Merges
• For ad hoc queries, select the method that
you can code in the shortest time.
• For production jobs, experiment with different
coding techniques and evaluate performance
statistics.
74
Exercises
75
Section 3.5
Set Operators
76
Objectives
77
Types of Set Operators
78
...
INTERSECT
UNION
All columns from
both tables are selected.
OUTER UNION
79
Types of Set Operators
EXCEPT
Unique rows from the first
table that are not found
in the second table are
selected.
80
Types of Set Operators
INTERSECT
Common unique rows
from both tables are
selected.
81
Types of Set Operators
UNION
All unique rows from both
tables are selected with
columns overlaid.
82
Types of Set Operators
OUTER UNION
All rows from both
tables, unique as well
as non-unique, are
selected.
Columns are not
overlaid.
83
Modifiers
84
Modifiers
ALL
does not remove duplicate rows, and so
avoids an extra pass through the data. Use
the ALL keyword for better performance when
it is possible.
is not allowed in connection with an OUTER
UNION operator. (It is implicit.)
85
Modifiers
CORRESPONDING
overlays columns by name, instead of by
position
removes any columns not found in both
tables when used in EXCEPT, INTERSECT, and
UNION operations
causes common columns to be overlaid when
used in OUTER UNION operations
can be abbreviated as CORR.
86
EXCEPT
87
The EXCEPT Operator
Display the unique rows in table ONE that are
not found in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except
select *
from two; 88
The EXCEPT Operator
Display the unique rows in table ONE that are
not found in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except
select *
from two; 89
The EXCEPT Operator
Display the unique rows in table ONE that are
not found in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except
select *
from two; 90
The EXCEPT Operator
Display the unique rows in table ONE that are
not found in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g X A
select *
from one 1 a
except 1 b
2 c
select * 4 e
from two; 6 g 91
The EXCEPT Operator
Display the rows (duplicates included) that are
found in table ONE but not in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g select *
from one
except all
select *
from two;
92
The EXCEPT Operator
Display the rows (duplicates included) that are
found in table ONE but not in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g select *
from one
except all
select *
from two;
93
The EXCEPT Operator
Display the rows (duplicates included) that are
found in table ONE but not in table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e X A
6 g select *
1 a
from one 1 a
except all 1 b
select * 2 c
from two; 4 e
6 g 94
The EXCEPT Operator
Display the unique rows that exist in table ONE and
not in table TWO, based on same-named columns.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except corr
select *
from two;
95
The EXCEPT Operator
Display the unique rows that exist in table ONE and
not in table TWO, based on same-named columns.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except corr
select *
from two;
96
The EXCEPT Operator
Display the unique rows that exist in table ONE and
not in table TWO, based on same-named columns.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except corr
select *
from two;
97
The EXCEPT Operator
Display the unique rows that exist in table ONE and
not in table TWO, based on same-named columns.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
except corr
select *
from two;
98
The EXCEPT Operator
Display the unique rows that exist in table ONE and
not in table TWO, based on same-named columns.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
X
from one
except corr 4
select * 6
from two;
99
The EXCEPT Operator
AIRLINE.STAFFCHANGES and
AIRLINE.PAYROLLCHANGES contain information
about
• current employees who have salary or job
code changes
• new employees.
The new tables have the same layout as the
AIRLINE.STAFFMASTER and
AIRLINE.PAYROLLMASTER tables.
100
The EXCEPT Operator
select FirstName,
LastName
from airline.staffchanges
except all
select FirstName,
LastName
from airline.staffmaster;
101
The EXCEPT Operator
FirstName LastName
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
AMY BRIDESTON
JIM POWELL
102
The EXCEPT Operator
103
The EXCEPT Operator
No. of
Persons
ƒƒƒƒƒƒƒƒ
144
104
INTERSECT
105
The INTERSECT Operator
Display the unique rows common to table ONE
and table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
intersect
select *
from two; 106
The INTERSECT Operator
Display the unique rows common to table ONE
and table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
intersect
select *
from two; 107
The INTERSECT Operator
Display the unique rows common to table ONE
and table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
intersect
select *
from two; 108
The INTERSECT Operator
Display the unique rows common to table ONE
and table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select * X A
from one 3 v
intersect
select *
from two; 109
The INTERSECT Operator
Display the unique rows common to table ONE
and table TWO, based on same-named columns.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g
select *
from one
intersect corr
select *
from two;
110
The INTERSECT Operator
Display the unique rows common to table ONE
and table TWO, based on same-named columns.
select FirstName,
LastName
from airline.staffmaster
intersect all
select FirstName,
LastName
from airline.staffchanges;
115
The INTERSECT Operator
FirstName LastName
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
DIANE WALTERS
KAREN CARTER
NEIL CHAPMAN
RAYMOND SANDERS
116
UNION
117
The UNION Operator
Display the unique rows that table ONE and table
TWO have all together.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g select *
from one
union
select *
from two;
118
The UNION Operator
The SQL processor creates an intermediate result
by concatenating and sorting ONE and TWO.
Table ONE Table TWO
X A X B
1 a
1 a 1 x 1 a
1 a 2 y 1 b
1 b 3 z 1 x
2 c 3 v 2 c
3 v 5 w 2 y
4 e 3 v
6 g select * 3 v
from one 3 z
union 4 e
select * 5 w
from two; 6 g
119
The UNION Operator
The SQL processor removes duplicate rows from
the intermediate result.
Table ONE Table TWO
X A X B
1 a
1 a 1 x 1 a
1 a 2 y 1 b
1 b 3 z 1 x
2 c 3 v 2 c
3 v 5 w 2 y
4 e 3 v
6 g select * 3 v
from one 3 z
union 4 e
select * 5 w
from two; 6 g
120
The UNION Operator
Final result set.
X A
Table ONE Table TWO
X A X B 1 a
1 b
1 a 1 x 1 x
1 a 2 y 2 c
1 b 3 z 2 y
2 c 3 v 3 v
3 v 5 w
3 z
4 e
6 g 4 e
select *
5 w
from one 6 g
union
select *
from two;
121
The UNION Operator
Display all of the unique rows of same-named
columns in table ONE and table TWO.
Table ONE Table TWO
X A X B
1 a 1 x
1 a 2 y
1 b 3 z
2 c 3 v
3 v 5 w
4 e
6 g select *
from one
union corr
select *
from two;
122
The UNION Operator
The SQL processor creates an intermediate result by
concatenating and sorting data from the 1st column.
X
Table ONE Table TWO 1
X A X B 2
3
1 a 1 x 4
1 a 2 y 5
1 b 3 z 6
2 c 3 v
3 v 5 w
4 e
6 g select *
from one
union corr
select *
from two;
124
The UNION Operator
Final result.
126
The UNION Operator
title 'Points and Miles Traveled '
'by Frequent Flyers';
select 'Total Points Earned :',
sum(PointsEarned) format=comma12.
from airline.frequentflyers
union
select 'Total Points Used :',
sum(PointsUsed) format=comma12.
from airline.frequentflyers
union
select 'Total Miles Traveled:',
sum(MilesTraveled) format=comma12.
from airline.frequentflyers; 127
The UNION Operator
128
OUTER UNION
129
The OUTER UNION Operator
Display all data values from table ONE and table TWO.
Table ONE Table TWO
X A X B
X A X B
1 a 1 x
1 a 2 y 1 a .
1 b 3 z 1 a .
2 c 3 v 1 b .
3 v 5 w 2 c .
3 v .
4 e 4 e .
6 g select * 6 g .
from one . 1 x
outer union . 2 y
select * . 3 z
. 3 v
from two; . 5 w
130
The OUTER UNION Operator
Display all data values from table ONE and table
TWO, but overlay common columns.
Table ONE Table TWO
X A X B
1 a 1 x X A B
1 a 2 y 1 a
1 b 3 z 1 a
2 c 3 v 1 b
3 v 5 w 2 c
4 e 3 v
6 g select * 4 e
6 g
from one 1 x
outer union corr 2 y
select * 3 z
from two; 3 v
5 w
131
The OUTER UNION Operator
132
The OUTER UNION Operator
Partial Output
Employee Job
Number Code Salary
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1400 ME1 $41,677
1403 ME1 $39,301
1120 ME1 $40,067
1121 ME1 $40,757
1412 ME1 $38,919
1200 ME1 $38,942
1995 ME1 $40,334
1418 ME1 $39,207
1653 ME2 $49,151
1782 ME2 $49,483 133
SQL versus Traditional
SAS Programming
The following programs produce the same report:
data three;
set one two;
run;
proc print data=three noobs;
run;
proc sql;
select * from one
outer union corr
select * from two;
quit;
proc append base=one data=two;
run;
proc print data=one noobs;
run; 134
Comparing Methods of Combining
Tables Vertically
PROC APPEND is the fastest method of
performing a simple concatenation of two
tables. The BASE= table is not completely
read; only the DATA= table is completely read.
When logical conditions are involved, you can
choose either the DATA step or PROC SQL.
continued...
135
Comparing Methods of Combining
Tables Vertically
SQL set operators generally require more
computer resources, but are more convenient
and flexible, than the DATA step equivalents.
With the DATA step, you can process an
unlimited number of tables at one time.
With SQL set operators, you can work on only
two tables at a time.
continued...
136
Comparing Methods of Combining
Tables Vertically
If multiple DATA steps are required to
perform the task, consider using PROC SQL.
If you are unsure which method is best,
benchmark using the techniques discussed
in Chapter 5.
137
Exercises
138