← Back to SQL, page 5

sql-0094 · Medium mid

A scalar subquery accidentally returns two rows. What happens in SQLite and in PostgreSQL?

PostgreSQL raises 'more than one row returned by a subquery used as an expression'. SQLite does not complain: it silently uses the first row the subquery produces, which may change with the plan. Make the subquery return one row on purpose, with an aggregate, a unique key filter, or ORDER BY plus LIMIT 1.

Full answer

A scalar subquery promises one value. When the data breaks that promise, engines react very differently.

sql
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:

sql
SELECT d.name,
       (SELECT e.name FROM employees e WHERE e.dept_id = d.id) AS someone
FROM departments d
WHERE d.id = 1;
namesomeone
EngineeringAsha

PostgreSQL stops with an error, which is the safe behaviour:

sql
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 purpose
text
Error: more than one row returned by a subquery used as an expression

SQLite 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:

sql
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;
nametop_earner
EngineeringAsha

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.

sql
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