Professional Documents
Culture Documents
1
Subqueries: Noncorrelated
Example: Display job codes where the group’s
average salary exceeds the company’s
average salary.
proc sql; Then pass results
select JobCode,avg(Salary) as MeanSalary to outer query
from airline.payrollmaster
group by JobCode
having avg(Salary) >
(select avg(Salary)
from airline.payrollmaster);
Evaluate
first
2 c2s6d01
...
Subqueries: Noncorrelated
• After the subquery executes, the outer query
code resolves to:
proc sql;
select JobCode,avg(Salary) as MeanSalary
from airline.payrollmaster
group by JobCode
having avg(Salary) > (54079.65);
3
Subqueries: Noncorrelated
Output
Job
Code MeanSalary
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
ME3 59375
NA1 58845.08
NA2 73336.2
PT1 95071.2
PT2 122253.6
PT3 154706.3
TA3 55551.42
4
Subqueries: Noncorrelated
Example: Send birthday cards to employees
with February birthdays. Names and
addresses
are in airline.staffmaster, and
birth dates are in
proc sql;
airline.payrollmaster
select EmpID, LastName, FirstName, .
City, State
from airline.staffmaster
where EmpID in
(select EmpID
from airline.payrollmaster
where month(DateOfBirth)=2);
5 c2s6d02
Noncorrelated Subqueries:
How Do They Work?
airline.payrollmaster
Partial Listing
EmpID DateOfBirth
proc sql;
... . . .
select EmpID, LastName,
1038 11/13/1967
FirstName, City, State 1420 02/23/1963
from airline.staffmaster 1561 12/03/1961
where EmpID in 1434 07/14/1960
(select EmpID 1414 03/28/1970
from airline.payrollmaster 1112 12/03/1962
where month(DateOfBirth)=2); 1390 02/23/1963
1332 09/20/1968
... . . .
Step 1: Evaluate the inner query and build a virtual table that satisfies the
WHERE criteria.
6 c2s6d02
Noncorrelated Subqueries:
How Do They Work?
airline.payrollmaster
Partial Listing
EmpID DateOfBirth
proc sql;
... . . .
select
select EmpID,
EmpID, LastName,
LastName,
1038 11/13/1967
FirstName, City, State 1420 02/23/1963
from airline.staffmaster 1561 12/03/1961
where EmpID in 1434 07/14/1960
(select EmpID 1414 03/28/1970
from airline.payrollmaster 1112 12/03/1962
where month(DateOfBirth)=2); 1390 02/23/1963
1332 09/20/1968
... . . .
Emp
ID LastName FirstName City State
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1403 BOWDEN EARL BRIDGEPORT CT
1404 CARTER DONALD NEW YORK NY
1834 LONG RUSSELL NEW YORK NY
1103 MCDANIEL RONDA NEW YORK NY
1420 ROUSE JEREMY PATERSON NJ
1390 SMART JONATHAN NEW YORK NY
9
Correlated Subqueries
• Rules for correlated subqueries include the
following:
– cannot be evaluated independently, but depend
on
the values returned by the outer query for their
results
– are evaluated for each row in the outer query
10
•
Correlated Subqueries
Example: Display the names and states
of all navigator managers.
proc sql;
select LastName, FirstName, State
from airline.staffmaster
where 'NA'=
(select JobCategory
from airline.supervisors
where staffmaster.EmpID=
supervisors.EmpID) ;
You must qualify each
column with a table name.
11 c2s6d05
...
airline.staffmaster
Partial Listing
20
airline.staffmaster
Partial Listing
29
airline.staffmaster
Partial Listing
33