How Does the MERGE Statement Work in SQL Server?
π‘ Concept Name
MERGE Statement in SQL Server is a powerful command that allows conditional INSERT, UPDATE, or DELETE actions based on the comparison between source and target tables.
π Quick Intro
Instead of writing separate statements for insert, update, or delete, the MERGE statement unifies all these operations based on matching keys between two datasets.
π§ Analogy / Short Story
Think of MERGE like synchronizing two contact lists. If a contact exists in both, update it. If it's only in the new list, add it. If itβs missing from the new list but present in the old, delete it. MERGE automates this smart syncing.
π§ Technical Explanation
- π MERGE compares a
target
and asource
table. - β
WHEN MATCHED
: Perform anUPDATE
if rows match. - β
WHEN NOT MATCHED BY TARGET
: Perform anINSERT
. - β
WHEN NOT MATCHED BY SOURCE
: Perform aDELETE
. - β οΈ Must include an
ON
clause to define the matching condition. - π SQL Server supports outputting changes using the
OUTPUT
clause.
π― Purpose & Use Case
- β Synchronize a staging table with a live production table.
- β Simplify conditional upsert operations.
- β Automate ETL processes where source and destination data must align.
π» Real Code Example
MERGE INTO Customers AS Target
USING NewCustomers AS Source
ON Target.CustomerID = Source.CustomerID
WHEN MATCHED THEN
UPDATE SET Target.Name = Source.Name, Target.Email = Source.Email
WHEN NOT MATCHED BY TARGET THEN
INSERT (CustomerID, Name, Email)
VALUES (Source.CustomerID, Source.Name, Source.Email)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;

β Interview Q&A
Q1: What is the purpose of the MERGE statement in SQL Server?
A: To combine INSERT, UPDATE, and DELETE logic into a single command based on a join condition.
Q2: What does "WHEN MATCHED" mean?
A: It indicates the action to take (like UPDATE) when a row exists in both source and target.
Q3: Is MERGE ANSI-standard SQL?
A: Yes, but implementations can vary between databases.
Q4: Can MERGE cause deadlocks?
A: Yes, if not properly managed, especially in concurrent environments.
Q5: What happens if there are multiple matches in MERGE?
A: SQL Server throws an error unless you handle it with additional filters.
π MCQs
Q1. What is the primary purpose of MERGE in SQL Server?
- To delete duplicate rows
- To drop foreign keys
- To combine insert, update, and delete
- To create temporary tables
Q2. What clause defines row matching in MERGE?
- WHERE
- JOIN
- IF
- ON
Q3. What happens when a row only exists in source?
- UPDATE
- DELETE
- ROLLBACK
- INSERT into target
Q4. What can you use to capture changes in MERGE?
- RETURN
- OUTPUT
- ECHO
Q5. Which condition applies when a row is not in the source?
- WHEN NOT MATCHED BY TARGET
- WHEN MATCHED
- WHEN NOT MATCHED BY SOURCE
- WHERE NOT EXISTS
π‘ Bonus Insight
Although powerful, MERGE can have concurrency issues and may perform worse than separate statements in some cases. Test performance and wrap it in transactions for safety.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!