SQL MERGE for Beginners: A Full Tutorial with Example

Опубликовано: 28 Июль 2026
на канале: Ryan & Matt Data Science
2,821
55

🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automations! 📈 https://www.skool.com/data-and-ai-aut...

Today we are taking a look at using MERGE within SQL. This allows us to update, delete, and insert entries within a table.


🚀 Hire me for Data Work: https://ryanandmattdatascience.com/da...
👨‍💻 Mentorships: https://ryanandmattdatascience.com/me...
📧 Email: [email protected]
🌐 Website & Blog: https://ryanandmattdatascience.com/
🖥️ Discord:   / discord  
📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan
📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg

🍿 WATCH NEXT
SQL Tutorials Playlist:    • SQL Tutorials for Data Analysts  
SQL Wildcards:    • SQL Wildcards Made Simple: 8 Examples You ...  
SQL Union vs Union All:    • SQL UNION vs UNION ALL: Understand the Dif...  
SQL Cross Joins:    • SQL Cross Joins Made Easy: 2 Essential Exa...  


In this SQL tutorial, I explain how to use the MERGE statement to efficiently update and insert data in a single query. The MERGE statement allows you to update information in a target table based on data from a source table or query, making it incredibly useful for real-world database operations.

I walk through the key concepts you need to know, starting with understanding target and source tables. Using a baseball example with the 3000 strikeout club, I demonstrate how to update existing player statistics for Max Scherzer and Justin Verlander, while simultaneously inserting new records into the database. This practical example shows exactly how MERGE statements work in action.

The video covers the complete MERGE syntax, including the WHEN MATCHED and WHEN NOT MATCHED clauses, which give you precise control over how data is handled. I explain how to join tables within a MERGE statement, set conditions for updates, and define insert operations—all in one powerful query. Whether you need to update customer lists, synchronize inventory data, or maintain any database where records need both updating and inserting, the SQL MERGE statement is an essential tool to master.

By the end of this tutorial, you'll understand how to write complete MERGE statements and apply them to your own database projects with confidence.

TIMESTAMPS
00:00 Introduction to SQL MERGE
00:52 Real World Applications
01:40 Baseball Example Setup
02:25 Comparing Target and Source Tables
03:15 Defining Target and Source
04:15 Connecting Tables with ON
05:10 UPDATE - When Matched Logic
06:35 INSERT - When Not Matched Logic
08:12 Running the MERGE Statement
09:05 Verifying Results

OTHER SOCIALS:
Ryan’s LinkedIn:   / ryan-p-nolan  
Matt’s LinkedIn:   / matt-payne-ceo  
Twitter/X: https://x.com/RyanMattDS

Who is Ryan
Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Who is Matt
Matt is the founder of Width.ai, an AI and Machine Learning agency. Before starting his own company, he was a Machine Learning Engineer at Capital One.

*This is an affiliate program. We receive a small portion of the final sale at no extra cost to you.