From the course: SQL Queries Made Easy

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Merge rows in tables

Merge rows in tables - SQL Tutorial

From the course: SQL Queries Made Easy

Start my 1-month free trial

Merge rows in tables

- [Instructor] In this video, we'll be learning about the last DML statement, that is MERGE. It updates or inserts data in tables based on a condition. If the row is already there in the table, it updates it and otherwise, it inserts a new row in the table. Instead of giving loops, we can just use one MERGE statement. Hence, we actually improve the performance by using MERGE. It's also useful in data warehouse systems because the data comes from various sources and there's a possibility for duplicate records to be inserted into the table. So in order to prevent that, we use the MERGE statement. Now this is the syntax. Here, as you can see, this table is the table where rows must be inserted or updated. This is going to be the data source from where we get the data to insert or update. ON clause has the condition which determines whether to insert or update. WHEN MATCHED or WHEN NOT MATCHED clause instruct the server…

Contents