In modern applications, keeping data in sync between tables is a common requirement—especially in ETL processes, reporting systems, and bulk updates. The
SQL MERGE statement is designed to handle this exact problem by combining
INSERT, UPDATE, and DELETE operations into a single, atomic query.
What is SQL MERGE?
MERGE allows you to compare two tables (source and target) and perform different actions based on whether records match or not.
In simple terms:
“If data exists → update it, if not → insert it, and optionally delete old data.”
Basic Syntax
MERGE TargetTable AS target
USING SourceTable AS source
ON target.Id = source.Id
WHEN MATCHED THEN
UPDATE SET target.Name = source.Name
WHEN NOT MATCHED THEN
INSERT (Id, Name)
VALUES (source.Id, source.Name)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
How It Works
The MERGE statement works in 3 logical steps:
- Match Condition (
ON)
Defines how records from source and target are compared. - WHEN MATCHED
If a record exists in both → usuallyUPDATE. - WHEN NOT MATCHED (BY TARGET)
If record exists in source but not in target →INSERT. - WHEN NOT MATCHED BY SOURCE
If record exists in target but not in source →DELETE(optional).
Real-World Example
Scenario:
You have:
Employees(target table)Employees_Staging(source table)
You want to:
- Update existing employees
- Insert new employees
- Remove employees no longer present
MERGE Employees AS target
USING Employees_Staging AS source
ON target.EmpId = source.EmpId
WHEN MATCHED THEN
UPDATE SET
target.Name = source.Name,
target.Salary = source.Salary
WHEN NOT MATCHED THEN
INSERT (EmpId, Name, Salary)
VALUES (source.EmpId, source.Name, source.Salary)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
Why Use MERGE?
1. Single Query for Multiple Operations
Instead of writing separate INSERT, UPDATE, and
DELETE, everything is handled in one statement.
2. Better Performance (in bulk operations)
Efficient for syncing large datasets.
3. Cleaner Code
Reduces complexity and improves readability.
Advanced Features
Conditional Updates
WHEN MATCHED AND target.Salary <> source.Salary THEN
UPDATE SET target.Salary = source.Salary
Output Clause (Track Changes)
OUTPUT $action, inserted.*, deleted.*;
Useful for auditing changes (INSERT/UPDATE/DELETE logs).
Important Considerations
1. Use Carefully in High-Concurrency Systems
MERGEcan cause unexpected results if multiple operations happen simultaneously.
2. Known Issues in SQL Server
- In some versions of SQL Server,
MERGEhas edge-case bugs. Many experts recommend:- Use separate statements (
INSERT,UPDATE) for critical systems.
- Use separate statements (
3. Always Use Proper Indexing
- Matching condition (
ON) should be indexed for performance.
When to Use MERGE
Use MERGE when:
- You need to sync two tables
- You are working with ETL or data warehousing
- You want bulk upsert operations (Update + Insert)
Avoid it when:
- You need simple CRUD operations
- Your system is highly concurrent and sensitive
MERGE vs Traditional Approach
| Operation | Traditional SQL | MERGE |
|---|---|---|
| Insert | Separate query | Combined |
| Update | Separate query | Combined |
| Delete | Separate query | Combined |
| Readability | Medium | High |
| Risk | Low | Medium (if misused) |
Best Practices
- Always test
MERGEqueries on staging data - Use transactions for safety
- Add proper indexes
- Avoid complex logic inside
MERGE - Log changes using
OUTPUT
Conclusion
The SQL MERGE statement is a powerful tool for data synchronization and bulk operations. While it simplifies code and improves efficiency, it must be used carefully, especially in high-concurrency environments.
One-Line Summary
SQL MERGE lets you insert, update, and delete data in a single statement by comparing source and target tables.