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.

What lands in your log

ERROR: more than one row returned by a subquery used as an expression

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

In 10 seconds

What triggers it
Create a table where more than one row can share the same lookup value.
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.

Fix

What to do right now

Application-level steps for 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;

Verification

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

Went further?

Pro unlocks the second lab proof

Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.

FollowSubstackLinkedInnew errors · lab notes · hiring loops