Table of contents
1.
Introduction
2.
Syntax
3.
Example
3.1.
SQL
3.2.
SQL
4.
-- Employee Updates table
4.1.
SQL
4.2.
SQL
4.3.
SQL
5.
Frequently Asked Questions
5.1.
Can the MERGE statement be used to update & insert rows simultaneously?
5.2.
Is it necessary to have both the WHEN MATCHED THEN & WHEN NOT MATCHED THEN clauses in a MERGE statement?
5.3.
Can the MERGE statement be used with multiple source tables?
6.
Conclusion
Last Updated: Jul 10, 2024
Easy

MERGE Statement in SQL

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

SQL, or Structured Query Language, is a powerful tool used to manage & manipulate data in relational databases. One of the useful statements in SQL is the MERGE statement, which allows you to perform insert, update, or delete operations in a single statement based on certain conditions. 

MERGE Statement in SQL

In this article, we will discuss the MERGE statement in detail, understanding its syntax, usage, & important points to keep in mind while working with it.

Syntax

The basic syntax of the MERGE statement is:

MERGE INTO target_table
USING source_table
ON merge_condition
WHEN MATCHED THEN
    UPDATE SET column1 = value1, column2 = value2, ...
    DELETE WHERE condition
WHEN NOT MATCHED THEN
    INSERT (column1, column2, ...) VALUES (value1, value2, ...);


Let’s understand the different parts of the MERGE statement:

  1. target_table: The table into which you want to merge the data.
     
  2. source_table: The table or subquery that provides the data to be merged.
     
  3. merge_condition: The condition that determines how the rows from the source table match the rows in the target table.
     
  4. WHEN MATCHED THEN: Specifies the actions to be performed when a row in the source table matches a row in the target table based on the merge condition.
     
  5. UPDATE SET: Updates the specified columns in the target table with the corresponding values from the source table.
     
  6. DELETE WHERE: Deletes the matched rows in the target table that satisfy the given condition.
     
  7. WHEN NOT MATCHED THEN: Specifies the action to be performed when a row in the source table does not match any row in the target table based on the merge condition.
     
  8. INSERT: Inserts a new row into the target table with the specified column values.

Example

Let's consider an example. Suppose we have two tables: employees and employee_updates. The employees table contains the current employee information, while the employee_updates table contains the updated information for some employees.

-- Employees table

  • SQL

SQL

CREATE TABLE employees1 (

   employee_id INT PRIMARY KEY,

   name VARCHAR(50),

   department VARCHAR(50),

   salary DECIMAL(10, 2)

);
Output
  • SQL

SQL

INSERT INTO employees1 (employee_id, name, department, salary) VALUES

 (1, 'Rahul', 'IT', 50000.00);

INSERT INTO employees1 (employee_id, name, department, salary) VALUES

 (2, 'Rinki', 'HR', 60000.00);

INSERT INTO employees1 (employee_id, name, department, salary) VALUES

 (3, 'Harsh', 'Finance', 55000.00);
Output

-- Employee Updates table

  • SQL

SQL

CREATE TABLE employee_updates (

   employee_id INT,

   name VARCHAR(50),

   department VARCHAR(50),

   salary DECIMAL(10, 2)

);
Output
  • SQL

SQL

INSERT INTO employee_updates (employee_id, name, department, salary) VALUES

 (1, 'Rahul', 'IT', 55000.00);

INSERT INTO employee_updates (employee_id, name, department, salary) VALUES

 (3, 'Harsh', 'Finance', 60000.00);

INSERT INTO employee_updates (employee_id, name, department, salary) VALUES

 (4, 'Sanjana', 'Marketing', 50000.00);
Output

Now, let's use the MERGE statement to update the employees table based on the information in the employee_updates table:

  • SQL

SQL

MERGE INTO employees1 e

USING employee_updates u

ON e.employee_id = u.employee_id

WHEN MATCHED THEN

   UPDATE SET e.name = u.name, e.department = u.department, e.salary = u.salary

WHEN NOT MATCHED THEN

   INSERT (employee_id, name, department, salary)

   VALUES (u.employee_id, u.name, u.department, u.salary);
Output

In this example:

  1. The MERGE INTO clause specifies the target table employees1 with the alias e.
     
  2. The USING clause specifies the source table employee_updates with the alias u.
     
  3. The ON clause defines the merge condition, which matches the employee_id from both tables.
     
  4. The WHEN MATCHED THEN clause updates the matched rows in the employees table with the corresponding values from the employee_updates table.
     
  5. The WHEN NOT MATCHED THEN clause inserts new rows into the employees table for the records that exist in the employee_updates table but not in the employees table.
     

After executing the MERGE statement, the employees table will be updated with the changes from the employee_updates table, and any new employees will be inserted.

Before executing the MERGE statement, the employees1 table contains the following data:

Output


After executing the MERGE statement, the employees table will be updated as follows:

Output

Let’s see what happened during the MERGE operation:

  1. The row with employee_id = 1 in the employees table matched the row in the employee_updates table. The name, department, and salary columns were updated based on the values from the employee_updates table.
     
  2. The row with employee_id = 2 in the employees table did not have a matching row in the employee_updates table, so it remained unchanged.
     
  3. The row with employee_id = 3 in the employees table matched the row in the employee_updates table. The salary column was updated based on the value from the employee_updates table.
     
  4. The row with employee_id = 4 did not exist in the employees table but was present in the employee_updates table. A new row was inserted into the employees table with the values from the employee_updates table.

Frequently Asked Questions

Can the MERGE statement be used to update & insert rows simultaneously?

Yes, the MERGE statement allows you to update existing rows & insert new rows in a single statement based on the specified conditions.

Is it necessary to have both the WHEN MATCHED THEN & WHEN NOT MATCHED THEN clauses in a MERGE statement?

No, you can use either one or both clauses depending on your requirements. You can have only the WHEN MATCHED THEN clause to update existing rows or only the WHEN NOT MATCHED THEN clause to insert new rows.

Can the MERGE statement be used with multiple source tables?

Yes, you can use joins or subqueries in the USING clause to merge data from multiple source tables into the target table.

Conclusion

In this article, we learned about the MERGE statement in SQL, which provides a powerful & efficient way to perform update, insert, & delete operations in a single statement. We talked about the syntax of the MERGE statement & understood its various components. With the help of a proper example,, we saw how the MERGE statement can be used to update existing rows & insert new rows based on the matching condition.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass