MERGE is standard SQL in SQL Server, Oracle and PostgreSQL 15+, and the three spellings differ only in details. Teams that distrust SQL Server's MERGE under concurrency often write an UPDATE followed by an INSERT … WHERE NOT EXISTS in one transaction instead.
Where they differ
MERGE is the batch version of the one-row upsert: it joins a source (a staging table, a query, a VALUES list) to the target and applies a different action to matched and unmatched rows. Each example adds the day's stock movements and deletes items whose stock drops to zero or below. MySQL and SQLite have no MERGE, so their rows run the upsert and then a DELETE for the items that moved and hit zero.
How each engine writes it
SQL Server
MERGE INTO stock WITH (HOLDLOCK) AS t
USING daily_moves AS s ON t.sku = s.sku
WHEN MATCHED AND t.qty + s.qty <= 0 THEN DELETE
WHEN MATCHED THEN UPDATE SET t.qty = t.qty + s.qty
WHEN NOT MATCHED THEN INSERT (sku, qty) VALUES (s.sku, s.qty);
MERGE must end with a semicolon. HOLDLOCK keeps concurrent merges from inserting the same new key twice.
PostgreSQL
MERGE INTO stock t
USING daily_moves s ON t.sku = s.sku
WHEN MATCHED AND t.qty + s.qty <= 0 THEN DELETE
WHEN MATCHED THEN UPDATE SET qty = t.qty + s.qty
WHEN NOT MATCHED THEN INSERT (sku, qty) VALUES (s.sku, s.qty);
PostgreSQL 15+. The SET target can't be qualified with the alias. Before 15, use INSERT … ON CONFLICT plus a separate DELETE.
MySQL
INSERT INTO stock (sku, qty)
SELECT sku, qty FROM daily_moves AS m
ON DUPLICATE KEY UPDATE qty = stock.qty + m.qty;
DELETE FROM stock
WHERE qty <= 0 AND sku IN (SELECT sku FROM daily_moves);
No MERGE. INSERT … SELECT … ON DUPLICATE KEY UPDATE covers the matched and unmatched cases and the DELETE removes the items that hit zero; run both in one transaction.
SQLite
INSERT INTO stock (sku, qty)
SELECT sku, qty FROM daily_moves WHERE true
ON CONFLICT (sku) DO UPDATE SET qty = qty + excluded.qty;
DELETE FROM stock
WHERE qty <= 0 AND sku IN (SELECT sku FROM daily_moves);
No MERGE. The WHERE true is required: without it, SQLite reads ON as the start of a join constraint and the statement fails. Run the INSERT and the DELETE in one transaction.
Oracle
MERGE INTO stock t
USING daily_moves s ON (t.sku = s.sku)
WHEN MATCHED THEN UPDATE SET t.qty = t.qty + s.qty
DELETE WHERE t.qty <= 0
WHEN NOT MATCHED THEN INSERT (sku, qty) VALUES (s.sku, s.qty);
Oracle has had MERGE since 9i. Its DELETE WHERE clause is part of the update branch and tests the values after the update, so only rows that were just updated can be deleted.
Questions that come up
Why is SQL Server MERGE considered risky?
Without HOLDLOCK (serializable) on the target, two concurrent merges can both decide a key is missing and both insert it, and one fails with a duplicate-key error. Older builds also had a list of MERGE bugs around filtered indexes and triggers, so some shops ban it in favor of UPDATE then INSERT.
Does MERGE need a unique index?
Not formally, but each target row must match at most one source row. If two source rows match the same target row, SQL Server (error 8672), PostgreSQL and Oracle (ORA-30926) abort the statement. A unique key on the join columns of the source prevents that.
What replaces MERGE in MySQL?
INSERT … SELECT … ON DUPLICATE KEY UPDATE for the matched and unmatched cases, with a DELETE before or after it in the same transaction for the removal case.