Update One Table with Another Table

Sometimes we need to update the data of one table with the data from another table in SQL. It can be done in different ways. We can use stored procedure, single SQL update statement or else. This article explains how to update one table by another table in SQL query. Summary of the article:

Table Creation

Data Insertion

SQL Query to Update One Table with Another Table

Output

Suppose we have two tables (Student1 and Student2). We want to update the data of Student1 table with Student2 table.

Table Creation
At first we need to create Student1 & Student2 tables.It can be done in different ways. We can create the table in database directly or can use temporary table. For simplicity here we will use table variable. Following SQL scripts can be used to declare table variable.

If we run a normal a normal SQL query to select all the data from the two tables we will get the following result. This is for observing the current status of the tables. For that sample SQL code is given bellow:

SELECT * FROM @Students1
SELECT * FROM @Students2

SQL Query to Update One Table with Another Table
In order to update the data of Student1 table with the data of Student2 table writes the following SQL quary and execute it:

Output
If we check the current status of the table, we will get the updated data. Simple SQL select query is given bellow:

SELECT * FROM @Students1
SELECT * FROM @Students2

In this way we can update one table by another table using SQL query.

About Author

Rashed

I’m Rashedul Alam, software engineer/architect, technical blogger, and educator. I have over 10 years of working experience in the field of software design and development. I like to share my knowledge and technical stuff with others. I am a community contributor, providing problem solving answer through different forum.