Back to Chapters
Chapter Progress: 0 %0 / 5 Lessons Read
‹›

3. Clean layers: bronze, silver and gold

3.2 MERGE: add new rows, update old ones

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      Every night new orders arrive. Some old orders also change: a pending order becomes delivered. If you only INSERT, the changed order appears twice. You need an upsert: update the row if it exists, insert it if it does not.

      MERGE does both in one statement:

      • ON says how to match rows, usually by the key order_id
      • WHEN MATCHED THEN UPDATE handles orders you already have
      • WHEN NOT MATCHED THEN INSERT handles new orders

      This is an incremental load: you process only today's rows, not the whole history. On big tables it saves hours and money. Dataform writes this same MERGE for you. You will meet it again in the Dataform topic.

      Three ways to load

      • Full refresh: delete everything and rebuild. Simple, but slow on big tables.
      • Append: only add rows. Fast, but changed rows become duplicates.
      • Merge (upsert): add new rows and update changed ones. The safe daily choice.
      Must Know:

      A good load is idempotent: running it twice gives the same table as running it once. MERGE on a key is idempotent. A plain INSERT is not.

      How it fits together

      1. Today's rows
      2. MERGE on order_id
      3. Update matched · insert new

      One MERGE, two jobs

      The target is silver.orders. The source is today's rows.

      SQL

      MERGE silver.orders AS tUSING bronze.orders_today AS sON t.order_id = s.order_idWHEN MATCHED THEN  UPDATE SET status = s.statusWHEN NOT MATCHED THEN  INSERT (order_id, status)  VALUES (s.order_id, s.status);

      Rows at this step

      order_id | status
      ---------+----------
      106      | delivered
      107      | delivered
      109      | pending

      Try it yourself

      MERGE is a DML statement, and the free sandbox does not run DML. If your project has billing turned on, run the walkthrough on two small tables. If not, do it on paper:
      1. Write the target table with orders 106 and 107.
      2. Write today's rows: 106 (delivered) and 110 (pending).
      3. Write the table after the MERGE. It should have three rows.

      Question 1 · Easy

      What does "upsert" mean?

      Question 2 · Medium

      In a MERGE, which part decides whether two rows are the same order?

      Question 3 · Hard

      A nightly load is started twice by mistake. Which design still leaves the table correct?

      Key takeaways

      MERGE updates rows that match on the key and inserts the ones that do not. It lets you load only new and changed rows, and it is safe to run twice.

      Lesson 12 of 90

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousMERGE: add new rows, update old ones
      Save your progress free for 1 month. Start free · Log in