sqlstreak

SQL error

subquery has too many columns / sub-select returns 2 columns - expected 1

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

A subquery used with IN, = or as a value must return exactly one column. Select only the column you compare against: WHERE id IN (SELECT item_id FROM order_items).

The exact messages

  • PostgreSQLERROR: subquery has too many columns
  • SQLitesub-select returns 2 columns - expected 1

How to read this message

This is what PostgreSQL 18.6 prints for the broken query below, the way psql shows it. LINE gives the line it was reading when it gave up, and the ^ marks the exact spot.

ERROR:  subquery has too many columns
LINE 2: WHERE id IN (SELECT item_id, quantity FROM order_items);
                 ^

SQLite 3.54.1 reports the same mistake as sub-select returns 2 columns - expected 1, with no line number: look for the word or column it names.

Why it happens

An extra column in an IN subquery

WHERE id IN (SELECT item_id, quantity …) compares one value with two columns. Keep only item_id.

SELECT * inside the subquery

IN (SELECT * FROM …) returns every column. Name the one you need.

A scalar subquery with two columns

A subquery in SELECT must produce one value. Need two? Use a JOIN, or two subqueries.

Reproduce it, then fix it

Both queries run on a real database in your browser. Switch dialects to compare.

Broken
SELECT name FROM menu_items
WHERE id IN (SELECT item_id, quantity FROM order_items);
Fixed
SELECT name FROM menu_items
WHERE id IN (SELECT item_id FROM order_items);

One column on each side of IN: the item id.

The fix returns, PostgreSQL: first 6 of 11 rows

name
Margherita
Pepperoni Rocket
Four Cheese Orbit
BBQ Meteor
Veggie Nebula
Garlic Knots

The fix returns, SQLite: first 6 of 11 rows

name
Margherita
Pepperoni Rocket
Four Cheese Orbit
BBQ Meteor
Veggie Nebula
Garlic Knots

Experiment with it: PostgreSQL playground·SQLite playground

Other forms of this mistake

Each one was run when this page was built; the result is what the engine really does.

  • SELECT name, (SELECT id, name FROM staff LIMIT 1) AS boss FROM customers;

    PostgreSQLERROR: subquery must return only one column

    The same mistake in a scalar subquery gets a different PostgreSQL message (SQLite's stays the same).

Official documentation

Stop hitting this error

Practise the concept behind it in a five-minute lesson, or read the full guide: SQL subqueries.