Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 11 / Topic 6 of 7

MERGE

MERGE (PostgreSQL 15+) is the set-based big sibling: compare a source against a target and say what happens on match, no-match — including deletes. A price-list sync where zero means discontinue:

CREATE TABLE price_updates (product_id integer, new_price numeric(10,2));
INSERT INTO price_updates VALUES (1, 1150.00), (9, 0), (10, 18.00);
MERGE INTO products AS p
USING price_updates AS u ON p.product_id = u.product_id
WHEN MATCHED AND u.new_price = 0 THEN
  UPDATE SET stock = 0, import_note = 'discontinued'
WHEN MATCHED THEN
  UPDATE SET price = u.new_price
WHEN NOT MATCHED THEN
  DO NOTHING;
SELECT product_id, name, price, stock, import_note
FROM products
WHERE product_id IN (1, 9, 10)
ORDER BY product_id;
CREATE TABLE
INSERT 0 3
MERGE 3
 product_id |     name      |  price  | stock | import_note  
------------+---------------+---------+-------+--------------
          1 | Laptop Pro 14 | 1150.00 |    15 | 
          9 | Desk Lamp     |   45.00 |     0 | discontinued
         10 | USB-C Cable   |   18.00 |   200 | 
(3 rows)

Three rows of instructions, three different outcomes, one statement, one pass. MERGE vs ON CONFLICT: upsert is the right tool for "this row arrived, store it"; MERGE shines for reconciling whole datasets — nightly syncs, staging-to-production merges — where matched/not-matched/delete branches all carry logic. (One honest caution: under heavy concurrency MERGE doesn't have ON CONFLICT's never-errors guarantee — concurrent inserts can still make it raise a unique violation.)

🧪 Quick quiz — answer from memory, then check.

  1. What three row-fates can a MERGE express?
  2. Upsert or MERGE for a nightly full price-list reconciliation?
Show the answers
  1. A matched row gets an update (or a delete), an unmatched one gets an insert (or nothing) — each branch with its own conditions.
  2. MERGE — multiple branches over a whole source set is exactly its shape.

Exercise 6: Reconcile the stock count

Scenario: Inventory says products 1, 2, 3 hold 15, 8, 25. The warehouse count says: product 1 → 14, product 3 → 0 (gone — remove the row), product 4 → 40 (new). What to do: One MERGE: matched-and-zero deletes, matched updates, unmatched inserts. Product 2, uncounted, stays untouched.

Sample output:

 product_id | stock 
------------+-------
          1 |    14
          2 |     8
          4 |    40
(3 rows)

💡 Hint: branch order matters — the AND s.counted = 0 THEN DELETE arm must come before the plain WHEN MATCHED.


Show the solution to Exercise 6
-- Exercise 6: Reconcile the stock count (MERGE)
DROP TABLE IF EXISTS inventory, stock_count;
CREATE TABLE inventory (product_id integer PRIMARY KEY, stock integer NOT NULL);
INSERT INTO inventory VALUES (1, 15), (2, 8), (3, 25);
CREATE TABLE stock_count (product_id integer, counted integer);
INSERT INTO stock_count VALUES (1, 14), (3, 0), (4, 40);
MERGE INTO inventory AS i
USING stock_count AS s ON i.product_id = s.product_id
WHEN MATCHED AND s.counted = 0 THEN DELETE
WHEN MATCHED THEN UPDATE SET stock = s.counted
WHEN NOT MATCHED THEN INSERT (product_id, stock) VALUES (s.product_id, s.counted);
SELECT * FROM inventory ORDER BY product_id;

Search the guide