Explain the following SQL MERGE statement.
1Times asked
Jun 2026Last seen
Jun 2026First seen
💡 Model Answer
The MERGE statement synchronizes two tables by performing INSERT, UPDATE, or DELETE operations in a single statement. In the provided code:
MERGE INTO customer_target USING customer_sourceestablishes the target and source tables.ON customer_target.customer_id = customer_source.customer_iddefines the join condition.WHEN MATCHED THEN UPDATE SET ...updates rows in the target when a matching key exists, copying the name and city from the source.WHEN NOT MATCHED THEN INSERT (customer_id, customer_name, city) VALUES (...)inserts new rows into the target when the key is absent in the target.WHEN NOT MATCHED BY SOURCE THEN DELETEremoves rows from the target that no longer exist in the source.
This pattern is common in ETL pipelines to keep a target staging or reporting table in sync with a source system. It ensures atomicity and reduces round‑trips compared to separate SELECT/INSERT/UPDATE/DELETE statements.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500