How to Use MERGE in Oracle
Last updated August 25, 2026 · By the SaturnSQL team
MERGE INTO ... USING ... ON combines an upsert into one statement: WHEN MATCHED updates existing rows, WHEN NOT MATCHED inserts new ones.
MERGE INTO customers c
USING staging_customers s
ON (c.customer_id = s.customer_id)
WHEN MATCHED THEN
UPDATE SET c.email = s.email, c.updated_at = SYSDATE
WHEN NOT MATCHED THEN
INSERT (customer_id, email, created_at)
VALUES (s.customer_id, s.email, SYSDATE);Deleting via MERGE
A DELETE clause nested inside WHEN MATCHED removes rows that matched the join and also satisfy the DELETE's WHERE condition. It only ever deletes rows already touched by the UPDATE, never arbitrary rows from the source or target.
MERGE INTO inventory i
USING discontinued_products d
ON (i.product_id = d.product_id)
WHEN MATCHED THEN
UPDATE SET i.status = 'discontinued'
DELETE WHERE i.quantity_on_hand = 0;Columns referenced in the ON clause cannot be changed by the UPDATE SET list. Oracle raises ORA-38104 if you try, because updating a join column mid-merge would make it ambiguous which rows still match.
Run this in SaturnSQL
SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.
Try it free