Lesson 55/100

Tutorials PostgreSQL Tutorial

MERGE Statement — Complete Guide

MERGE Statement — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.

On this page

PostgreSQL Tutorial · Lesson 55 of 100

MERGE Statement

SQL ✓Advanced

Advanced · 2 — Production · ~10 min · PostgreSQL — JSONB & Modern Features

What is this?

MERGE (PostgreSQL 15+) upserts in one statement — match source to target ON keys, WHEN MATCHED UPDATE, WHEN NOT MATCHED INSERT. Replaces brittle INSERT ON CONFLICT patterns for some ETL.

Why should you care?

PostgresVerse nightly inventory sync from warehouse file merges stock counts without separate UPDATE and INSERT scripts.

See it live — copy this example

Run in pgAdmin or psql.

MERGE INTO products AS t
USING staging_products AS s
ON t.product_id = s.product_id
WHEN MATCHED AND s.stock_qty IS DISTINCT FROM t.stock_qty THEN
  UPDATE SET stock_qty = s.stock_qty, updated_at = now()
WHEN NOT MATCHED THEN
  INSERT (product_id, name, stock_qty) VALUES (s.product_id, s.name, s.stock_qty);

What happened?

  • staging_products drives merge.
  • Matched rows update only if stock changed.
  • Unmatched rows insert new products.
  • One atomic statement.

Practice next

  1. Create staging_products with mix of existing and new ids.
  2. Run MERGE and compare products table.
  3. Add WHEN NOT MATCHED BY SOURCE DELETE for full sync (careful).
  4. Add AND s.deleted THEN DELETE branch for soft-delete sync.
  5. Compare row counts with INSERT ON CONFLICT DO UPDATE.

Remember

MERGE combines insert/update (and optional delete). IS DISTINCT FROM avoids noop updates. Requires PostgreSQL 15+.

PostgresVerse WMS sync

Warehouse management MERGE refreshes 50k SKUs every 15 minutes.

Outcome: Inventory accuracy improves; script lines cut in half.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior Detailed
Explain SQL queries in the context of PostgreSQL.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define SQL queri…
Mid Detailed
What are common mistakes teams make with Schema design when using PostgreSQL?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Schema de…
Senior Detailed
How would you debug a production issue related to Transactions in a PostgreSQL application?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Transacti…
Mid Detailed
Compare two approaches to Indexing—when would you choose each?
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Indexing…
Junior Detailed
Describe a real-world scenario where Normalization mattered in a PostgreSQL project.
Short answer: Interviewers want a crisp definition, a practical example from your projects, and awareness of trade-offs—not textbook dumps. Explain a bit more How to structure your answer (60–90 seconds) Define Normaliza…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

PostgreSQL Tutorial
Course syllabus

PostgreSQL Tutorial

PostgreSQL — Foundations
PostgreSQL — SQL & Queries
PostgreSQL — Indexing & Performance
PostgreSQL — Transactions & MVCC
PostgreSQL — Functions & Automation
PostgreSQL — JSONB & Modern Features
PostgreSQL — Replication & High Availability
PostgreSQL — Security & Cloud
PostgreSQL — Monitoring & Troubleshooting
PostgreSQL — Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details