IT lexicon › Database › MERGE (SQL)

MERGE (SQL)

Database På svenska → Updated: 2026-05-28

SQL-standard command for upsert + delete in one. MERGE INTO target USING source ON ... WHEN MATCHED THEN UPDATE ... WHEN NOT MATCHED THEN INSERT. Standardized since SQL:2003. PostgreSQL 15+ (2022), Oracle, SQL Server, MySQL 8+.

Use case: sync large dataset from external source (CSV import, replication batch). One statement instead of separate INSERT + UPDATE + DELETE loop. WHEN NOT MATCHED BY SOURCE: handle rows in target not in source (delete or mark stale). Trade-off vs Postgres INSERT ... ON CONFLICT: MERGE is more flexible (delete support), ON CONFLICT is simpler for pure upsert. Performance: MERGE optimized for batch operations, can use parallelism. Classic use: data warehouse ETL.

← Back to the lexicon