PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL error 21000: more than one row returned by a subquery used as an expression

A subquery used as a single value (after =, in the SELECT list, in SET) returned two or more rows.

PostgreSQL 17.12
21000: more than one row returned by a subquery used as an expression

Jump to a cause

  1. 01= (subquery) where the subquery returns several rows
  2. 02Scalar subquery in the SELECT list finds two rows for one key
  3. 03UPDATE ... SET col = (subquery) from a table with duplicate keys

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

A subquery in parentheses used as a value must return at most one row. Where it is used after =, as a column in the SELECT list, or in UPDATE ... SET col = (...), PostgreSQL raises 21000 as soon as it gets a second row.

The message does not say which rows. Run the subquery on its own (or count rows per key) to see where the extra rows come from, then decide: should it be a list (IN), a narrower condition, or one chosen row?

01= (subquery) where the subquery returns several rows

dept_id = (SELECT id FROM depts WHERE region = 'KR') expects one department, but Korea has two: Sales Seoul (10) and Sales Busan (20).

Setup
Setuprun as lab_a
CREATE TABLE depts (id int PRIMARY KEY, name text, region text);
CREATE TABLE emps (id int PRIMARY KEY, name text, dept_id int);
INSERT INTO depts VALUES (10, 'Sales Seoul', 'KR'), (20, 'Sales Busan', 'KR'), (30, 'Sales Tokyo', 'JP');
INSERT INTO emps VALUES (1, 'Kim', 10), (2, 'Park', 20), (3, 'Sato', 30);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 3
INSERT 0 3
Reproducerun as lab_a
SELECT name FROM emps WHERE dept_id = (SELECT id FROM depts WHERE region = 'KR');
✕ ERROR
ERROR:  21000: more than one row returned by a subquery used as an expression
Checkrun as lab_a
SELECT id, name FROM depts WHERE region = 'KR';
OUTPUT
 id |    name
----+-------------
 10 | Sales Seoul
 20 | Sales Busan
(2 rows)

When you want all of them, compare with a list: dept_id IN (SELECT ...). Kim and Park are returned.

After the fixrun as lab_a
SELECT name FROM emps WHERE dept_id IN (SELECT id FROM depts WHERE region = 'KR') ORDER BY name;
✓ SUCCESS
 name
------
 Kim
 Park
(2 rows)

02Scalar subquery in the SELECT list finds two rows for one key

The phone column is a subquery per customer. Customer 2 (Lee) has two contacts, so the whole query fails, not just Lee's row. The check finds customer_id 2 with count 2.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE contacts (customer_id int, phone text, is_primary boolean);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
INSERT INTO contacts VALUES (1, '010-1111-1111', true), (2, '010-2222-2222', true), (2, '02-333-3333', false);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 3
Reproducerun as lab_a
SELECT c.name, (SELECT t.phone FROM contacts t WHERE t.customer_id = c.id) AS phone FROM customers c;
✕ ERROR
ERROR:  21000: more than one row returned by a subquery used as an expression
Checkrun as lab_a
SELECT customer_id, count(*) FROM contacts GROUP BY customer_id HAVING count(*) > 1;
OUTPUT
 customer_id | count
-------------+-------
           2 |     2
(1 row)

Add the condition that makes the result unique. Here AND t.is_primary picks the main number, and each customer gets one phone.

After the fixrun as lab_a
SELECT c.name, (SELECT t.phone FROM contacts t WHERE t.customer_id = c.id AND t.is_primary) AS phone FROM customers c ORDER BY c.id;
✓ SUCCESS
 name |     phone
------+---------------
 Kim  | 010-1111-1111
 Lee  | 010-2222-2222
(2 rows)

03UPDATE ... SET col = (subquery) from a table with duplicate keys

The uploaded price list has B-2 twice (2100 and 2200, loaded on different days). The subquery for B-2 returns two rows, and the UPDATE fails without changing any product.

Setup
Setuprun as lab_a
CREATE TABLE products (sku text PRIMARY KEY, price int);
CREATE TABLE price_upload (sku text, price int, loaded_at timestamp);
INSERT INTO products VALUES ('A-1', 1000), ('B-2', 2000);
INSERT INTO price_upload VALUES ('A-1', 1100, '2026-11-01 09:00'), ('B-2', 2100, '2026-11-01 09:00'), ('B-2', 2200, '2026-11-02 09:00');
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 3
Reproducerun as lab_a
UPDATE products p SET price = (SELECT u.price FROM price_upload u WHERE u.sku = p.sku);
✕ ERROR
ERROR:  21000: more than one row returned by a subquery used as an expression
Checkrun as lab_a
SELECT sku, price, loaded_at FROM price_upload WHERE sku IN (SELECT sku FROM price_upload GROUP BY sku HAVING count(*) > 1) ORDER BY loaded_at;
OUTPUT
 sku | price |      loaded_at
-----+-------+---------------------
 B-2 |  2100 | 2026-11-01 09:00:00
 B-2 |  2200 | 2026-11-02 09:00:00
(2 rows)

Decide which row wins. Here the latest load is used: ORDER BY u.loaded_at DESC LIMIT 1. The UPDATE changes 2 rows, and B-2 gets 2200. Cleaning the duplicates in the source is also an option.

After the fixrun as lab_a
UPDATE products p SET price = (SELECT u.price FROM price_upload u WHERE u.sku = p.sku ORDER BY u.loaded_at DESC LIMIT 1);
SELECT sku, price FROM products ORDER BY sku;
✓ SUCCESS
UPDATE 2
 sku | price
-----+-------
 A-1 |  1100
 B-2 |  2200
(2 rows)

FAQ

How do I fix more than one row returned by a subquery used as an expression?

Run the subquery alone to see the extra rows. Use IN if you want all of them, add a condition that makes it unique, or pick one row with ORDER BY ... LIMIT 1.

Should I use = or IN with a subquery in PostgreSQL?

Use = only when the subquery can return at most one row. If it can return several, IN (SELECT ...) compares against all of them.

Why does UPDATE SET (SELECT ...) fail with 21000?

The source table has more than one row for some key. Find duplicates with GROUP BY key HAVING count(*) > 1, then choose one row per key.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12