Sub topic 1
What a Subquery Is
A subquery is a query nested inside another SQL statement. The inner query produces a value or set of rows that the outer query uses. Subqueries let you express a question in steps without creating a temporary table.
This chapter assumes you know basic SELECT, WHERE, ORDER BY, and GROUP BY, joins, aggregate functions such as AVG and COUNT, and the Oracle HR sample schema.
Prerequisites
You should be familiar with these tables:
employees, which contains employee information such asemployee_id,last_name,salary, anddepartment_iddepartments, which containsdepartment_idanddepartment_name
You should also understand that a query can return one row, many rows, or no rows. The number and shape of rows returned by a subquery determine how the outer query can use it.
The basic structure
A subquery appears inside parentheses. The outer query uses its result:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);The inner query calculates the average salary for the whole employees table. It returns one value. The outer query then returns employees whose salary is greater than that value.
You can read the statement in two logical steps:
- Calculate the average salary.
- Find employees whose salary exceeds the calculated average.
Oracle's optimizer may execute these steps differently internally. The important point is the relationship between the two queries and the result the inner query must provide.
How the result shape matters
A subquery can return different kinds of results:
| Subquery result | Typical use | Example operator |
|---|---|---|
| One value | Compare one column with one value | `=`, `>`, `<` |
| One column with many rows | Test membership | `IN` |
| Several columns and rows | Compare row combinations | `(col1, col2) IN` |
A comparison such as salary > (subquery) requires the subquery to return at most one value. If it returns multiple rows, Oracle raises ORA-01427: single-row subquery returns more than one row.
For example, this query is valid because MAX returns one value:
SELECT last_name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);The subquery returns the highest salary. The outer query finds every employee with that salary, so ties are included.
Subqueries and table aliases
Aliases make relationships clear, especially when the same table appears in both queries:
SELECT e.last_name, e.salary
FROM employees e
WHERE e.salary > (
SELECT AVG(i.salary)
FROM employees i
);Here, e identifies rows in the outer query and i identifies rows in the inner query. At this point, the inner query is independent because it does not refer to e. A subquery that refers to an outer query column is called a correlated subquery. Correlated subqueries are covered in a later chapter.
Visual model
Exercise
What error should you expect if a subquery used with = returns two rows?
Show answer
Oracle raises ORA-01427 because = requires a single value, not multiple rows.