Skip to content
Advertisement

Problem with Ambiguous Column Name in SQL script

I am new to SQL and using this online compiler.

https://www.programiz.com/sql/online-compiler/

I have the following script and I am getting the error: Error: ambiguous column name: Customers.customer_id

UPDATE 
    Customers
SET 
    last_name = 'cow'
FROM 
    Customers 
    INNER JOIN Orders ON Customers.customer_id = Orders.customer_id
WHERE 
    Orders.item = 'Keyboard'

There is a customer_id column in multiple tables, but I am specifying which table to pull each column from. Why is it still saying it is ambiguous?

Advertisement

Answer

An UPDATE statement with a join requires an alias for the table being updated. In that case, the following will work as expected. You can also alias the Orders table if desired.

UPDATE C
    SET last_name = 'cow'
FROM
    Customers C
    INNER JOIN Orders
        ON C.customer_id = Orders.customer_id
WHERE Orders.item = 'Keyboard';
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement