Subquery
Subquery
Subqueries
Subquery
A subquery is a SQL query nested inside a larger
query.
A subquery may occur in :
- A SELECT clause
- A FROM clause
- A WHERE clause
The subquery can be nested inside a SELECT,
INSERT, UPDATE, or DELETE statement or inside
another subquery.
Continue…..
A subquery is usually added within the WHERE Clause of another SQL
SELECT statement.
You can use the comparison operators, such as >, <, or =. The
comparison operator can also be a multiple-row operator, such as IN,
ANY, or ALL.
A subquery is also called an inner query or inner select, while the
statement containing a subquery is also called an outer query or outer
select.
The inner query executes first before its parent query so that the results
of an inner query can be passed to the outer query.
.
Syntax:-
SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE "condition");
Syntax :
Select select_list from table where expr opearator
(select select_list from table);
The subquery (inner query) executes once before the main query
(outer query) executes.
The main query (outer query) use the subquery
result.
Using a Subquery
to Solve a Problem
“Who has a salary greater than Jones’?”
Main Query
Subquery
?
“What is Jones’ salary?”
Subqueries
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
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
----------
KING
FORD
SCOTT
Guidelines for Using
Subqueries
⚫ Enclose subqueries in parentheses.
⚫ Place subqueries on the right side of the
comparison operator.
⚫ Do not add an ORDER BY clause to a
subquery.
⚫ Use single-row operators with single-row
subqueries.
⚫ Use multiple-row operators with multiple-
row subqueries.
Types of Subqueries
⚫ Single-row subquery : Queries that return only one row from the
inner SELECT statement
Main query
returns
Subquery CLERK
• Multiple-row subquery Queries that return more than one row from the
inner SELECT statement
Main query
returnsCLERK
Subquery
MANAGER
• Multiple-column subqueryQueries that return more than
one column from the inner SELECT statement
Main query
returns CLERK 7900
Subquery
MANAGER 7698
Single-Row Subqueries
⚫ Return only one row
⚫ Use single-row comparison operators
Operator Meaning
= Equal to
ENAME JOB
---------- ---------
JAMES CLERK
SMITH CLERK
ADAMS CLERK
MILLER CLERK
Example:-
Find the employee whose salary is
second highest
Select max(sal) from emp where
sal<(select max(Sal) from emp);
Third highest:-
Select max(sal) from emp where sal<(
select max(sal) from emp where
sal<(select max(Sal) from emp));
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.
SQL> SELECT ename, job
2 FROM emp
3 WHERE job = CLERK
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal > 1100
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);
ENAME JOB
---------- ---------
MILLER 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.
The SQL statement on the slide displays all the departments that
have a minimum salary greater than that of department 20.
ERROR:
ORA-01427: single-row subquery returns more than
one row
no rows 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.
Multiple-Row Subqueries
⚫ Return more than one row
⚫ Use multiple-row comparison operators
Operator Meaning