Sub topic 2
Single-Row Subqueries
This chapter assumes that you know basic SELECT, WHERE, ORDER BY, and GROUP BY, can join tables, and can use aggregate functions such as AVG, COUNT, and MAX. The examples use Oracle's HR sample schema, especially employees.
What a Single-Row Subquery Is
A subquery is a query inside another SQL statement. A single-row subquery returns at most one row and usually one column. The outer query uses that value in a comparison.
For example, this query finds employees whose salary is above the company-wide average:
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary)
FROM employees);The inner query calculates one average salary. The outer query compares each employee's salary with that result.
The parentheses make the boundary between the outer query and the subquery clear:
outer query: employees whose salary is greater than ...
└─ inner query: average salaryComparison Operators
Use a single-row operator when the subquery returns one value.
| Operator | Meaning |
|---|---|
| `=` | Equal to the returned value |
| `<>` | Not equal to the returned value |
| `>` | Greater than the returned value |
| `<` | Less than the returned value |
| `>=` | Greater than or equal to the returned value |
| `<=` | Less than or equal to the returned value |
This example finds employees who earn the same as the highest salary:
SELECT first_name, last_name, salary
FROM employees
WHERE salary = (SELECT MAX(salary)
FROM employees);An aggregate function without GROUP BY, such as MAX or AVG, normally returns one row. A nonaggregate subquery must be restricted so that it also returns no more than one row:
SELECT first_name, last_name, salary
FROM employees
WHERE salary = (SELECT salary
FROM employees
WHERE employee_id = 100);employee_id is unique, so this subquery returns one salary.
Errors and Missing Values
If a subquery used with = or > returns more than one row, Oracle raises ORA-01427: single-row subquery returns more than one row.
For example, this is unsafe because several employees may work in department 60:
SELECT first_name, last_name
FROM employees
WHERE salary > (SELECT salary
FROM employees
WHERE department_id = 60);Use IN, ANY, or ALL when the inner query is expected to return multiple rows. Alternatively, use an aggregate such as MAX(salary) if the business question requires one value.
If the subquery returns no rows, Oracle treats the result as NULL. Comparisons with NULL are not true, so the outer query usually returns no matching row.
Choosing a Safe Single-Row Query
Ask these questions:
- Does a key or unique condition limit the subquery to one row?
- Should an aggregate reduce several rows to one value?
- Could the subquery return no rows or
NULL? - Would
IN,ANY, orALLbetter describe a multi-row result?
Exercise
Write a query that returns employees whose salary is less than the average salary of all employees.
Show answer
SELECT firstname, lastname, salary FROM employees WHERE salary < (SELECT AVG(salary) FROM employees);