T-SQL reference › UPDATE with JOIN

UPDATE … FROM JOIN in SQL Server

SQL Server lets you UPDATE from a JOIN. That is handy and dangerous. Write the SELECT first. When the grid is right, change SELECT to UPDATE.

Preview, then write

-- Look first
SELECT c.customer_id, c.phone, i.new_phone
FROM Customers c
JOIN ImportPhones i ON i.customer_id = c.customer_id;

-- Then, and only then
-- UPDATE c SET phone = i.new_phone
-- FROM Customers c
-- JOIN ImportPhones i ON i.customer_id = c.customer_id;
No WHERE and no JOIN

UPDATE Customers SET phone = NULL is everyone. Check @@ROWCOUNT after. WorkSQL will not run this. Your live server will.

Try it on Harbour & Co

Kwame Asante (Data Analyst) needs this now: “My top-10 spenders query gives numbers way higher than finance’s. Can you see why?” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets