You join two tables to see which student takes which course. Both queries worked on their own, but together SQLite refuses:
ambiguous column name: idambiguous column name: id means your query mentions a column called id, and more than one of the tables in the query has a column with that name. SQLite can't tell which one you mean, so it stops instead of guessing. It almost always appears after you add a JOIN, because related tables often share column names like id, name or created_at. The fix is to say which table each column comes from: students.id instead of id. This article shows where the error hides (not only in SELECT) and the clean way to write joins so it never comes back.
Try it yourself
Paste this into DB Browser for SQLite, or any SQLite tool:
CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE enrollments (id INTEGER PRIMARY KEY, student_id INTEGER, course TEXT);
INSERT INTO students VALUES (1, 'Alex'), (2, 'Sam'), (3, 'Mia');
INSERT INTO enrollments VALUES (1, 1, 'SQL'), (2, 1, 'Python'), (3, 2, 'Web');Then run:
SELECT id, name, course
FROM students
JOIN enrollments ON students.id = enrollments.student_id;You'll get the error above. Both students and enrollments have an id column, and SELECT id doesn't say which.
What the error means
When a query uses one table, every column name is clear. When it joins two tables, SQL combines their columns into one wide row. If both tables have an id, the combined row has two columns called id. You can see this with SELECT *:
SELECT * FROM students s JOIN enrollments e ON s.id = e.student_id;id name id student_id course
-- ---- -- ---------- ------
1 Alex 1 1 SQL
1 Alex 2 1 Python
2 Sam 3 2 WebTwo id columns: the first is the student's id, the second is the enrollment's id. A bare id could mean either. Ambiguous means exactly that: it has more than one possible meaning.
Read the message: ambiguous column name is the problem, and : id is the column. So ask: which table did I mean this id to come from?
The fix: qualify the column with its table
Write the table name and a dot before the column:
SELECT students.id, name, course
FROM students
JOIN enrollments ON students.id = enrollments.student_id;id name course
-- ---- ------
1 Alex SQL
1 Alex Python
2 Sam Webname and course exist in only one table each, so they're not ambiguous and work without a prefix. Only shared names need one. This is called qualifying a column.
Better: use short table aliases
Long table names make queries hard to read. Give each table a short alias right after its name, and use the alias everywhere:
SELECT s.id, s.name, e.course
FROM students s
JOIN enrollments e ON s.id = e.student_id;The result is the same. s stands for students and e for enrollments. Many teams qualify every column in a join, even the unambiguous ones. It costs a few characters, and it means nobody, including you in a month, has to look up which table course comes from. It also protects you from the future: if someone later adds a name column to enrollments, every unqualified name breaks.
It's not only in SELECT
This is the part that confuses people: you fixed the SELECT, and the error is still there. A bare column name is ambiguous anywhere in the query. In our tests, all of these failed with ambiguous column name: id:
-- in WHERE
SELECT name, course FROM students s JOIN enrollments e ON s.id = e.student_id WHERE id = 1;
-- in ORDER BY
SELECT name, course FROM students s JOIN enrollments e ON s.id = e.student_id ORDER BY id;
-- in GROUP BY
SELECT s.name, COUNT(*) AS courses FROM students s JOIN enrollments e ON s.id = e.student_id GROUP BY id;
-- in the ON condition itself
SELECT s.name, e.course FROM students s JOIN enrollments e ON id = e.student_id;The fix is the same in every clause: s.id or e.id. When the error persists, search the whole query for the column name from the message, and check each place.
Joining a table to itself
A self-join, joining a table to itself, makes every column ambiguous, because both "sides" have exactly the same columns:
SELECT s.name, s2.name AS other
FROM students s
JOIN students s2 ON s.id < s2.id
WHERE name = 'Alex';ambiguous column name: nameHere aliases aren't optional: they're the only way to tell the two copies apart. Write WHERE s.name = 'Alex'.
Errors after adding aliases
Once you give a table an alias, use the alias. The full table name no longer works in that query:
SELECT students.name FROM students s;no such column: students.namePostgreSQL explains this one well:
ERROR: invalid reference to FROM-clause entry for table "students"
LINE 1: SELECT students.name FROM students s;
^
HINT: Perhaps you meant to reference the table alias "s".And an alias that doesn't exist in the query gives no such column: c.course in SQLite. For more on that message, see SQLite: no such column.
The same error in PostgreSQL and MySQL
PostgreSQL calls it a "column reference" and points to the spot:
ERROR: column reference "id" is ambiguous
LINE 1: SELECT id, name, course FROM students JOIN enrollments ON st...
^MySQL says Column 'id' in field list is ambiguous (with in where clause or in order clause when it's elsewhere). The fix is the same in all three: qualify the column.
USING: one shared column
When the two tables are joined on a column that has the same name in both, USING joins them and merges that column into one:
CREATE TABLE courses (id INTEGER PRIMARY KEY, title TEXT);
INSERT INTO courses VALUES (1, 'SQL');
SELECT id, name, title FROM students JOIN courses USING (id);id name title
-- ---- -----
1 Alex SQLIn our SQLite test, a bare id worked here, because USING makes it one column. This only fits when the shared column really means the same thing in both tables. Joining students and courses on their own ids, as in this tiny example, wouldn't make sense in a real database; it only shows the syntax. For the usual case, student_id in one table and id in the other, use ON with aliases.
Errors that look alike
no such column: x: the column, or the alias before it, doesn't exist. See SQLite: no such column.misuse of aggregateormust appear in the GROUP BY clause: a column problem in grouped queries. See GROUP BY errors.- Duplicate rows with no error: a join on the wrong column can multiply rows silently. Count the rows before and after a join, as our SQL for data analysis roadmap recommends.
Wrong fixes that don't help
- Renaming the columns in the tables to
student_id_pkand similar. It works, but naming every table's keyidis normal; aliases are the standard answer. - Removing the column from the query. You'll need it again in the next query.
- Using
SELECT *to avoid naming columns. You get twoidcolumns, and a program reading the result by name may pick the wrong one.
A debugging walkthrough
- Read the column name after
ambiguous column name:. - List the tables in
FROMand everyJOIN. Which of them have that column? - Decide which table you mean in each place: the student's id or the enrollment's id?
- Search the whole query for the bare name:
SELECT,ON,WHERE,GROUP BY,ORDER BY. - Give every table an alias and prefix every column with it.
Practice
With the tables above, which of these run?
SELECT name, course FROM students JOIN enrollments ON students.id = enrollments.student_id;SELECT s.name FROM students s JOIN enrollments e ON s.id = e.student_id WHERE id > 1;SELECT s.name, e.course FROM students s LEFT JOIN enrollments e ON s.id = e.student_id;
Answers. (1) Runs: name and course each exist in one table only. (2) ambiguous column name: id, from the WHERE. (3) Runs, and also lists Mia with NULL, because LEFT JOIN keeps students without enrollments.
Frequently asked questions
What does "ambiguous column name" mean in SQL?
A column name in your query exists in more than one of the joined tables, and SQL can't tell which you mean.
How do I fix ambiguous column name: id?
Prefix it with its table or alias, for example students.id or s.id, everywhere it appears in the query.
Why do I still get the error after fixing the SELECT?
The bare name also appears in ON, WHERE, GROUP BY or ORDER BY. Fix it there too.
Should I always use table aliases?
In any query with a join, it's a good habit: short aliases and every column prefixed.
What's the PostgreSQL version of this error?
column reference "id" is ambiguous. Same cause, same fix.
A quick checklist
- Read the column name in the message.
- Find which joined tables have that column.
- Give each table a short alias.
- Prefix the column, and ideally every column, with its alias.
- Check every clause, not only
SELECT.
For a method that works on any error, read how to read an error message. The SQLite documentation describes the full SELECT syntax, including joins and aliases.
