“Column Reference Is Ambiguous” — Fix a SQL JOIN Error
Both joined tables have that column name, so the database refuses to guess.
- DuckDB
-
Binder Error: Ambiguous reference to column name "customer_id" (use: "customers.customer_id" or "orders.customer_id") - PostgreSQL
-
column reference "customer_id" is ambiguous - SQL Server
-
Ambiguous column name 'customer_id'. - MySQL
-
Column 'customer_id' in field list is ambiguous - SQLite
-
ambiguous column name: customer_id
The symptom
You join two tables and the query fails with an error like "column reference 'customer_id' is ambiguous" or "ambiguous column name". Both tables have a column with that name, so the database refuses to guess which one you meant.
The quick fix
Prefix the ambiguous column with its table name or alias. Change an
unqualified reference such as customer_id to
c.customer_id or o.customer_id, depending on which
table you mean.
select customer_id, order_date
from customers
join orders on orders.customer_id = customers.customer_id
Running it raises
Ambiguous reference to column name.
select c.customer_id,
c.first_name,
o.order_date
from customers as c
join orders as o on o.customer_id = c.customer_id
order by o.order_date Why it happens
After a JOIN, the result has columns from every table. When two
of them have the same name, an unqualified reference in SELECT,
WHERE, ORDER BY, or GROUP BY is
genuinely ambiguous, and SQL treats that as an error rather than silently
picking one. Aliasing every table and qualifying shared columns prevents the
error from returning as the query grows.
A column that lives in only one table does not strictly need qualifying, but adding an alias to a query and forgetting to qualify a now-ambiguous column is a common way to introduce this error later. Qualify shared columns from the start.
How other engines word it
| Engine | Message |
|---|---|
| DuckDB | Binder Error: Ambiguous reference to column name "customer_id" (use: "customers.customer_id" or "orders.customer_id") |
| PostgreSQL | column reference "customer_id" is ambiguous |
| SQL Server | Ambiguous column name 'customer_id'. |
| MySQL | Column 'customer_id' in field list is ambiguous |
| SQLite | ambiguous column name: customer_id |
Related errors
- Fix “Referenced Column Not Found” in SQL
No table in the FROM clause has a column by that name. Usually a typo or a table you did not join.
- Fix “Column Not Found” When Filtering on a SQL Alias
In logical order, WHERE comes before SELECT assigns aliases, so many engines reject the alias there.
- Fix “Column Must Appear in the GROUP BY Clause” in SQL
A selected column is neither grouped nor aggregated, so this engine cannot derive one value for the group.
Learn more
Related
Join two related tables on a key.
INNER, LEFT, RIGHT, FULL, and CROSS joins.
Join types and common join patterns.
Practice join keys, left joins, duplicate rows, and debugging.
One-to-many joins can inflate totals.
Join through a bridge table.