MERGE
MERGE is a SQL statement in the DML Statements category. Performs UPSERT in a single statement (SQL Server, Oracle, PostgreSQL). Matched? UPDATE. Not matched? INSERT. The syntax is MERGE INTO target_table USING source_table ON condition WHEN MATCHED THEN UPDATE ... WHEN NOT MATCHED THEN INSERT ...;. It returns rows affected. A typical example: MERGE INTO products AS t USING new_products AS s ON t.id = s.id WHEN MATCHED THEN UPDATE SET t.price = s.price, t.name = s.name WHEN NOT MATCHED THEN INSERT (id, name, price) VALUES (s.id, s.name, s.price); -- Updates existing or inserts new products A close relative is INSERT INTO, which inserts new rows into a table. A close relative is UPDATE, which modifies existing rows in a table. Always use WHERE to avoid updating all rows. A close relative is DELETE, which removes rows from a table. Always use WHERE to avoid deleting all rows.