Brainlag
  1. Library
  2. Debugging
  3. SQL

ambiguous column nameWhat is "ambiguous column name"?

SQLite stops with "ambiguous column name" when two joined tables share a column name and the query does not say which one it means.

SQL5 min

What SQLite is telling you

Output
ambiguous column name: name

The query joins two tables that both have a column called name. In the shop data, customers.name is a person and products.name is a thing. When the query just says name, SQLite cannot know which one you mean, and it refuses to guess.

It happens most with id and name, because nearly every table has them. It only shows up once you join: the same query on one table works fine.

How to find it

The message names the column. Find every place the query uses it bare: in SELECT, WHERE, ORDER BY, GROUP BY and ON. Put the table (or its short alias) in front of each one: c.name, p.name. Even where only one table has the column, writing the prefix makes the query easier to read. If two prefixed columns would both come out called name, rename them with AS so the result is clear.

Run it: two tables with an id

Press Run and read the error from the bottom up.

Tables customers, products, orders, order_items
customers 8 rows, 3 columns
idnameyear_group
1Amara12
2Ben13
3Chloe12
4Dev12
5Ella13
6Femi13
7Grace10
8Hugo10
products 12 rows, 5 columns
idnamecategorypricestock
1Sketchbook A4art4.530
2Acrylic paintsart8.012
3Brush setart5.2520
4Finelinersart3.7545
5Graph papermaths2.060
6Scientific calculatormaths12.998
7Protractormaths0.9970
8Lab gogglesscience6.515
9Periodic table posterscience3.025
10Hoodiemerch24.010
11Water bottlemerch9.518
12Lanyardmerch2.540
orders 41 rows, 4 columns
idcustomer_idplaced_onstatus
112026-09-16paid
212026-09-03paid
312026-09-21paid
412026-09-03paid
512026-09-20paid
612026-09-27paid
712026-09-16paid
812026-09-09paid
912026-09-08paid
1022026-09-28paid
1122026-09-30refunded
1222026-09-13paid

First 12 of 41 rows. Query the table to see the rest.

order_items 53 rows, 4 columns
idorder_idproduct_idqty
1122
22102
33101
4411
5582
6691
77111
8891
98122
10911
119101
121071

First 12 of 53 rows. Query the table to see the rest.

query.sql to run
SELECT id, name, price, qty
FROM products
JOIN order_items ON order_items.product_id = products.id
WHERE category = 'art';

Who bought the art supplies

The art teacher wants to know who bought art products: the customer's name as customer, the product's name as product, and the qty. It stops with "ambiguous column name". Fix it.

exercise_1.sql to run
SELECT name AS customer, name AS product, qty
FROM order_items AS oi
JOIN orders AS o ON o.id = oi.order_id
JOIN customers AS c ON c.id = o.customer_id
JOIN products AS p ON p.id = oi.product_id
WHERE category = 'art';

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.