ITE 014 | INFORMATION MANAGEMENT
MODULE 4: USING SUBQUERIES The result of the subquery is used by the
main query.
4.1 Using Subqueries to Solve Queries
A subquery is a SELECT statement that is
Introduction
embedded in a clause of another SELECT
Overview statement.
In this lesson, you learn about more advanced A subquery can be placed in the following clauses:
features of the SELECT statement. You can write
WHERE clause
subqueries in the WHERE clause of another SQL
statement to obtain values based on an unknown HAVING clause
conditional value. This lesson covers single-row
FROM clause
subqueries and multiple-row subqueries.
Syntax Details
Using a Subquery to Solve a Problem The operator includes a comparison
condition such as >, =, or IN.
Suppose you want to write a query to find out who
earns a salary greater than Abel’s salary. Comparison conditions fall into two classes:
To solve this problem, you need two queries: o Single-row operators: >, =, >=, <, <>,
<=
1. One to find how much Abel earns.
o Multiple-row operators: IN, ANY, ALL
2. A second query to find who earns more
than that amount. A subquery is often referred to as a nested SELECT,
sub-SELECT, or inner SELECT statement.
You can solve this problem by combining the two The subquery generally executes first, and its
queries, placing one query inside the other. output is used to complete the query condition for
the main (outer) query.
The inner query (subquery) returns a value
that is used by the outer query (main
query).
Using a Subquery
Using a subquery is equivalent to
performing two sequential queries and
using the result of the first query as the
search value in the second query.
Subquery Syntax
In the example:
The inner query determines the salary of
employee Abel.
The subquery (inner query) executes before
the main query (outer query). The outer query takes the result of the inner
query and uses this result to display all the
ITE 014 | INFORMATION MANAGEMENT
employees who earn more than this Multiple-column subqueries return more
amount. than one column from the inner SELECT
statement.
Guidelines for Using Subqueries
Single-Row Subqueries
Enclose subqueries in parentheses.
Return only one row
Place subqueries on the right side of the
comparison condition. Use single-row comparison operators
The ORDER BY clause in the subquery is not
needed unless performing top-N analysis.
Use single-row operators with single-row
subqueries and use multiple-row operators
with multiple-row subqueries.
With Oracle8i and later releases, an ORDER
BY clause can be used and is required in the
subquery to perform top-N analysis.
Before Oracle8i, subqueries could not
contain an ORDER BY clause. Only one
ORDER BY clause could be used for a SELECT
statement, and it had to be the last clause in Example
the main SELECT statement.
Display the employees whose job ID is the same as
that of employee 141:
Types of Subqueries
Single-row subqueries return only one row
from the inner SELECT statement.
Multiple-row subqueries return more than
Executing Single-Row Subqueries
one row from the inner SELECT statement.
A SELECT statement can be considered as a query
block.
The example below displays employees:
Whose job ID is the same as that of
employee 141
ITE 014 | INFORMATION MANAGEMENT
Whose salary is greater than that of SELECT job_id, AVG(salary)
employee 143
FROM employees
The inner query blocks are executed first,
GROUP BY job_id
producing the results (ST_CLERK and 2600).
HAVING AVG(salary) = (SELECT MIN(AVG(salary))
The outer query block is then processed and
FROM employees GROUP BY job_id);
uses the values returned by the inner
queries to complete its search conditions.
Since both inner queries return single Errors with Subqueries
values, this SQL statement is called a single-
Common Errors
row subquery.
One common error with subqueries occurs when
The outer and inner queries can get data from
more than one row is returned for a single-row
different tables.
subquery.
Fix: Change the = operator to IN.
Using Group Functions in a Subquery
Another issue occurs when no rows are returned by
You can display data from a main query by using a the inner query.
group function in a subquery to return a single row.
If the inner query returns no rows, the outer
Example query receives NULL, leading to no results.
The query below displays the employee last name,
job ID, and salary of all employees whose salary is
Multiple-Row Subqueries
equal to the minimum salary:
Return more than one row
sql
Use multiple-row comparison operators
Copy code
Example
SELECT last_name, job_id, salary
Find employees who earn the same salary as the
FROM employees
minimum salary for each department:
WHERE salary = (SELECT MIN(salary) FROM
sql
employees);
Copy code
The HAVING Clause with Subqueries SELECT last_name, salary, department_id
The Oracle server executes subqueries first. FROM employees
The Oracle server returns results into the WHERE salary IN (SELECT MIN(salary) FROM
HAVING clause of the main query. employees GROUP BY department_id);
Example
Using the ANY Operator in Multiple-Row
Find the job with the lowest average salary:
Subqueries
sql
The ANY operator compares a value to each
Copy code value returned by a subquery.
ITE 014 | INFORMATION MANAGEMENT
Operator Usage
<ANY means less than the maximum value.
>ANY means more than the minimum value.
=ANY is equivalent to IN.
Using the ALL Operator in Multiple-Row
Subqueries
The ALL operator compares a value to every
value returned by a subquery.
Operator Usage
>ALL means more than the maximum value.
<ALL means less than the minimum value.
The NOT operator can be used with IN, ANY,
and ALL operators.
This version maintains all the original information
while improving the layout for better readability.
Let me know if you need further refinements.