SQL
MySQL
database error
SQL troubleshooting
SQL query issues

1052 Column 'id' in field list is ambiguous

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

MySQL error 1052 Column 'id' in field list is ambiguous happens when a query references a column name that exists in more than one joined table and does not say which one it means. The database engine refuses to guess, so the query fails before execution completes. The fix is simple in principle: qualify the column with a table name or alias everywhere ambiguity is possible.

Why MySQL Calls the Column Ambiguous

Ambiguity appears most often in JOIN queries. If both tables contain an id column, then a clause such as SELECT id is unclear.

Problem example:

sql
SELECT id, name
FROM users
JOIN orders ON users.id = orders.user_id;

Both users and orders may have an id column, so MySQL cannot decide which id to return.

Correct version:

sql
SELECT users.id, users.name
FROM users
JOIN orders ON users.id = orders.user_id;

Or, more readably with aliases:

sql
SELECT u.id, u.name
FROM users AS u
JOIN orders AS o ON u.id = o.user_id;

Ambiguity Can Happen in More Than SELECT

Developers often notice the error in the field list, but the same root cause can affect other clauses.

Examples:

sql
1SELECT u.name, o.total
2FROM users AS u
3JOIN orders AS o ON u.id = o.user_id
4WHERE id = 10;

The WHERE clause is ambiguous. Fix it:

sql
1SELECT u.name, o.total
2FROM users AS u
3JOIN orders AS o ON u.id = o.user_id
4WHERE u.id = 10;

It can also appear in ORDER BY, GROUP BY, or even in UPDATE ... JOIN statements.

Use Aliases Consistently

Aliases make long queries easier to read, but only if you use them consistently in every clause.

sql
1SELECT
2  u.id AS user_id,
3  u.name,
4  o.id AS order_id,
5  o.created_at
6FROM users AS u
7JOIN orders AS o ON u.id = o.user_id
8ORDER BY o.created_at DESC;

This version does two useful things:

  • it qualifies every ambiguous column
  • it gives result columns explicit output names

That second point matters when the application layer consumes both IDs from the same result set.

SELECT * Makes This Worse

SELECT * does not always trigger error 1052, but it often creates downstream confusion because duplicate column names come back in the result.

Instead of:

sql
SELECT *
FROM users AS u
JOIN orders AS o ON u.id = o.user_id;

Prefer:

sql
1SELECT
2  u.id AS user_id,
3  u.email,
4  o.id AS order_id,
5  o.total
6FROM users AS u
7JOIN orders AS o ON u.id = o.user_id;

Explicit projections are easier to debug, easier to map in application code, and less likely to break when schemas evolve.

Example in Application Code

When queries are built in code, ambiguous columns can hide inside raw SQL strings.

python
1import mysql.connector
2
3conn = mysql.connector.connect(
4    host="127.0.0.1",
5    user="app",
6    password="secret",
7    database="shop"
8)
9
10query = """
11SELECT u.id AS user_id, u.name, o.id AS order_id, o.total
12FROM users AS u
13JOIN orders AS o ON u.id = o.user_id
14WHERE u.id = %s
15"""
16
17with conn.cursor() as cur:
18    cur.execute(query, (10,))
19    for row in cur.fetchall():
20        print(row)
21
22conn.close()

Being explicit in SQL reduces surprises in ORMs and raw database adapters alike.

Schema Awareness Helps Prevent the Error

The fastest way to avoid ambiguity is to know which commonly repeated column names exist across tables. Names like id, name, created_at, and status often repeat in many tables.

A practical team rule is:

  • always qualify columns in joined queries
  • always alias duplicate output columns
  • avoid relying on implicit resolution even when a query happens to work today

This makes queries more stable as schemas grow.

Common Pitfalls

  • Qualifying columns only in SELECT but not in WHERE or ORDER BY. Fix: qualify repeated column names in every clause.
  • Using SELECT * in joined queries. Fix: project only the columns you need and alias duplicates.
  • Assuming MySQL can infer the intended id. Fix: use table names or aliases explicitly.
  • Forgetting that UPDATE ... JOIN and DELETE ... JOIN can also be ambiguous. Fix: apply the same qualification rules in write queries.
  • Returning two different id columns without output aliases. Fix: rename them as user_id, order_id, and similar result names.

Summary

  • Error 1052 means MySQL found the same column name in more than one joined table.
  • The fix is to qualify ambiguous columns with table names or aliases.
  • Ambiguity can appear in SELECT, WHERE, ORDER BY, GROUP BY, and joined write queries.
  • Avoid SELECT * in joined queries when repeated column names are likely.
  • Explicit column qualification makes SQL easier to read, debug, and maintain.

Course illustration
Course illustration

All Rights Reserved.