internalsGet Pro

Module 02 · Lesson 10 of 12Pro

MERGE and upserts: updating tables in place

Level
Intermediate
Topic
SQL for data engineering
Reading time
18 min

When you finish this page you can

  1. Choose between a full rebuild, replacing a slice and a MERGE for a changed batch, by what each one reads, writes and keeps as state.
  2. Write a MERGE and an INSERT … ON CONFLICT that update matched rows and insert new ones, and leave frozen columns alone.
  3. Make a merge safe to run twice and safe when an old change arrives after a newer one, with one row per key and a guard on updated_at.
  4. Explain why a row missing from an incremental batch is not a deleted row, and how soft deletes reach the table instead.
  1. Design: a first analytics pipeline for an online store30 min
  2. Deduplication: keeping one row per entity17 min

180,000 changes, 20 million rows rewritten

It’s 01:40 on Tuesday, 10 November. is an online store in India. Each night, Meera, a data engineer on its data team, copies every order that changed from the store’s PostgreSQL read replica into raw_orders in . Then she builds orders_current, one row per order, which Daniel’s finance report reads at 07:00.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan