SQL - UPDATE JOIN
SQL - UPDATE JOIN
htm
For instance, if a student changes their primary phone number and wishes to update it
in their organizational database, the information needs to be modified in multiple tables
like student records, laboratory records, canteen passes etc. Using the JOIN clause, you
can combine all these tables into one, and then using UPDATE statement, you can
update the student data in them simultaneously.
If we want to update data in multiple tables, we can combine multiple tables into one
using JOINS and then update them using UPDATE statement. This is also known as
cross-table modification.
Syntax
UPDATE table(s)
JOIN table2 ON table1.join_column = table2.join_column
SET table1.column1 = table2.new_value1,
table1.column2 = table2.new_value2;
Where, JOIN can be: Regular Join, Natural Join, Inner Join, Outer Join, Left Join, Right
Join, Full Join etc.
Example
Assume we have created a table named CUSTOMERS, which contains the personal
details of customers including their name, age, address and salary etc., using the
following query −
Open Compiler
1 of 7 12/4/2024, 4:53 PM
SQL - UPDATE JOIN https://www.tutorialspoint.com/sql/sql-update-joins.htm
Now, insert values into this table using the INSERT statement as follows −
Open Compiler
Let us create another table ORDERS, containing the details of orders made and the date
they are made on.
2 of 7 12/4/2024, 4:53 PM
SQL - UPDATE JOIN https://www.tutorialspoint.com/sql/sql-update-joins.htm
Open Compiler
Using the INSERT statement, insert values into this table as follows −
Open Compiler
Following UPDATE... JOIN query increments the salary of customers by 1000 with
respect to the inflation of their order amount by 500 −
UPDATE CUSTOMERS
JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
SET CUSTOMERS.SALARY = CUSTOMERS.SALARY + 1000,
ORDERS.AMOUNT = ORDERS.AMOUNT + 500;
3 of 7 12/4/2024, 4:53 PM
SQL - UPDATE JOIN https://www.tutorialspoint.com/sql/sql-update-joins.htm
Verification
We can verify whether the changes are reflected in a table by retrieving its contents
using the SELECT statement as follows −
Now, check whether the ORDERS table is updated using the following SELECT
statement −
4 of 7 12/4/2024, 4:53 PM
SQL - UPDATE JOIN https://www.tutorialspoint.com/sql/sql-update-joins.htm
While updating records from multiple tables, if we use the WHERE clause along with the
UPDATE... JOIN statement we can filter the records to be updated (from the combined
result set).
Syntax
The syntax of SQL UPDATE... JOIN with WHERE clause in MySQL database is as follows
−
UPDATE table(s)
JOIN table2 ON column3 = column4
SET table1.column1 = value1, table1.column2 = value2, ...
WHERE condition;
Example
Now, let us execute the following query to increase the salary of customer whose id is 3
−
UPDATE CUSTOMERS
LEFT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
SET CUSTOMERS.SALARY = CUSTOMERS.SALARY + 1000
WHERE ORDERS.CUSTOMER_ID = 3;
Verification
We can verify whether the changes are reflected in a table by retrieving its contents
using the SELECT statement as follows.
As we can see in the table below, SALARY value of "Kaushik" is increased by 1000 −
5 of 7 12/4/2024, 4:53 PM
SQL - UPDATE JOIN https://www.tutorialspoint.com/sql/sql-update-joins.htm
Learn SQL in-depth with real-world projects through our SQL certification course.
Enroll and become a certified expert to boost your career.
In MySQL, the UPDATE statement is followed by the JOIN clause and SET
statements respectively. Whereas, in MS SQL Server the SET statement is followed
by the JOIN clause.
Syntax
UPDATE tables(s)
SET column1 = value1, column2 = value2, ...
FROM table1
JOIN table2 ON table1.join_column = table2.join_column;
Example
In this example, we will update values of the CUSTOMERS and ORDERS table that we
created above; using the following UPDATE... JOIN query −
UPDATE CUSTOMERS
SET SALARY = SALARY + 1000
FROM CUSTOMERS
JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
6 of 7 12/4/2024, 4:53 PM
SQL - UPDATE JOIN https://www.tutorialspoint.com/sql/sql-update-joins.htm
Verification
We can verify whether the changes are reflected in a table by retrieving its contents
using the SELECT statement as follows.
7 of 7 12/4/2024, 4:53 PM