sqlstreak

SQL error

column reference "id" is ambiguous / ambiguous column name: id

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

Short answer

After a join, more than one table has a column with that name, so the database doesn't know which one you mean. Prefix the column with its table or alias: o.id, c.id.

The exact messages

  • PostgreSQLERROR: column reference "id" is ambiguous
  • SQLiteambiguous column name: id

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:  column reference "id" is ambiguous
LINE 1: SELECT id, name
               ^

SQLite 3.54.1 reports the same mistake as ambiguous column name: id, with no line number: look for the word or column it names.

Why it happens

Two joined tables share a column name

id, name and created_at are the usual suspects: almost every table has them.

It can appear anywhere

SELECT, WHERE, GROUP BY, ORDER BY and ON can all trigger it. Qualify the column in every place it appears.

Reproduce it, then fix it

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

Broken
SELECT id, name
FROM orders o
JOIN customers c ON c.id = o.customer_id;
Fixed
SELECT o.id, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id;

Give every table a short alias and prefix every column in multi-table queries. It also makes the query easier to read.

The fix returns, PostgreSQL: first 6 of 17 rows

idname
1Maya Ortiz
2Jonah Reed
3Maya Ortiz
4Priya Nair
5Tomás Herrera
6Ellie Park

The fix returns, SQLite: first 6 of 17 rows

idname
1Maya Ortiz
2Jonah Reed
3Maya Ortiz
4Priya Nair
5Tomás Herrera
6Ellie Park

Experiment with it: PostgreSQL playground·SQLite playground

Official documentation

Stop hitting this error

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