// dialect comparison

UPDATE with a join to another table

Set a column based on data in a different table: three clause orders and a subquery fallback.

Filed under Inserting & updating

The verdict

PostgreSQL's UPDATE … FROM is the cleanest design, and SQLite 3.33+ and Oracle 23ai copied it. SQL Server and MySQL support join updates too but disagree on clause order. The EXISTS or IN subquery is the only form that runs on all five.

Where they differ

This is where the dialects visibly diverge. SQL Server puts a FROM … JOIN after SET, MySQL puts the JOIN before SET, and PostgreSQL, SQLite 3.33+ and Oracle 23ai take a FROM clause with the join condition in WHERE. Older Oracle and SQLite need a correlated subquery, which runs everywhere.

How each engine writes it

SQL Server

UPDATE o
SET o.status = 'shipped'
FROM orders o
JOIN shipments s ON s.order_id = o.id
WHERE s.tracking IS NOT NULL;

The target appears twice: after UPDATE and again in FROM with its alias. Rows matched by several joined rows are updated once, non-deterministically.

PostgreSQL

UPDATE orders o
SET status = 'shipped'
FROM shipments s
WHERE s.order_id = o.id
  AND s.tracking IS NOT NULL;

FROM lists only the other tables; the join condition lives in WHERE. Like SQL Server, a many-match join updates once per target row, not per match.

MySQL

UPDATE orders o
JOIN shipments s ON s.order_id = o.id
SET o.status = 'shipped'
WHERE s.tracking IS NOT NULL;

The JOIN sits between UPDATE and SET, the reverse of SQL Server's order. A multi-table UPDATE can also set columns in both tables at once.

SQLite

UPDATE orders
SET status = 'shipped'
WHERE id IN (SELECT order_id FROM shipments
             WHERE tracking IS NOT NULL);

An IN subquery runs on every version. UPDATE ... FROM exists since 3.33 if you prefer join-shaped code.

Oracle

UPDATE orders o
SET o.status = 'shipped'
WHERE EXISTS (SELECT 1 FROM shipments s
              WHERE s.order_id = o.id
                AND s.tracking IS NOT NULL);

Runs on every version. 23ai adds join updates: UPDATE orders o SET o.status = 'shipped' FROM shipments s WHERE s.order_id = o.id AND s.tracking IS NOT NULL. MERGE or an updatable join view also work.

Questions that come up

What if the join matches several rows?

Each target row is still updated once, with whichever matching row the engine meets first, in SQL Server, PostgreSQL, MySQL and SQLite; none of them raises an error. Oracle refuses with ORA-30926, in MERGE and in 23ai's UPDATE … FROM alike. Aggregate or deduplicate the source first if it can fan out.

Does Oracle have UPDATE … FROM?

Since 23ai, yes. Before that, use a correlated subquery, a MERGE with only a WHEN MATCHED branch, or an update through a key-preserved inline view.

Can I update the same table I join?

Yes, with a self-join under two aliases in SQL Server, PostgreSQL and SQLite 3.33+. MySQL rejects a plain subquery on the target table (error 1093) but accepts a join to a derived table. Older Oracle needs the correlated form.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.