Sub topic 6
Scalar Subqueries in the SELECT List
This chapter assumes you know basic SELECT, WHERE, GROUP BY, joins, and aggregate functions such as AVG and COUNT. You should also recognize the EMPLOYEES and DEPARTMENTS tables in Oracle's HR sample schema.
A scalar subquery is a subquery that returns one column and at most one row. In the SELECT list, Oracle treats its result like a single value. You can use that value as a selected column, an expression, or an argument to a function.
SELECT first_name,
last_name,
salary,
(SELECT AVG(salary) FROM employees) AS company_avg_salary
FROM employees;The inner query calculates one company-wide average. Oracle repeats that value beside each employee. The subquery is uncorrelated because it does not refer to the outer query.
| Scalar subquery result | Value returned to the outer query |
|---|---|
| One row with one value | That value |
| No rows | `NULL` |
| More than one row | Error `ORA-01427` |
A scalar subquery must return only one column. It must also return no more than one row. An aggregate such as AVG or COUNT usually meets the row requirement because it returns one result for the whole input, even when the input has no matching rows.
A correlated scalar subquery
A correlated subquery refers to a column from the current row of the outer query. The following query counts employees in each department:
SELECT d.department_name,
(SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.department_id) AS headcount
FROM departments d;The alias d belongs to the outer query. The inner query uses d.department_id, so Oracle evaluates the count for the department currently being displayed.
This is useful when the derived value belongs naturally beside each outer row. A department with no employees still appears because the outer query reads from DEPARTMENTS; its scalar subquery returns 0 because COUNT(*) counts zero matching rows.
Avoiding ORA-01427
This query is invalid if a department has several employees:
SELECT d.department_name,
(SELECT e.last_name
FROM employees e
WHERE e.department_id = d.department_id) AS employee_name
FROM departments d;The subquery selects one column, but it can return many rows. Oracle raises ORA-01427: single-row subquery returns more than one row.
Make the result single-row by aggregating it:
SELECT d.department_name,
(SELECT MAX(e.salary)
FROM employees e
WHERE e.department_id = d.department_id) AS highest_salary
FROM departments d;Or select a deliberately limited row when your Oracle version supports row limiting:
SELECT d.department_name,
(SELECT e.last_name
FROM employees e
WHERE e.department_id = d.department_id
ORDER BY e.salary DESC
FETCH FIRST 1 ROW ONLY) AS highest_paid_employee
FROM departments d;Use an aggregate when you want a summary. Use row limiting when you want one selected row and have defined which row should win.
Exercise
What error occurs if a scalar subquery in the SELECT list returns two employee last names for one department, and how can you prevent it?
Show answer
Oracle raises ORA-01427. Use an aggregate such as MAX, or use ORDER BY with FETCH FIRST 1 ROW ONLY when one specific row is required.