SQL Query to Update Columns Values With Column of Another Table
Last Updated :
17 Dec, 2024
The SQL UPDATE statement is one of the most powerful tools for modifying data in a relational database. Sometimes, we need to update column values in one table based on values from another table. This can be done using various methods such as JOIN or nested SELECT statements.
In this article, we will explore both of these methods, providing us with practical examples and best practices to help you master SQL updates across multiple tables.
What is the SQL UPDATE Statement?
The SQL UPDATE statement is used to modify the existing data in one or more columns of a table. It is a very common operation when managing database records, and it allows us to change the values of specific columns based on certain conditions. When working with multiple tables, SQL provides several ways to update columns in one table using the data from another table.
Syntax
UPDATE table_name
SET column_name = value
WHERE condition;
Methods to Update Column Values Using Data from Another Table
There are several methods to achieve this, such as using nested SELECT statements or JOIN operations. Both techniques allow us to retrieve data from one table and use it to update corresponding values in another table, ensuring data consistency and reducing manual intervention.
1. Using Nested SELECT Statements
One of the most common ways to update column values from another table is by using nested SELECT statements. This method allows us to select data from the second table and use it to update the first table. The nested SELECT query is placed inside the SET clause of the UPDATE statement.
Example: Update Column Values Using Nested SELECT
Let’s consider two tables: student_details and Updated_CG. In the student_details table, we store student information, including their CGPA (Cumulative Grade Point Average), and in the Updated_CG table, we have the updated CGPA values for each student
Table1: Student_details
CREATE TABLE student_details(
stu_id VARCHAR(8),
stu_name VARCHAR(20),
stu_cgpa DECIMAL(4,2) );
INSERT INTO student_details (stu_id, stu_name, stu_cgpa) VALUES
('40001', 'PRADEEP', 9.6),
('40002', 'ASHOK', 8.2),
('40003', 'PAVAN KUMAR', 7.6),
('40004', 'NIKHIL', 8.2),
('40005', 'RAHUL', 7.0);
SELECT * FROM student_details
Output
Student_details TableTable2: Updated_CG
CREATE TABLE Updated_CG(
stu_id VARCHAR(20),
updated_cg DECIMAL(4,2));
INSERT INTO Updated_CG (stu_id, updated_cg) VALUES
('40001', 8.6),
('40002', 8.4),
('40003', 6.6),
('40004', 7.2),
('40005', 7.8);
SELECT * FROM Updated_CG
Output
Updated_CG TableIn this example, we want to update the CGPA in the student_details table using the updated CGPA values from the Updated_CG table. We can do this with a nested SELECT statement.
Query:
UPDATE student_details
SET stu_cgpa = (SELECT updated_cg
FROM Updated_CG
WHERE student_details.stu_id = Updated_CG.stu_id)
Output
updated Student_details Using Nested SELECT Explanation:
In this example, the CGPA values in the student_details table are updated based on the corresponding stu_id values from the Updated_CG table. The nested SELECT statement retrieves the updated CGPA for each student and updates the record accordingly
2. Using SQL JOIN to Update Column Values from Another Table
Another method to update a column in one table using data from another table is by using an SQL JOIN. This method is particularly useful when we need to update values based on a relationship between two tables.
Example: Update Column Values Using SQL JOIN
In the following example, we will update the CGPA in the student_details table using the Updated_CG table, this time using an INNER JOIN.
Query:
UPDATE student_details
SET stu_cgpa = Updated_CG.updated_cg
FROM student_details
INNER JOIN Updated_CG
ON student_details.stu_id = Updated_CG.stu_id;
Output
stu_id | stu_name | stu_cgpa |
---|
40001 | PRADEEP | 8.6 |
40002 | ASHOK | 8.4 |
40003 | PAVAN KUMAR | 6.6 |
40004 | NIKHIL | 7.2 |
40005 | RAHUL | 7.8 |
Explanation:
In this case, the INNER JOIN is used to link the student_details table with the Updated_CG table based on the stu_id column. The UPDATE statement then sets the stu_cgpa in the student_details table to the corresponding updated_cg value from the Updated_CG table
Conclusion
The SQL UPDATE statement is an essential part of database management. Updating columns with values from another table is a common use case that can be achieved using either nested SELECT statements or JOINs. By using these methods, we can efficiently update multiple records and ensure that our data remains consistent across tables. By mastering these SQL techniques, we can confidently handle bulk updates, maintain data consistency, and ensure smooth database operations.
Similar Reads
Update One Table with Another Table's Values in MySQL
Sometimes we need to update a table data with the values from another table in MySQL. Doing this helps in efficiently updating tables while also maintaining the integrity of the database. This is mostly used for automated updates on tables. We can update values in one table with values in another ta
3 min read
SQL Query to Update All Columns in a Table
In SQL, sometimes we need to write a single query to update the values of all columns in a table. We will use the UPDATE keyword to achieve this. For this, we use a specific kind of query shown in the below demonstration. For this article, we will be using the Microsoft SQL Server as our database an
2 min read
SQL Query to Combine Two Tables Without a Common Column
In SQL, combining tables from different sources often involves joining tables with common columns or relationships. However, real-world scenarios frequently require more complex tasks like joining two tables without a common column. This article will guide us through various methods to merge tables
4 min read
How to Update a Column in a Table in SQL Server
In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out
4 min read
How to Add a Column with a Default Value to an Existing Table in SQL Server
In SQL Server sometimes it may be required to set the default value to a Column in a table. This default value will be set as the column value when there is no value inserted or left empty for that particular column while data is inserted into the Table. Sometimes after creating a table, it may be r
8 min read
SQL Server Update From One Table to Another Based on an ID Match
In the world of database management, we need to perform various OLTP operations like insert, update, and delete. The ability to efficiently update data between tables is crucial for maintaining data integrity and ensuring accurate information. SQL Server provides powerful tools to accomplish this ta
8 min read
SQL Query to Update All Rows in a Table
Updating all rows in a SQL table is a common task when we need to make changes to every record without specifying individual rows. This operation can be performed using the UPDATE statement combined with the SET clause, which allows for modifying values across all rows of a specified table. Understa
5 min read
How to Update Table Rows Using Subquery in MySQL
Updating table rows using subqueries in MySQL enables precise modifications based on specific conditions or values from other tables. This technique leverages subqueries within the SET or WHERE clauses of the UPDATE statement, allowing dynamic and context-specific updates. This guide covers the synt
5 min read
How to Copy Data From One Column to Another in the Same Table in SQL?
Efficiency in data manipulation is crucial while using Structured Query Language (SQL). To manage a wide range of tasks, including organizing, retrieving, updating, and deleting data, SQL provides a comprehensive set of instructions. Among these, copying data between columns in the same table is a c
4 min read
How to Update all the Values of a Specific Column of SQLite Table using Python ?
In this article, we are going to update all the values of a specific column of a given SQLite table using Python. In order to update all the columns of a particular table in SQL, we use the UPDATE query. The UPDATE statement in SQL is used to update the data of an existing table in the database. We
3 min read