Fix “Ambiguous Column Reference” in a SQL JOIN
Both joined tables have that column name, so the database refuses to guess.
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.
Why it happens
After a JOIN, the result has columns from every table. When two of them have the same name, an unqualified reference inSELECT, WHERE, ORDER BY, orGROUP BY is genuinely ambiguous, and SQL treats that as an error rather than silently picking one.
The fix
Qualify the column with its table (or, better, a short alias) so there's exactly one thing it can refer to. Aliasing every table and qualifying every column is a good habit in any multi-table query.
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 |