SQLSTATE 21000Severity lowLab verified

Incident brief

More than one row returned by a subquery used as an expression

A scalar subquery matched more than one row. PostgreSQL only allows a scalar subquery to return exactly one row, so it rejects the query.

Reproduced on PostgreSQL 16.14Verified 2026-07-16 (Docker lab, PostgreSQL 16.14)Reviewed by Verified against PostgreSQL 16.14 in an isolated lab environment

In 10 seconds

What
More than one row returned by a subquery used as an expression
What triggers it
Create a table where more than one row can share the same lookup value.
The fix
Add a WHERE condition that narrows the subquery down to a single row, if one uniquely identifies the row you want.
Proof
Reproduced on PostgreSQL 16.14 → A scalar subquery that matched 2 rows (dept = 'eng') was rejected with SQLSTATE 21000. The underlying table data was never touched.

The fix

What to do right now

The immediate, application-level response to this error.

  • Add a WHERE condition that narrows the subquery down to a single row, if one uniquely identifies the row you want.
  • Otherwise, wrap the subquery in an aggregate (MIN, MAX, etc.) to guarantee exactly one value comes back.
  • Avoid using LIMIT 1 alone as the fix — see the premium counterexample below for why.
Fix — SQL
-- an aggregate guarantees exactly one value comes back, no matter how many rows match
SELECT (SELECT MIN(id) FROM hr.employees WHERE dept = 'eng') AS one_id;

Related & next steps

Follow the thread

Everything this error touches — jump straight to the sibling error, term, runbook, or parameter.

Verification

PG 16.14
Last verified
2026-07-16 (Docker lab, PostgreSQL 16.14)
Reviewed by
Verified against PostgreSQL 16.14 in an isolated lab environment
Audit status
reviewed