Professional Documents
Culture Documents
Subquery
Subquery
Subquery
Objectives
Subquery
?
“What is Jones’ salary?”
Subqueries
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
The
The subquery
subquery (inner
(inner query)
query) executes
executes once
once before
before the
the main
main query.
query.
The
The result
result of
of the
the subquery
subquery isis used
used by
by the
the main
main query
query (outer
(outer query).
query).
Subqueries
A subquery is a SELECT statement that is embedded in a clause of
another SELECT statement.
They can be very useful when you need to select rows from a table with
a condition that depends on the data in the table itself.
You can place the subquery in a number of SQL clauses:
WHERE clause
HAVING clause
FROM clause
Using a Subquery
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);
In the example, the inner query determines the salary of employee 7566. The outer
query takes the result of the inner query and uses this result to display all the employees
who earn more than this amount.
ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT
Guidelines
Guidelines for
for Using
Using Subqueries
Subqueries
Enclose
Enclose subqueries
subqueries in
in parentheses.
parentheses.
Place
Place subqueries
subqueries on
on the
the right
right side
side of
of the
the
comparison
comparison operator.
operator.
Do
Do not
not add
add an
an ORDER
ORDER BY BY clause
clause to to aa
subquery.
subquery.
Use
Use single-row
single-row operators
operators with
with single-row
single-row
subqueries.
subqueries.
Use
Use multiple-row
multiple-row operators
operators with
with multiple-
multiple-
row
row subqueries.
subqueries.
GROUPBY
GROUPBY command
command can can be
be used
used
The
The subquery
subquery generally
generally executes
executes first,
first, and
and
its
its output
output is
is used
used toto complete
complete the
the query
query
condition
condition for
for the
the main
main or
or outer
outer query.
query.
Types of Subqueries
Single-row subquery :: Queries
Single-row subquery Queries that
that return
return only
only one
one row
row from
from the
the
inner
inner SELECT
SELECT statement
statement
Main query
returns
Subquery CLERK
•• Multiple-row subquery Queries
Multiple-row subquery Queries that
that return
return more
more than
than one
one row
row from
from the
the inner
inner SELECT
SELECT statement
statement
Main query
Subquery
returns CLERK
MANAGER
Multiple-column subqueryQueries
•• Multiple-column subqueryQueries that
that return
return more
more than
than one
one column
column from
from the
the inner
inner SELECT
SELECT statement
statement
Main query
returns CLERK 7900
Subquery
MANAGER 7698
Single-Row Subqueries
Return
Return only
only one
one row
row
Use
Use single-row
single-row comparison
comparison operators
operators
Operator Meaning
= Equal to
ENAME JOB
---------- ---------
JAMES CLERK
SMITH CLERK
ADAMS CLERK
MILLER CLERK
Executing Single-Row Subqueries
The example on the slide displays employees whose
job title is the same as that of employee 7369 and
whose salary is greater than that of employee 7876.
ENAME
ENAME JOB
JOB
----------
---------- ---------
---------
MILLER
MILLER CLERK
CLERK
Using Group Functions
in a Subquery
Display the employee name, job title, and salary of all employees
whose salary is equal to the minimum salary.
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
SMITH
SMITH CLERK
CLERK 800
800
HAVING Clause with Subqueries
The
The Oracle
Oracle Server
Server executes
executes subqueries
subqueries first.
first.
The
The Oracle
Oracle Server
Server returns
returns results
results into
into the
the HAVING
HAVING
clause
clause of
of the
the main
main query.
query.
The SQL statement on the slide displays all the departments that
have a minimum salary greater than that of department 20.
no
no rows
rows selected
selected
Errors with Subqueries
One common error with subqueries is more than one row
returned for a single-row subquery.
In the SQL statement on the slide, the subquery contains a
GROUP BY (deptno) clause, which implies that the subquery
will return multiple rows, one for each group it finds. In this
case, the result of the subquery will be 800, 1300, and 950.
The outer query takes the results of the subquery (800, 950,
1300) and uses these results in its WHERE clause. The
WHERE clause contains an equal (=) operator, a single-row
comparison operator expecting only one value. The = operator
cannot accept more than one value from the subquery and
hence generates the error.
To correct this error, change the = operator to IN.
Will This Statement
Work?
SQL> SELECT ename, job
2 FROM emp
3 WHERE job =
4 (SELECT job
5 FROM emp
6 WHERE ename='SMYTHE');
l ues
o va
s n
no
no rows
rows selected
selected t ur n
r e
uery
ubq
S
Problems with Subqueries
A common problem with subqueries is no rows being returned by
the inner query.
In the SQL statement on the slide, the subquery contains a WHERE
(ename='SMYTHE') clause. Presumably, the intention is to find
the employee whose name is Smythe. The statement seems to be
correct but selects no rows when executed.
The problem is that Smythe is misspelled. There is no employee
named Smythe. So the subquery returns no rows. The outer query
takes the results of the subquery (null) and uses these results in its
WHERE clause. The outer query finds no employee with a job title
equal to null and so returns no rows.
Multiple-Row Subqueries
Return
Return more
more than
than one
one row
row
Use
Use multiple-row
multiple-row comparison
comparison operators
operators
Operator Meaning
EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7654
7654 MARTIN
MARTIN SALESMAN
SALESMAN
7521
7521 WARD
WARD SALESMAN
SALESMAN
EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7839
7839 KING
KING PRESIDENT
PRESIDENT
7566
7566 JONES
JONES MANAGER
MANAGER
7902
7902 FORD
FORD ANALYST
ANALYST
7788
7788 SCOTT
SCOTT ANALYST
ANALYST
>ALL means more than the maximum and <ALL means less than the
minimum.The NOT operator can be used with IN, ANY, and ALL operators.
FROM clause
A subquery can also be found in the FROM clause
select dept.deptno, subquery1.total_amt
from dept,
( select emp.deptno, Sum(emp.sal) total_amt
from emp
group by deptno) subquery1
WHERE subquery1.deptno = dept.deptno
SELECT clause
A subquery can also be found in the SELECT clause.
For example:
select tbls.owner, tbls.table_name,
(select count(column_name) as total_columns
from all_tab_columns cols
where cols.owner = tbls.owner
and cols.table_name = tbls.table_name) subquery2
;from all_tables tbls
Examples
Using subqueries to find out what values exist in one set of data
and not in another
Write a query to display the employee name and hiredate for all