Skip to content

Ambiguous Column Reference

Error message

                column reference is ambiguous
              

Quick summary

You referenced a column name that exists in more than one table in the query, without specifying which table.

Why this happens

  • Two joined tables have a column with the same name (e.g., both have id)

  • Using an unqualified column name in a multi-table query

  • Aliasing a column to a name that conflicts with another column

Minimal example

✗ Broken code
SELECT id, name FROM users
INNER JOIN orders ON orders.user_id = users.id;
-- 'id' exists in both users and orders → ambiguous
✓ Fixed code
SELECT users.id AS user_id, orders.id AS order_id, users.name
FROM users
INNER JOIN orders ON orders.user_id = users.id;

Qualifying id with the table name and adding aliases makes it clear which id is meant.

How to diagnose

  • Identify which tables in the query have a column with the same name

  • Check if the unqualified column name appears in multiple tables

How to fix

  • Qualify the column with the table name or alias: table.column or alias.column

  • Use a column alias to disambiguate: SELECT a.id AS user_id

  • Rename the column in one table if the conflict is a design issue

How to prevent

  • Always qualify column names with table aliases in multi-table queries

  • Use consistent table aliases in queries

  • Avoid naming every primary key id — use user_id, order_id, etc. in joins

Related Resources

Related Glossary

Related Lessons

  • Joins

    Learn to avoid ambiguity in joins.

← Back to language