Sub topic 10
Multi-Column Subqueries
This chapter assumes that you know SELECT, WHERE, GROUP BY, aggregate functions, and joins. You should also be familiar with the Oracle HR sample schema, especially EMPLOYEES and DEPARTMENTS.
A multi-column subquery returns more than one column from each result row. The outer query compares those columns as a group. This is also called a row subquery or a tuple comparison.
Comparing Columns as a Group
Suppose you need the highest-paid employee in each department. A subquery can return each department together with its maximum salary:
SELECT last_name, department_id, salary
FROM employees
WHERE (department_id, salary) IN (
SELECT department_id, MAX(salary)
FROM employees
GROUP BY department_id
);The outer expression (department_id, salary) is compared with each two-column row returned by the subquery. An employee matches only when both values match:
- The employee belongs to the department.
- The employee has that department's maximum salary.
If two employees tie for the highest salary in one department, both rows are returned.
The number and order of columns must match. In this example, the first outer column, department_id, matches the first subquery column, and salary matches the second.
| Outer expression | Subquery expression | Meaning |
|---|---|---|
| `(department_id, salary)` | `(department_id, MAX(salary))` | Both values must match |
A multi-column comparison is different from two independent conditions joined with OR. The pair is treated as one value.
`IN` and Single-Row Comparisons
Use multi-column IN when the subquery can return multiple rows. Use = only when the subquery must return exactly one row:
SELECT last_name, salary
FROM employees
WHERE (department_id, salary) = (
SELECT department_id, salary
FROM employees
WHERE employee_id = 100
);The subquery returns one department and one salary for employee 100. If it returns multiple rows, Oracle raises ORA-01427: single-row subquery returns more than one row.
The expressions must also have compatible data types. For example, comparing a department number with a salary number might be syntactically valid but logically incorrect.
NULL Values
A comparison involving NULL can produce UNKNOWN rather than TRUE. A row with a null value might therefore fail to match, even when the other column values are equal.
For reliable matching, filter nulls explicitly when null values are possible:
SELECT employee_id, last_name, department_id, salary
FROM employees
WHERE department_id IS NOT NULL
AND salary IS NOT NULL
AND (department_id, salary) IN (
SELECT department_id, MAX(salary)
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
);A multi-column NOT IN condition deserves extra care. If any returned pair contains a NULL, Oracle's three-valued logic can make the condition UNKNOWN and produce fewer rows than expected. For exclusion tests, NOT EXISTS with matching column conditions is often clearer.
Exercise
How would you return every employee whose (department_id, salary) pair matches a department's minimum salary?
Show answer
Use (department_id, salary) IN (SELECT department_id, MIN(salary) FROM employees GROUP BY department_id). This returns all employees tied at the minimum.