SQL · Pattern 12 — Data Manipulation (DML)
MERGE / UPSERT
MERGE inserts new rows or updates existing rows in a single statement.
Source data — Pattern 12: Data Manipulation (DML)
All queries run against the shared employees table (see schema.sql). This pattern introduces core Data Manipulation Language (DML) statements. Topic 79 introduces an employee_updates table to demonstrate the MERGE / UPSERT operation.


What it does
MERGE inserts new rows or updates existing rows in a single statement.
Problem
Insert or update employee records.
MERGE INTO employees e
USING employee_updates u
ON(e.employee_id=u.employee_id)
WHEN MATCHED THEN UPDATE SET e.salary=u.salary
WHEN NOT MATCHED THEN INSERT(employee_id,first_name,salary)
VALUES(u.employee_id,u.first_name,u.salary);
Result
