Full answer
A scalar subquery promises one value. When the data breaks that promise, engines react very differently.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');The intent is "the manager of the Engineering team", but Engineering has three employees:
SELECT d.name,
(SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS someone
FROM departments d
WHERE d.id = 1;| name | someone |
|---|---|
| Engineering | Asha |
PostgreSQL stops with an error, which is the safe behaviour:
SELECT d.name,
(SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS someone
FROM departments d
WHERE d.id = 1;
-- error on purposeError: more than one row returned by a subquery used as an expressionSQLite quietly took one of the rows. Nothing in the result shows that the subquery was ambiguous, and a different index or insert order could pick a different employee.
Decide which single row you mean and write it:
SELECT d.name,
(SELECT e.name FROM employees e
WHERE e.dept_id = d.id
ORDER BY e.salary DESC, e.name
LIMIT 1) AS top_earner
FROM departments d
WHERE d.id = 1;| name | top_earner |
|---|---|
| Engineering | Asha |
Or use an aggregate when the question is really about a summary value, such as (SELECT MAX(salary) ...). If a subquery should logically return one row because of a business rule, back the rule with a unique constraint so the data cannot drift.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
SELECT d.name,
(SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS someone
FROM departments d
WHERE d.id = 1;
SELECT d.name,
(SELECT e.name FROM employees e
WHERE e.dept_id = d.id
ORDER BY e.salary DESC, e.name
LIMIT 1) AS top_earner
FROM departments d
WHERE d.id = 1;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- PostgreSQL errors when a scalar subquery returns several rows
- SQLite silently uses the first row
- Make single-row intent explicit with an aggregate, a key, or ORDER BY ... LIMIT 1
- Back 'there is only one' rules with constraints
Common mistakes
- Testing on SQLite and missing a multi-row subquery bug
- Adding LIMIT 1 without ORDER BY, which still picks an arbitrary row
Follow-ups
- How would you write this with a join instead?
- When is a subquery in the SELECT list a good idea?
Tips
- Mention that the SQLite behaviour hides data problems