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

sqljoins selected

Uploaded by

hod cse
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)
31 views

sqljoins selected

Uploaded by

hod cse
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/ 115

SQL Joins

1
Objectives
 Horizontally combine data from multiple tables.
 Distinguish between inner and outer SQL joins.
 Compare SQL joins to DATA step merges.

2
Combining Data from Multiple Tables
SQL uses set operators to combine tables vertically.

Table
Table AA
Table
Table BB
This produces results that can be compared to a
DATA step concatenation.

3
Combining Data from Multiple Tables
SQL uses joins to combine tables horizontally.

Table
Table AA Table
Table BB

This produces results that can be compared to a


DATA step merge.

4
Types of Joins
PROC SQL supports two types of joins:
 inner joins

 outer joins

5
Types of Joins
Inner joins
 return only matching rows

 enable a maximum of 256 tables to be joined

at the same time.

6
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

7
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
8
Cartesian Product
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 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

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

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

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

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

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

s105d01
15 ...
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
16 ...
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
17 ...
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
18 ...
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

20 ...
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

21 ...
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

22
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
100,000 x 100,000 = 10,000,000,000

A Cartesian product is rarely the desired result of a query.

23
24
5.02 Quiz
How many rows are returned from this query?

select *
from three, four;

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

25
s105a01
5.02 Quiz – Correct Answer
How many rows are returned from this query?
The query produces 20 rows.
select *
from three, four;

Table Three Table Four Partial Results Set


X A X B X A X B
1 a1 2 x1 1 a1 2 x1
1 a2 2 x2 1 a1 2 x2
2 b1 3 y 1 a1 3 y
2 b2 5 v 1 a1 5 v
4 d 1 a2 2 x1
1 a2 2 x2
5*4=20 1 a2 3 y
1 a2 5 v
2 b1 2 x1
26
2 b1 2 x2
s105a01
Inner Joins
Inner join syntax resembles Cartesian product syntax,
but a WHERE clause restricts which rows are returned.
General form of an inner join:

SELECT
SELECTcolumn-1<,
column-1<, …column-n>
…column-n>
FROM
FROM table-1|view-1<,
table-1|view-1<, …… table-n|view-n>
table-n|view-n>
WHERE
WHEREjoin-condition(s)
join-condition(s)
<AND
<ANDother
othersubsetting
subsettingconditions>
conditions>
<other
<otherclauses>;
clauses>;

27 ...
Inner Joins
Inner join syntax resembles Cartesian product syntax,
but a WHERE clause restricts which rows are returned.
General form of an inner join:

SELECT
SELECTcolumn-1<,
column-1<, …column-n>
…column-n>
FROM
FROM table-1|view-1<,
table-1|view-1<, …… table-n|view-n>
table-n|view-n>
WHERE
WHEREjoin-condition(s)
join-condition(s)
<AND
<ANDother
othersubsetting
subsettingconditions>
conditions>
<other
<otherclauses>;
clauses>;

Significant syntax changes from earlier queries:


 The FROM clause references multiple tables.

 The WHERE clause includes join conditions in addition

to other subsetting specifications.


28
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

29
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
30 ...
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
31 ...
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

 Tables do not have to be sorted before they are


joined.

s105d02
32
Inner Joins
One method of displaying the X column only once is to
use a table qualifier in the SELECT list.
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

s105d03
33
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

proc sql;
select *
from three, four
where three.x=four.x;
quit;
s105d04
34 ...
Inner Joins
Display all combinations of rows with matching keys,
including duplicates.
Table Three Table Four Results Set
X A X B X A X B
1 a1 2 x1 2 b1 2 x1
1 a2 2 x2 2 b1 2 x2
2 b1 3 y 2 b2 2 x1
2 b2 5 v 2 b2 2 x2
4 d

proc sql;
select *
from three, four
where three.x=four.x;
quit;
s105d04
35
36
Setup for the Poll
Run program s105a02 and review the results to determine
how many rows (observations) the DATA step MERGE
statement produces in the output table.

Three Four
X A X B
1 a1 2 x1
1 a2 2 x2
2 b1 3 y
2 b2 5 v
4 d

s105a02
37
5.03 Multiple Choice Poll
How many rows (observations) result from the DATA step
MERGE statement in program s105a02?
a. 4

b. 2

c. 6

d. 20

e. None of the above

38
5.03 Multiple Choice Poll – Correct Answer
How many rows (observations) result from the DATA step
MERGE statement in program s105a02?
a. 4

b. 2

c. 6

d. 20

e. None of the above

Three Four New


X A X B X A B
1 a1 2 x1 2 b1 x1
1 a2 2 x2 2 b2 x2
2 b1 3 y
2 b2 5 v
4 d
39
Business Scenario
Display the name, city, and birth month of all Australian
employees. Here is a sketch of the desired report:
Australian Employees’ Birth Months

Birth
Name City
Month
Last, First City Name
1

40
Business Scenario
Considerations:
 orion.Employee_Addresses contains
employee name, country, and city data.
 orion.Payroll contains employee birth dates.
 Both orion.Employee_Addresses and
orion.Payroll contain Employee_ID.
 Names are stored in the Employee_Name column
as Last, First.

41
Inner Joins
proc sql;
title "Australian Employees' Birth Months";
select Employee_Name as Name format=$25.,
City format=$25.,
month(Birth_Date) 'Birth Month' format=3.
from orion.Employee_Payroll,
orion.Employee_Addresses
where Employee_Payroll.Employee_ID=
Employee_Addresses.Employee_ID
and Country='AU'
order by 3,City, Employee_Name;
quit;

s105d05
42
Inner Joins
Partial PROC SQL Output

Australian Employees Birthday Months

Birth
Name City Month
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Aisbitt, Sandy Melbourne 1
Graham-Rowe, Jannene Melbourne 1
Hieds, Merle Melbourne 1
Sheedy, Sherie Melbourne 1
Simms, Doungkamol Melbourne 1
Tannous, Cos Melbourne 1
Body, Meera Sydney 1
Clarkson, Sharryn Sydney 1
Dawes, Wilson Sydney 1
Rusli, Skev Sydney 1
Glattback, Ellis Melbourne 2
Gromek, Gladys Melbourne 2

43
Inner Join Alternate Syntax
An inner join can also be accomplished using an alternate
syntax, which limits the join to a maximum of two tables.
General form of an inner join:
SELECT
SELECTcolumn-1
column-1<, <, …column-n>
…column-n>
FROM
FROM table-1
table-1
INNER
INNERJOIN JOIN
table-2
table-2
ON
ONjoin-condition(s)
join-condition(s)
<other
<otherclauses>;
clauses>;

 This syntax is common in SQL code produced by


code generators such as SAS Enterprise Guide.
The ON clause specifies the JOIN criteria; a
WHERE clause can be added to subset the results.
44
Inner Join Alternate Syntax
proc sql;
title "Australian Employees' Birth Months";
select Employee_Name as Name format=$25.,
City format=$25.,
month(Birth_Date) 'Birth Month' format=3.
from orion.Employee_Payroll
inner join
orion.Employee_Addresses
on Employee_Payroll.Employee_ID=
Employee_Addresses.Employee_ID
where Country='AU'
order by 3,City, Employee_Name;
quit;

s105d06
45
46
5.04 Multiple Choice Poll
How many tables can be combined using a single inner
join?
a. 2
b. 32
c. 256
d. 512
e. Limited only by my computer’s resources
f. No limit

47
5.04 Multiple Choice Poll – Correct Answer
How many tables can be combined using a single inner
join?
a. 2
b. 32
c. 256
d. 512
e. Limited only by my computer’s resources
f. No limit

48
49
Outer Joins
Inner joins returned only matching rows. When you join
tables, you might want to include nonmatching rows as
well as matching rows.

50
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.

Left Full Right

51
Compare Inner Joins And Outer Joins
The following table is a comparison of inner and outer join
syntax and limitations:
Key Point Inner Join Outer Join
Table Limit 256 2
Join Behavior Returns matching rows Returns matching and
only nonmatching rows
Join Options Matching rows only LEFT, FULL, RIGHT
Syntax changes  Multiple tables in the ON clause that
FROM clause specifies join criteria
 WHERE clause that
specifies join criteria

52
Outer Joins
Outer join syntax is similar to the inner join alternate
syntax.
General form of an outer join:

SELECT
SELECTcolumn-1
column-1<, <, …column-n>
…column-n>
FROM
FROM table-1
table-1
LEFT|RIGHT|FULL
LEFT|RIGHT|FULLJOIN JOIN
table-2
table-2
ON
ONjoin-condition(s)
join-condition(s)
<other
<otherclauses>;
clauses>;

The ON clause specifies the join criteria in outer joins.

53
Determining Left and Right
Consider the position of the tables in the FROM clause.
 Left joins include all rows from the first (left) table,

even if there are no matching rows in the second


(right) table.
 Right joins include all rows from the second (right)

table, even if there are no matching rows in the first


(left) table.
 Full joins include all rows from both tables, even if

there are no matching rows in either table.


Left table Right table

FROM
FROM table-1
table-1join-type
join-typetable-2
table-2
ON
ONjoin-condition(s);
join-condition(s);
54
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
55
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
56
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
57
Business Scenario
List the employee ID and gender for all
married employees. Include the names
of any charities to which the employee
donates via the company program.

58
Business Scenario
Considerations:
 The table orion.Employee_Payroll
contains gender and marital status information.

Employee_Payroll
(all employees)

59 ...
Business Scenario
Considerations:
 The table orion.Employee_Payroll
contains gender and marital status information.
 The table orion.Employee_Donations
contains records only for those employees who
donate to a charity via the company program.

Employee_Payroll
(all employees)

Employee_Donations
(employees who
donate to charity)
60 ...
Business Scenario
Considerations:
 The table orion.Employee_Payroll
contains gender and marital status information.
 The table orion.Employee_Donations
contains records only for those employees who
donate to a charity via the company program.
 Less than half of all employees are married.

Married Employees Employee_Payroll


(get all of these) (all employees)

Employee_Donations
(employees who
donate to charity)
61 ...
Business Scenario
Considerations:
 The table orion.Employee_Payroll
contains gender and marital status information.
 The table orion.Employee_Donations
contains records only for those employees who
donate to a charity via the company program.
 Less than half of all employees are married.

Married Employees Employee_Payroll


(get all of these) (all employees)

Employees who
Employee_Donations
donate to charity:
(employees who
(include donation data
donate to charity)
when matched)
62
64
5.05 Multiple Choice Poll
For the report, you need the data for all married
employees from orion.Employee_Payroll.
You also want to include the charity names from the
orion.Employee_Donations table if
Employee_ID matches. What type of join should you
use to combine the information from these two tables?
a. Inner Join

b. Left Join

c. Full Join

d. None of the above

65
5.05 Multiple Choice Poll – Correct Answer
For the report, you need the data for all married
employees from orion.Employee_Payroll.
You also want to include the charity names from the
orion.Employee_Donations table if
Employee_ID matches. What type of join should you
use to combine the information from these two tables?
a. Inner Join

b. Left Join

c. Full Join

d. None of the above

66
Outer Joins
proc sql;
select Employee_payroll.Employee_ID,
Employee_Gender, Recipients
from orion.Employee_payroll
left join
orion.Employee_donations
on Employee_payroll.Employee_ID=
Employee_donations.Employee_ID
where Marital_Status="M"
;
quit;

s105d10
67
Outer Joins
Partial PROC SQL Output (Rows 203-215)

 Remember that output order is not guaranteed unless you use an ORDER BY clause.

Employee_
Employee_ID Gender Recipients
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
121128 F Cancer Cures, Inc.
121131 M Vox Victimas 40%, Conserve Nature, Inc. 60%
121132 M EarthSalvors 50%, Vox Victimas 50%
121133 M Disaster Assist, Inc.
121138 M Cuidadores Ltd.
121139 F
121142 M AquaMissions International 10%, Child Survivors 90%
121143 M Mitleid International 60%, Save the Baby Animals
40%
121144 F
121145 M Save the Baby Animals
121146 F
121147 F Cuidadores Ltd. 50%, Mitleid International 50%
121148 M

68
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.
General form of the FROM clause:

SELECT
SELECTalias-1.column-1<,
alias-1.column-1<, …alias-2.column-n>
…alias-2.column-n>
FROM
FROMtable-1
table-1AS
ASalias-1
alias-1
join-type
join-type
table-2
table-2AS
ASalias-2
alias-2
ON
ONjoin-condition(s)
join-condition(s)
<other
<otherclauses>;
clauses>;

69
Using a Table Alias
proc sql;
select p.Employee_ID, Employee_Gender,
Recipients
from orion.Employee_payroll as p
left join
orion.Employee_donations as d
on p.Employee_ID=d.Employee_ID
where Marital_Status="M"
;
quit;

s105d11
70
DATA Step Merge (Review)
A DATA step with MERGE and BY statements
automatically overlays same-name columns.
Table One Table Two
X A X B Table One must be sorted or
1 a 2 x indexed on column X before
4 d 3 y a merge can be performed.
2 b 5 v
Output
data merged; X A B
1 a
merge one two; 2 b x
by x; 3 y
run; 4 d
proc print data=merged; 5 v
run;

s105d12
71 ...
SQL Join versus DATA Step Merge
SQL joins do not automatically overlay same-named
columns.
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Output
proc sql; X A B
1 a
select one.x, a, b 2 b x
from one full join two y
on one.x=two.x 4 d
; v
quit;

s105d12
72
The COALESCE Function
The COALESCE function returns the value of the first
non-missing argument.
General form of the COALESCE function:

COALESCE(argument-1,argument-2<,
COALESCE(argument-1,argument-2<, ...argument-n)
...argument-n)

argument can be a constant, expression, or variable


name. When all arguments are missing,
COALESCE returns a missing value.
 All arguments must be of the same type (character
or numeric).

73
SQL Join versus DATA Step Merge
You can use the COALESCE function to overlay columns.
Table One Table Two
X A X B
1 a 2 x
4 d 3 y
2 b 5 v
Output
proc sql; X A B
1 a
select coalesce(one.x,two.x) 2 b x
as x,a,b 3 y
from one full join two 4 d
on one.x=two.x; 5 v
quit;

s105d12
74
SQL Join versus DATA Step Merge
Key Points SQL Join DATA Step
Merge
Explicit sorting of data Not required Required
before join/merge
Same-named columns in Not required Required
join/merge expressions
Equality in join or merge Not required Required
expressions

75
76
Exercise

This exercise reinforces the concepts discussed


previously.

77
Chapter 5: SQL Joins

5.1: Introduction to SQL Joins

5.2: Complex SQL Joins

78
Objectives
 Create and use in-line views.
 Use in-line views and subqueries to simplify coding
a complex query.

79
In-Line Views
In-line views are often useful when you build complex
SQL queries.

An in­line view is
 a temporary “virtual table” that exists only during

query execution
 created by placing a query expression in a FROM

clause where a table name would normally be used.

80
In-Line Views
An in-line view is a query expression (SELECT statement)
that resides in a FROM clause. It acts as a virtual table,
used in place of a physical table in a query.
proc sql;
select *
from
(in-line view query expression)
quit;

81
Business Scenario
List all active Sales employees having annual salaries
significantly (more than 5%) lower than the average salary
for everyone with the same job title.

82
Considerations
First, you must calculate the average salaries for active
employees in the Sales department, grouped by job title.

Next, you must match each employee to a GROUP-BY


job title.

Finally, you must compare the employee's salary to the


group's average to determine if it is more than 5% below
the group average.

83
In-Line Views
Build a query to produce the aggregate averages.
proc sql;
title 'Sales Department Average Salary';
title2 'By Job Title';
select Job_Title,
avg(Salary) as Job_Avg
format=comma7.
from orion.Employee_payroll as p,
orion.Employee_organization as o
where p.Employee_ID=o.Employee_ID
and not Employee_Term_Date
and o.Department="Sales"
group by Job_Title;
quit;
s105d13
84
In-Line Views
PROC SQL Output
Sales Department Average Salary
by Job Title

Job_Title Job_Avg
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Sales Rep. I 26,576
Sales Rep. II 27,348
Sales Rep. III 29,214
Sales Rep. IV 31,589

85
In-Line Views
If you create a table from the results of
the query, you can join this table and the
orion.Employee_payroll table
and subset the appropriate rows to get
the answer. This adds unnecessary I/O.

Would it be useful to use only the query


itself in place of a table?

In SQL, you can with an in-line view!

86
In-Line Views
Using a query in the FROM clause in place of a table
causes the query output to be used as an in-line view.
proc sql;
title 'Employees with salaries less than';
title2 '95% of the average for their job';
select Employee_Name, emp.Job_Title,
Salary format=comma7., Job_Avg format=comma7.
from (select Job_Title,
avg(Salary) as Job_Avg format=comma7.
from orion.Employee_payroll as p,
orion.Employee_organization as o
where p.Employee_ID=o.Employee_ID
and not Employee_Term_Date
and o.Department="Sales"
group by Job_Title) as job,
orion.Salesstaff as emp
where emp.Job_Title=job.Job_Title
and Salary < Job_Avg*.95
order by Job_Title, Employee_Name;
s105d14
87
In-Line Views
PROC SQL Output
Employees with salaries less than
95% of the average for their job

Employee
Annual
Employee_Name Employee Job Title Salary Job_Avg
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
Ould, Tulsidas Sales Rep. I 22,710 26,576
Polky, Asishana Sales Rep. I 25,110 26,576
Tilley, Kimiko Sales Rep. I 25,185 26,576
Voron, Tachaun Sales Rep. I 25,125 26,576

88
89
Business Scenario
In 2003, Top Sports launched a premium line of sleeping
bags called Expedition Zero, which was sold through
Orion Star.
The CEO of Top Sports wants to send a letter of thanks
to the manager of each employee who sold Expedition
Zero sleeping bags in 2003, with a $50 reward certificate
(in U.S. dollars) to be presented by the manager to the
employee.
The Task:
Prepare a list of the managers’ names and the cities
in which they are located.

90
Planning the Complex Query
Identify the employees who sold Expedition
Step 1
Zero merchandise in 2003.

Find the employee identifier for the managers


Step 2
of these employees

Obtain the managers’ names and city


Step 3
information.

91
Complex Query: Step 1 Considerations
Get employee IDs for employees who sold
Step 1
Expedition Zero merchandise in 2003.

Select the employee’s identifier (Employee_ID)from


the results of joining the Order_Fact and
Product_Dim tables on Product_ID, where
Product_Name contains Expedition Zero.
Exclude Internet orders (Employee_ID NE
99999999).

92
Coding the Complex Query
Write a query to obtain the employee ID of
Step 1
all employees who sold Expedition Zero
merchandise in 2003.

select distinct Employee_ID


from orion.Order_Fact as o,
orion.Product_Dim as p
where o.Product_ID=p.Product_ID
and year(Order_Date)=2003
and Product_Name contains
'Expedition Zero'
and Employee_ID ne 99999999;

s105d15
93
Coding the Complex Query
Step 1 PROC SQL Output
Employee ID
ƒƒƒƒƒƒƒƒƒƒƒƒ
120145
120732

94
Complex Query: Step 2 Considerations
Find the employee identifier for the managers
Step 2
of these employees.

Select the manager’s identifier (Manager_ID) from


the results of joining the Employee_Organization
table with the first query’s results on Employee_ID.

95
96
5.06 Multiple Choice Poll
To join the Employee_Organization table with the
Step 1 query results, you use the query from Step 1 as
which of the following?

a. an in-line view
b. a subquery

97
5.06 Multiple Choice Poll – Correct Answer
To join the Employee_Organization table with the
Step 1 query results, you use the query from Step 1 as
which of the following?

a. an in-line view
b. a subquery

A query used in place of a physical table in a SELECT


statement FROM clause is called an in-line view.

98
Coding the Complex Query
Write a query to obtain the manager ID
Step 2
of the employee’s manager.
select Manager_ID
from orion.Employee_Organization as o,
(<Step 1 query results>) as ID
where o.Employee_ID=ID.Employee_ID;

Employee_ID
120145
120732

99
Coding the Complex Query
Write a query to obtain the manager ID
Step 2
of the employee’s manager.
t Manager_ID
om orion.Employee_Organization as o,
(select distinct Employee_ID
from orion.Order_Fact as o,
orion.Product_Dim as p
where o.Product_ID=p.Product_ID
and year(Order_Date)=2003
Employee_ID
and Product_Name
120145
contains 'Expedition Zero'
120732
and Employee_ID ne 99999999)as ID
ere o.Employee_ID=ID.Employee_ID;

s105d16
100
Coding the Complex Query
Step 2 PROC SQL Output

Manager_ID
ƒƒƒƒƒƒƒƒƒƒ
120103
120736

101
Complex Query: Step 3 Considerations
Find the managers’ names and cities.
Step 3

Select the employee’s name (Employee_Name) and


City from the Employee_Addresses table, where
Employee_ID matches Manager_ID
in the results of the previous query.

102
103
5.07 Poll
Is it possible to use the entire query in Step 2 as
a subquery?
 Yes
 No

104
5.07 Poll – Correct Answer
Is it possible to use the entire query in Step 2 as
a subquery?
 Yes
 No

A subquery can return values for multiple rows,


but must return values for only one column. When
submitted on its own, the query in Step 2 returns
two rows and only one column, so it can be used
as a non-correlated subquery.

105
Coding the Complex Query
Step 3 Write a query to obtain the managers’ names
and city information.
proc sql;
select Employee_Name format=$25. as Name, City
from orion.Employee_Addresses
where Employee_ID in
(<Step 2 query results>);

Manager_ID
120145
120732

106
Coding the Complex Query
proc sql;
select Employee_Name format=$25. as Name
, City
from orion.Employee_Addresses
where Employee_ID in
(select Manager_ID
from orion.Employee_Organization as o,
(select distinct Employee_ID
Step 3 from orion.Order_Fact as o,
orion.Product_Dim as p
where o.Product_ID=p.Product_ID
Manager_ID and year(Order_Date)=2003
120145 and Product_Name contains
120732 'Expedition Zero'
and Employee_ID ne 99999999) as ID
where o.Employee_ID=ID.Employee_ID);

s105d17
107
Coding the Complex Query
Step 3 PROC SQL Output

Name City

ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
ƒ
Dawes, Wilson Sydney
Kiemle, Parie Miami-Dade

108
Coding the Complex Query
You can also solve this problem using a multiway join.
proc sql;
select distinct Employee_Name format=$25. as Name, City
from orion.Order_Fact as of,
orion.Product_Dim as pd,
orion.Employee_Organization as eo,
orion.Employee_Addresses as ea
where of.Product_ID=pd.Product_ID
and of.Employee_ID=eo.Employee_ID
and ea.Employee_ID=eo.Manager_ID
and Product_Name contains 'Expedition Zero'
and year(Order_Date)=2003
and eo.Employee_ID ne 99999999
;
quit;

s105d18
109
Chapter Review
1. How many rows are returned by the following query?

proc sql; Table1 Table2


X A X B
select *
1 a 2 x
from
3 d 1 y
table1,table2; 2 b 3 v
quit;

110
Chapter Review Answers
1. How many rows are returned by the following query?

proc sql; Table1 Table2


X A X B
select *
1 a 2 x
from
3 d 1 y
table1,table2; 2 b 3 v
quit;

This query produces a Cartesian product.


Nine rows will be returned.

111
Chapter Review
2. Which of the following statements describes an advantage of using a PROC SQL view?
a. Views often save space, because a view is usually
quite small compared with the data that it accesses.
b. Views can provide users a simpler alternative to
frequently retrieving and submitting query code to
produce identical results.
c. Views hide complex query details from users.
d. All of the above

112
Chapter Review Answers
2. Which of the following statements describes an advantage of using a PROC SQL view?
a. Views often save space, because a view is usually
quite small compared with the data that it accesses.
b. Views can provide users a simpler alternative to
frequently retrieving and submitting query code to
produce identical results.
c. Views hide complex query details from users.
d. All of the above

113
Chapter Review
3. Outer and Inner Joins:
a. An outer join can operate on a maximum of ___
tables simultaneously.
b. An inner join can operate on a maximum of ___
tables simultaneously.

114
Chapter Review Answers
3. Outer and Inner Joins:
a. An outer join can operate on a maximum of _2_
tables simultaneously.
b. An inner join can operate on a maximum of _256
tables simultaneously.

115
Chapter Review
4. True or False:
An in-line view can be used on a WHERE or HAVING
clause and can return many rows of data, but must
return only one column.

116
Chapter Review Answers
4. True or False:
An in-line view can be used on a WHERE or HAVING
clause and can return many rows of data, but must
return only one column.
False
An in-line view is a query used in the FROM
clause in place of a table. An in-line view can
return any number of rows or columns.

117

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy