proc sql;
select jobcode, avg(salary) as AvgSalary
format=dollar11.2
from certadv.payrollmaster
group by jobcode
having avg(salary)>(select avg(salary)
from certadv.payrollmaster);
quit;
Type
|
Description
|
---|---|
noncorrelated
|
A noncorrelated
subquery is a self-contained subquery that executes
independently of the outer query. The simplest type of subquery is
a noncorrelated subquery that returns a single value.
|
correlated
|
A dependent
subquery is one that requires one or more values to
be passed to it by the outer query before the subquery can return
a value to the outer query.
|
proc sql; select jobcode, avg(salary) as AvgSalary format=dollar11.2 from certadv.payrollmaster group by jobcode having avg(salary)> /*1*/ (select avg(salary) /*2*/ from certadv.payrollmaster); quit;
1 | The HAVING clause completes the expression by calculating the subquery. The subquery calculates the average salary for the entire company, all rows in the table, using the AVG summary function with Salary as an argument. The subquery returns the value of the average salary to the outer query. |
2 | In the SELECT clause, the outer query calculates the average salary for each JobCode group as defined by the GROUP BY clause. It selects only the groups whose average salary is greater than the company’s average salary. |
proc sql; select empid, lastname, firstname, city, state from certadv.staffmaster where empid in /*1*/ (select empid from certadv.payrollmaster /*2*/ where month(dateofbirth)=2); /*3*/ quit;
1 | The WHERE expression selects the employees whose birthday is in the month of February. Note that the MONTH function is used in the subquery. |
2 | The subquery returns the EmpID values of the selected employees to the outer query. |
3 | The outer query displays data for the employees identified by the subquery. |
where dateofbirth < any <subquery...>
Conditional Operator
|
Operator Looks for These
Values
|
Example
|
---|---|---|
ANY
|
values that meet a specified
condition with respect to any one of the values returned by a subquery
|
where dateofbirth < any
(select dateofbirth
from certadv.payrollmaster
where jobcode='FA3')
|
ALL
|
values that meet a specified
condition with respect to all the values returned by a subquery
|
where dateofbirth < all
(select dateofbirth
from certadv.payrollmaster
where jobcode='FA3')
|
EXISTS
|
values that are returned
by a subquery
|
where exists
(select *
from certadv.flightschedule
where fa.empid=
flightschedule.empid)
|
where dateofbirth < any <subquery...>
Comparison Operator
with ANY
|
Outer Query Action
|
Example
|
---|---|---|
> ANY
|
Selects values that
are greater than any value returned by the subquery.
|
If the subquery returns
the values
20 , 30 , 40 ,
the outer query selects all values that are > 20 (the
lowest value that was returned by the subquery). |
< ANY
|
Selects values that
are less than any value returned by the subquery.
|
If the subquery returns
the values
20 , 30 , 40 ,
the outer query selects all values that are < 40 (the
highest value that was returned by the subquery). |
= ANY
|
Selects values that
are equal to any value returned by the subquery.
|
If the subquery returns
the values
20 , 30 , 40 ,
the outer query selects all values that are = 20 or
= 30 or = 40 . |
proc sql; select empid, jobcode, dateofbirth from certadv.payrollmaster where jobcode in ('FA1', 'FA2') and dateofbirth <any /*1*/ (select dateofbirth /*2*/ from certadv.payrollmaster where jobcode='FA3'); quit;
1 | The subquery returns the birth dates of all level-3 flight attendants. |
2 | The outer query selects only level-1 and level-2 flight attendants whose birth dates indicate that they are older than any of the others whose birth dates are returned by the subquery. |
where jobcode in ('FA1','FA2') and dateofbirth < (select max(dateofbirth) from [...]
Comparison Operator
with ALL
|
Sample Values Returned
by Subquery
|
Effect
|
---|---|---|
> ALL
|
(
20 , 30 , 40 ) |
>
40 (greater than the highest
number in the list)
|
< ALL
|
(
20 , 30 , 40 ) |
<
20 (less than the lowest
number in the list)
|
proc sql; select empid, jobcode, dateofbirth from certadv.payrollmaster where jobcode in ('FA1', 'FA2') and dateofbirth < all /*1*/ (select dateofbirth /*2*/ from certadv.payrollmaster where jobcode='FA3'); quit;
1 | The subquery returns the birth dates of all level-3 flight attendants. |
2 | The outer query selects only level-1 and level-2 flight attendants whose birth dates indicate that they are older than any of the others whose birth dates are returned by the subquery. |
where jobcode in ('FA1','FA2') and dateofbirth < (select min(dateofbirth) from [...]
proc sql; select lastname, firstname from certadv.staffmaster where 'NA'= (select jobcategory from certadv.supervisors where staffmaster.empid = supervisors.empid); quit;
Condition
|
Requirement
|
---|---|
EXISTS
|
the subquery returns
at least one row
|
NOT EXISTS
|
the subquery returns
no data
|
proc sql;
select lastname, firstname
from certadv.flightattendants
where not exists
(select * from certadv.flightschedule
where flightattendants.empid=
flightschedule.empid);
quit;