0% found this document useful (0 votes)
71 views

SQL8 Chapter 3 Slides

The document discusses different types of joins in PROC SQL, including inner joins which return matching rows between tables based on a join condition, and outer joins which return all rows including non-matching rows. It provides examples of inner joins between multiple tables to combine data where the join keys are equal. The document also discusses Cartesian products which return all possible combinations of rows between tables without a join condition.

Uploaded by

kpallu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

SQL8 Chapter 3 Slides

The document discusses different types of joins in PROC SQL, including inner joins which return matching rows between tables based on a join condition, and outer joins which return all rows including non-matching rows. It provides examples of inner joins between multiple tables to combine data where the join keys are equal. The document also discusses Cartesian products which return all possible combinations of rows between tables without a join condition.

Uploaded by

kpallu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 137

Chapter 3

Combining Tables

1
Section 3.1

Overview

2
Objectives

• Distinguish between joins and set operations.

3
Combining Data from Multiple Tables

Joins combine tables horizontally (side by side).

Table A Table B

4
Combining Data from Multiple Tables

Set operations combine tables vertically (one on


top of the other).

Table A
Table B

5
Section 3.2

Joins

6
Objectives

• Describe the different joins available


in PROC SQL.
• Use a table alias.
• Compare SQL joins to DATA step merges.

7
Types of Joins

PROC SQL supports two types of joins:


• inner joins
• outer 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.

Left Full Right


10
Cartesian Product

A query that lists multiple tables in the FROM


clause, without row restrictions, results in all
possible combinations of rows from all tables.
This is called a Cartesian product.
select *
from one, two;

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

The number of rows in a Cartesian product is


the product of the number of rows in the
contributing tables.

3X3= 9

1,000 x 1,000 = 1,000,000

23
Inner Joins

Inner join syntax resembles Cartesian product


syntax, but it has a WHERE clause that restricts
how the rows can be combined.
SELECT col1, col2, …
FROM table1, table2, …
WHERE join-condition(s)
<AND other subsetting conditions>
<other clauses>;

24
...

Inner Joins

Conceptually, PROC SQL


• first builds a Cartesian product
• then applies the specified restriction(s) and
removes rows.

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

Example: Display the names, job codes,


and ages of all New York employees.
• Employee names are found in the
AIRLINE.STAFFMASTER table.
• Employee job codes and birth dates are found
in the AIRLINE.PAYROLLMASTER table.

33
Inner Joins

title 'New York Employees';


select substr(FirstName,1,1)||'. ' ||
LastName as Name,
JobCode,
int((today()-DateOfBirth)/365.25)
as Age
from airline.payrollmaster,
airline.staffmaster
where payrollmaster.empID=
staffmaster.empID
and State='NY'
order by JobCode;
34
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

You can retrieve nonmatching rows, as well as


matching rows, by using an outer join. Outer
joins are limited to two tables at a time.

Left Full Right

36
Outer Joins

General form of an outer join:

SELECT col1, col2, …


FROM table1
LEFT|RIGHT|FULL JOIN
table2
ON join-condition(s)
<other clauses>;

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.

Table ONE Table TWO


X A X B
1 a 2 x
2 b 3 y
4 d 5 v

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

Example: List all flights during March with


corresponding delay information
(if it exists).
 AIRLINE.FLIGHTDELAYS does not contain
delay information for all of the March flights.

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

An alias is a table nickname. You can assign an


alias to a table by following the table name in
the FROM clause with the AS keyword and a
nickname for the table. Then use the alias in
other clauses of the QUERY statement.

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

Joins do not require


• sorted or indexed tables
• same-named columns in join expressions
• equality in join expressions.

49
Internal Processing of Joins

Conceptually, during a join,


 a Cartesian product is built internally
 WHERE processing selects the appropriate
rows.
In reality, however, the PROC SQL optimizer
breaks the Cartesian product into smaller pieces.

50
Section 3.3

Complex Joins

51
Objectives

• Understand techniques that simplify the


coding of a complex query.
• Compare solving a problem using PROC SQL
with traditional SAS programming.

52
In-Line Views

An in­line 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

Example: Which destinations experience the


worst delays?

How do you define "worst delays"?


54
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

Boolean expressions can be used in the SELECT


clause.
select Delay,
(Delay > 0) as Late
from airline.flightdelays;
Delay Late
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
0 0
8 1
-5 0
18 1
57
In-Line Views
select *, Late/(Late+Early) as prob
format=5.2 label='Probability of Delay'
from (select Destination,
avg(Delay) as average
format=3.0 label='Average Delay',
max(Delay) as max
format=3.0 label='Maximum Delay',
sum(Delay > 0) as late
format=3.0 label='Number of
Delays',
sum(Delay <= 0) as early
format=3.0
label='Number of Early Arrivals'
from airline.flightdelays
group by 1) 58
order by 2;
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

What are the names of the supervisors


for the crew on the flight to Copenhagen
on March 4, 2000?

60
...

Handling a Complex Query

Step 1: Identify the crew for the flight.


Step 2: Find the states and job categories of the
crew returned from the first query.
Step 3: Find the employee numbers of the crew
supervisors based on the states and job
categories generated by the second
query.
Step 4: Find the names of the supervisors based
on the employee numbers returned from
the third query.
61
Handling a Complex Query

Step 1: Identify the crew for the flight.

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: Find the states and job categories of the


crew returned from the first query.

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');
64
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

Step 3: Find the employee numbers of the crew


supervisors based on the states and job
categories generated by the second
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

Step 4: Find the names of the supervisors.

69
Handling a Complex Query

select FirstName, LastName


from airline.staffmaster where empid in
(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); 70
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

These exercises reinforce the concepts


discussed previously.

75
Section 3.5

Set Operators

76
Objectives

• Use the SQL set operators.


• Compare the SQL set operators to traditional
SAS programming tools.

77
Types of Set Operators

Set operators combine rows from two tables


vertically.
There are four set operators:
• EXCEPT
• INTERSECT
• UNION
• OUTER UNION.

78
...

Default Behavior of Set Operators

• Columns are matched


by position and must be
the same data type.
• Column names in the result
set are determined
EXCEPT by the first table.

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

You can use two keywords to modify the


behavior of set operators:
• ALL
• CORRESPONDING.

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

 Unique rows from the first


table that are not found
in the second table are
selected.

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

Example: Display the names of new employees.

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

Example: How many employees have no changes


in salary or job code?
select count(*) label='No. of Persons'
from (select EmpID
from airline.staffmaster
except all
select EmpID
from airline.staffchanges);

103
The EXCEPT Operator

No. of
Persons
ƒƒƒƒƒƒƒƒ
144

104
INTERSECT

 Common unique rows


from both tables are
selected.

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.

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;
111
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;
112
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;
113
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 * X
from one 1
intersect corr
2
select *
from two; 3
114
The INTERSECT Operator

Example: What are the names of the old


employees who have changed salary
or job code?

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

 All unique rows from both


tables are selected with
columns overlaid.

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.

Table ONE Table TWO X


X A X B 1
1
1 a 1 x 1
1 a 2 y 1
1 b 3 z 2
2 c 3 v 2
3 v 5 w 3
4 e 3
6 g select * 3
from one 4
union corr 5
select * 6
from two;
123
The UNION Operator
The SQL processor removes duplicate rows from
the intermediate result.

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.

Table ONE Table TWO


X A X B X
1 a 1 x 1
1 a 2 y 2
1 b 3 z 3
2 c 3 v 4
3 v 5 w 6
5
4 e
6 g select *
from one
union corr
select *
from two;
125
The UNION Operator

Example: Add the miles traveled, bonus points


earned, and bonus points used by
frequent flyers.

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

Points and Miles Traveled by Frequent Flyers


ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Total Points Earned : 11,083,463
Total Points Used : 4,429,670
Total Miles Traveled: 10,477,963

128
OUTER UNION

 All rows from both


tables, unique as well
as non-unique, are
selected.
 Columns are not
overlaid.

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

Example: Display the employee numbers, job


codes, and salaries of all mechanics.
select *
from airline.mechanicslevel1
outer union corr
select *
from airline.mechanicslevel2
outer union corr
select *
from airline.mechanicslevel3;

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

These exercises reinforce the concepts


discussed previously.

138

You might also like