Prerequisite – MERGE Statement
As MERGE statement in SQL, as discussed before in the previous post, is the combination of three INSERT, DELETE and UPDATE statements. So if there is a Source table and a Target table that are to be merged, then with the help of MERGE statement, all the three operations (INSERT, UPDATE, DELETE) can be performed at once.
A simple example will clarify the use of MERGE Statement.
Example:
Suppose there are two tables:
- PRODUCT_LIST which is the table that contains the current details about the products available with fields P_ID, P_NAME, and P_PRICE corresponding to the ID, name and price of each product.
- UPDATED_LIST which is the table that contains the new details about the products available with fields P_ID, P_NAME, and P_PRICE corresponding to the ID, name and price of each product.
The task is to update the details of the products in the PRODUCT_LIST as per the UPDATED_LIST.
Solution
Now in order to explain this example better, let’s split the example into steps.
- Step 1: Recognise the TARGET and the SOURCE table
So in this example, since it is asked to update the products in the PRODUCT_LIST as per the UPDATED_LIST, hence the PRODUCT_LIST will act as the TARGET and UPDATED_LIST will act as the SOURCE table. - Step 2: Recognise the operations to be performed.
Now as it can be seen that there are three mismatches between the TARGET and the SOURCE table, which are:- The cost of COFFEE in TARGET is 15.00 while in SOURCE it is 25.00
PRODUCT_LIST 102 COFFEE 15.00 UPDATED_LIST 102 COFFEE 25.00 - There is no BISCUIT product in SOURCE but it is in TARGET
PRODUCT_LIST 103 BISCUIT 20.00
- There is no CHIPS product in TARGET but it is in SOURCE
UPDATED_LIST 104 CHIPS 22.00
Therefore, three operations need to be done in the TARGET according to the above discrepancies. They are:
- UDPATE operation
102 COFFEE 25.00
- DELETE operation
103 BISCUIT 20.00
- INSERT operation
104 CHIPS 22.00
- The cost of COFFEE in TARGET is 15.00 while in SOURCE it is 25.00
- Step 3: Write the SQL Query.
Note: Refer this post for the syntax of MERGE statement.
The SQL query to perform the above-mentioned operations with the help of MERGE statement is:
/* Selecting the Targetandthe Source */MERGE PRODUCT_LISTASTARGETUSING UPDATE_LISTASSOURCE/* 1. Performing theUPDATEoperation *//* If the P_IDissame,checkforchangeinP_NAMEorP_PRICE */ON(TARGET.P_ID = SOURCE.P_ID)WHENMATCHEDANDTARGET.P_NAME <> SOURCE.P_NAMEORTARGET.P_PRICE <> SOURCE.P_PRICE/*Updatethe recordsinTARGET */THENUPDATESETTARGET.P_NAME = SOURCE.P_NAME,TARGET.P_PRICE = SOURCE.P_PRICE/* 2. Performing theINSERToperation *//*Whennorecords are matchedwithTARGETtableTheninsertthe recordsinthe targettable*/WHENNOTMATCHEDBYTARGETTHENINSERT(P_ID, P_NAME, P_PRICE)VALUES(SOURCE.P_ID, SOURCE.P_NAME, SOURCE.P_PRICE)/* 3. Performing theDELETEoperation *//*Whennorecords are matchedwithSOURCEtableThendeletethe recordsfromthe targettable*/WHENNOTMATCHEDBYSOURCETHENDELETE/*ENDOFMERGE */chevron_rightfilter_none
Output:
PRODUCT_LIST P_ID P_NAME P_PRICE 101 TEA 10.00 102 COFFEE 25.00 104 CHIPS 22.00
So, in this way all we can perform all these three main statements in SQL together with the help of MERGE statement.
Note: Any name other than target and source can be used in the MERGE syntax. They are used only to give you a better explanation.
This article is contributed by Dimpy Varshni. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- SQL | MERGE Statement
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | INSERT INTO Statement
- SQL | DELETE Statement
- SQL | UPDATE Statement
- SQL | INSERT IGNORE Statement
- SQL | Case Statement
- SQL | DESCRIBE Statement
- Delete statement in MS SQL Server
- SELECT INTO statement in SQL
- Select statement in MS SQL Server
- Insert statement in MS SQL Server
- Insert Into Select statement in MS SQL Server
- CREATE and DROP INDEX Statement in SQL
- SQL | Procedures in PL/SQL
- SQL | Difference between functions and stored procedures in PL/SQL
- Difference between SQL and T-SQL
- MySQL | CREATE USER Statement
- Difference between Row level and Statement level triggers
- Mitigation of SQL Injection Attack using Prepared Statements (Parameterized Queries)
Improved By : RishabhPrabhu



