đ The MERGE statement in SQL is used to insert, update, or delete data in a single statement by comparing a target table with a source table or query. It is commonly used for data synchronization, ETL (Extract, Transform, Load) processes, data warehousing, and keeping tables up to date.
đ What is the MERGE Statement?
The MERGE statement compares rows in a target table with rows from a source table or query. Based on whether rows match, SQL can perform different actions such as updating existing rows, inserting new rows, or deleting rows.
Information
đ¯ Why Use MERGE?
MERGE is ideal when data must be synchronized between two datasets.
- đ Synchronize two tables.
- đ Update existing records.
- đ Insert new records automatically.
- đ Optionally delete obsolete records.
- đ Simplify ETL and data migration processes.
đ Sample Tables
Target table: Employees
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 101 | Alice | IT | 70000 |
| 102 | Bob | HR | 55000 |
| 103 | Charlie | Finance | 65000 |
Source table: EmployeeUpdates
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 101 | Alice | IT | 75000 |
| 103 | Charlie | Finance | 68000 |
| 104 | David | Marketing | 60000 |
đ Basic Syntax
MERGE Syntax
MERGE INTO target_table AS target
USING source_table AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET ...
WHEN NOT MATCHED THEN
INSERT (...)
VALUES (...);đĄ Basic MERGE Example
Update existing employees and insert new employees from the source table.
Update and Insert with MERGE
MERGE INTO Employees AS target
USING EmployeeUpdates AS source
ON target.EmployeeID = source.EmployeeID
WHEN MATCHED THEN
UPDATE SET
target.Name = source.Name,
target.Department = source.Department,
target.Salary = source.Salary
WHEN NOT MATCHED THEN
INSERT (EmployeeID, Name, Department, Salary)
VALUES (
source.EmployeeID,
source.Name,
source.Department,
source.Salary
);đ Result After MERGE
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 101 | Alice | IT | 75000 |
| 102 | Bob | HR | 55000 |
| 103 | Charlie | Finance | 68000 |
| 104 | David | Marketing | 60000 |
đī¸ MERGE with DELETE
Some database systems allow deleting target rows that do not exist in the source table.
MERGE with DELETE
MERGE INTO Employees AS target
USING EmployeeUpdates AS source
ON target.EmployeeID = source.EmployeeID
WHEN MATCHED THEN
UPDATE SET
target.Salary = source.Salary
WHEN NOT MATCHED BY TARGET THEN
INSERT (EmployeeID, Name, Department, Salary)
VALUES (
source.EmployeeID,
source.Name,
source.Department,
source.Salary
)
WHEN NOT MATCHED BY SOURCE THEN
DELETE;Important
đ MERGE Using a Query as the Source
The source of a MERGE operation can also be a SELECT query instead of a physical table.
MERGE Using a SELECT Query
MERGE INTO Employees AS target
USING
(
SELECT EmployeeID,
Name,
Department,
Salary
FROM NewEmployees
) AS source
ON target.EmployeeID = source.EmployeeID
WHEN MATCHED THEN
UPDATE SET
target.Salary = source.Salary
WHEN NOT MATCHED THEN
INSERT (EmployeeID, Name, Department, Salary)
VALUES (
source.EmployeeID,
source.Name,
source.Department,
source.Salary
);âī¸ MERGE vs INSERT vs UPDATE
| Feature | MERGE | INSERT | UPDATE |
|---|---|---|---|
| Add New Rows | â Yes | â Yes | â No |
| Modify Existing Rows | â Yes | â No | â Yes |
| Delete Rows | Some databases support it. | â No | â No |
| Typical Use | Synchronize tables. | Add new records. | Modify existing records. |
đŧ Real-World Example
A payroll system receives updated employee information every night. Instead of running separate INSERT and UPDATE statements, a single MERGE statement synchronizes the employee master table with the latest data.
Payroll Synchronization
MERGE INTO Employees AS target
USING PayrollUpdates AS source
ON target.EmployeeID = source.EmployeeID
WHEN MATCHED THEN
UPDATE SET
target.Salary = source.Salary
WHEN NOT MATCHED THEN
INSERT (EmployeeID, Name, Department, Salary)
VALUES (
source.EmployeeID,
source.Name,
source.Department,
source.Salary
);â ī¸ Database Compatibility
| Database | MERGE Support |
|---|---|
| SQL Server | â Supported |
| Oracle | â Supported |
| PostgreSQL | â Supported (PostgreSQL 15 and later) |
| MySQL | â Not supported directly. Use INSERT ... ON DUPLICATE KEY UPDATE or similar techniques. |
| SQLite | â Not supported directly. |
â ī¸ Common Mistakes
- â Matching rows using incorrect join conditions.
- â Assuming every database supports identical MERGE syntax.
- â Updating primary key values unintentionally.
- â Ignoring duplicate rows in the source data.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ MERGE combines insert and update operations into a single statement.
- đ Some databases also support deleting unmatched target rows.
- đ It compares a source dataset with a target table.
- đ It is widely used in ETL, reporting, and data synchronization.
- đ Database support and syntax vary between SQL implementations.
- đ Ensure matching keys uniquely identify rows to avoid errors.