Stop 09 · Modifying DataTopics · 7/9

MERGE Statement

Part of the SQL Roadmap

In short

When a nightly file needs to update some rows, add others and remove a few, writing three statements is slow and error-prone. MERGE compares two tables and handles every case in one go.

MERGE compares a source with a target table and inserts, updates or deletes rows in one statement depending on whether they match. It is standard SQL supported by SQL Server, Oracle and PostgreSQL 15+, but not MySQL.

The jaadu lays a fresh paper list beside an old attendance register on a teacher's desk, ticking some names, adding others and striking a few off.

Think of it like this

Matching the attendance register

A class teacher in Bhopal gets a fresh student list from the office. She goes name by name: if a student is already in her register, she updates their section. If a student is new, she adds them. If the office marks someone as transferred, she strikes them off. One pass, every case handled.

Watch it happen

What MERGE does with each source row

Each staging row
Matches a product?
WHEN MATCHED
UPDATE price, stock
DELETE if discontinued
WHEN NOT MATCHED
INSERT new product

1/4MERGE joins the staging table to products using the ON condition, here sku.

The big ideas

What you need to know

Source vs target

The target is the table you change. The source is where new data comes from, often a staging table.

First matching clause wins

WHEN clauses run in order. Put the more specific ones, like WHEN MATCHED AND discontinued, first.

Where it runs

SQL Server, Oracle and PostgreSQL 15+ support MERGE. MySQL doesn't, so use ON DUPLICATE KEY UPDATE there.

One source row per target

If two staging rows match the same product, MERGE stops with an error. Remove duplicates from the source first.

See it in code

Walk through the code

sql
1MERGE INTO products AS t2USING staging_products AS s3  ON t.sku = s.sku4WHEN MATCHED AND s.is_discontinued THEN5  DELETE6WHEN MATCHED THEN7  UPDATE SET price = s.price,8             stock = s.stock9WHEN NOT MATCHED THEN10  INSERT (sku, product_name, price, stock)11  VALUES (s.sku, s.product_name, s.price, s.stock);

1/4Target, source and how to pair their rows.

Sync staging_products into products in one statement. Needs PostgreSQL 15 or newer. SQL Server uses the same shape, and also allows WHEN NOT MATCHED BY SOURCE to delete products missing from the file (PostgreSQL added that in 17).

Save yourself the pain

Common mistakes

Oops

Putting WHEN MATCHED THEN UPDATE before WHEN MATCHED AND s.is_discontinued THEN DELETE.

Do this instead

The plain clause catches every match first, so the DELETE never runs. Specific clauses go on top.

Oops

Using MERGE on MySQL.

Do this instead

MySQL has no MERGE. Use INSERT ... ON DUPLICATE KEY UPDATE, plus a separate DELETE if needed.

Your mission

Nightly product sync

~30 min

Quick check

A staging row matches a product and is_discontinued is true. The first clause is WHEN MATCHED THEN UPDATE. What happens?

Go deeper

  1. 1Sync a staging table into a main table with MERGE: update matching rows and insert new ones.
  2. 2Check your database version supports MERGE before relying on it.
  3. 3Compare MERGE with INSERT ... ON CONFLICT for simple upserts.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge