CS 236: Advanced Database Management Systems
Topic: Subquery
BSCS13- ABCDE
Contents
• Subquery
• Single Row Subquery
• Single Row Operator
• Multiple Row Subquery
• Multiple Row Operator
2
Dependent and Correlated Data
Retrieval
3
Using a Subquery to Solve a Problem
Who has a salary greater than Abel’s?
Main Query:
Which employees have salaries greater
?
than Abel’s salary?
Subquery
?
What is Abel’s salary?
4
Subquery Syntax
SELECT column_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
• The subquery (inner query) executes once before the main
query.
• The result of the subquery is used by the main query (outer
query).
5
Using a Subquery
SELECT last_name
FROM employees 11000
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
6
Guidelines for Using
Subqueries
• Enclose subqueries in parentheses.
• Place subqueries on the right side of the
comparison condition.
• The ORDER BY clause in the subquery is not
needed unless you are performing Top-N analysis.
• Use single-row operators with single-row
subqueries and use multiple-row operators with
multiple-row subqueries.
SELECT
SELECTname,
name,salary
salary
FROM
FROMemployees
employees
WHERE
WHEREsalary
salaryIN
IN((
SELECT
SELECTsalary
salaryFROM
FROMemployees
employeesORDER
ORDERBY
BYsalary
salaryDESC
DESCLIMIT
LIMIT55
););
7
Types of
Subqueries
• Single row
• Multiple row
• Multiple column
• Scalar
• Correlated
• Exist Operator
8
Types of Subqueries
• Single-row subquery
Main query
returns
Subquery Single value
• Multiple-row subquery
Main query
returns More than one values
Subquery
9
Single-Row Subqueries
• Return only one row
• Use single-row comparison operators
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
10
Problem
Display the employees whose job ID is the same as
that of employee 141.
11
Executing Single-Row Subqueries
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = ST_CLERK
(SELECT job_id
FROM employees
WHERE employee_id = 141)
AND salary > 2600
(SELECT salary
FROM employees
WHERE employee_id = 143);
12
Problem
Select employee last name, job ID, and salary of all employees whose salary is
equal to the minimum salary.
13
Using Group Functions in a Subquery
SELECT last_name, job_id, salary
FROM employees 2500
WHERE salary =
(SELECT MIN(salary)
FROM employees);
14
Problem
Select departments that have a minimum salary greater than that of department
50.
15
The HAVING Clause with Subqueries
• The Oracle server executes subqueries first.
• The Oracle server returns results into the
HAVING clause of the main query.
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
2500
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
16
What is Wrong
with this Statement?
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
17
What is Wrong
with this Statement?
SELECT employee_id, last_name
FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);
ERROR
ERROR 1242
1242 (21000):
(21000): Subquery
Subquery returns
returns more
more than
than 11 row
row
Single-row
Single-row operator
operator with
with multiple-row
multiple-row subquery
subquery
salary = (subquery) expects a single salary value, causing an error.
18
Will this Statement
Return Rows?
SELECT last_name, job_id
FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');
no
no rows
rows selected
selected
Subqueryy returns
Subquer no values
returns no values
19
Multiple-Row Subqueries
• Return more than one row
• Use multiple-row comparison operators
Operator Meaning
IN Equal to any member in the list
ANY Compare value to each value returned by
the subquery
Compare value to every value returned by
ALL
the subquery
20
Using the IN Operator
in Multiple-Row Subqueries
SELECT
SELECT employee_id,
employee_id, last_name
last_name
FROM
FROM employees
employees 45000, 60000, 70000
WHERE
WHERE salary
salary IN
IN (SELECT
(SELECT MIN(salary)
MIN(salary)
FROM
FROM employees
employees
GROUP
GROUP BY BY department_id);
department_id);
21
Using the ANY Operator in Multiple-
Row Subqueries
The ANY operator is used in subqueries to compare a value against any value returned
by the subquery.
SELECT
SELECT employee_id,
employee_id, last_name,
last_name, salary salary
FROM
FROM employees
employees
45000, 60000, 70000 🔹 This means salary > 45000 OR salary > 60000 OR salary > 70000 is
WHERE salary > ANY
WHERE salary > ANY (( true.
SELECT
SELECT MIN(salary)
MIN(salary)
FROM
FROM employees
employees
GROUP
GROUP BY
BY department_id
department_id
);
);
22
Using the ALL Operator
in Multiple-Row Subqueries
SELECT employee_id, last_name, salary
FROM employees 45000, 60000, 70000
WHERE salary > ALL (
SELECT MIN(salary)
🔹 ALL means the salary must be greater than all of these values.
FROM employees
🔹 This means salary > 70000 must be true.
GROUP BY department_id
);
23
Correlated Subquery with EXIST
A correlated subquery requires values from its outer query in
order to execute.
The EXISTS operator is used to test for the existence of any
record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one
or more records.
25
Correlated Subquery with
EXIST
SELECT
SELECT employee_id,
employee_id, last_name,
last_name, department_id
department_id
FROM
FROM employees
employees ee
WHERE
WHERE EXISTS
EXISTS ((
SELECT
SELECT 11
FROM
FROM departments
departments dd
WHERE
WHERE d.department_id
d.department_id == e.department_id
e.department_id
);
);
• ✅ Returns employees only if their
department exists in the departments
table.
26
Correlated Subquery with
EXIST
SELECT
SELECT employee_id,
employee_id, last_name,
last_name, department_id
department_id
FROM
FROM employees
employees ee
WHERE
WHERE EXISTS
EXISTS ((
SELECT
SELECT 11
FROM
FROM departments
departments dd
WHERE
WHERE d.department_id
d.department_id == e.department_id
e.department_id
);
);
27
Correlated Subquery with
EXIST
28
Summary
In this lesson, you have learned how to:
• Identify when a subquery can help solve a
question
• Write subqueries when a query is based on
unknown values
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
29
30
Happy
Learning!