MySQL

MySQL UPDATE JOIN Queries: A Complete Guide

intro

Learn everything you need to know about the MySQL UPDATE JOIN statement for updating rows in a table based on data from another table.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Data in a relational MySQL database can be spread across several related tables. Using a JOIN query helps you retrieve that data in its entirety. But what if you want to update data in one table based on data from another related table? This is where a MySQL UPDATE JOIN query comes in!

In this guide, you will see how to use a MySQL UPDATE with JOIN, when it is useful, and more.

Let’s dive in!

MySQL UPDATE JOIN Query: Is It Possible?

TL;DR: Yes, it is possible to perform a query of UPDATE with JOIN in MySQL.

The UPDATE statement supports a JOIN clause, which opens the door to updating rows in one table based on matching data in another table. That is particularly useful for updating data across related tables.

Note that this operation should not be confused with a multi-table UPDATE, where multiple tables are updated simultaneously. In a MySQL UPDATE JOIN, data from multiple tables is used to determine the updates, but only rows in a single table are actually modified.

Scenarios of Using an UPDATE with JOIN in MySQL

Using an UPDATE with a JOIN in MySQL is useful in all high-level scenarios where you need to update rows in a table based on information from another related table.

For instance, consider an e-commerce platform where you have an orders table and a customers table. If you want to update the shipping status of orders based on whether the customer is a premium member, you would have to use a JOIN to fetch the relevant data from both tables and update the orders table accordingly. Another example could involve synchronizing prices in a product catalog based on discounts from a promotions table.

In both cases, a MySQL UPDATE JOIN query enables you to efficiently update rows in one table using data from another. Read our guide for more information on how JOIN operations work.

How to Perform a MySQL UPDATE with JOIN

The syntax of a query with a JOIN in an UPDATE statement in MySQL is as follows:

Copy
        
1 UPDATE table_1 T1 2 [INNER | LEFT] JOIN table_2 T2 ON T1.foreign_id = T2.id 3 SET T1.column_1 = T2.column_1 4 [WHERE ...];

The UPDATE JOIN query in MySQL updates rows in table_1 using data from table_2 based on the matching condition specified in the JOIN clause. In the case of an INNER JOIN, only rows in table_1 that have matching rows in table_2 are updated. With a LEFT JOIN, all rows in table_1 are updated; for rows that do not have a match in table2, NULL values are assigned to the columns in table_2.

Keep in mind that the WHERE clause at the end of the query is essential to prevent all matching rows from being updated. Thanks to it, only the selected joined rows in the filter will be updated.

Notes

Both RIGHT JOIN and CROSS JOIN are technically supported in UPDATE queries in MySQL, but their usage might not always make sense:

  • While a RIGHT JOIN is supported in MySQL UPDATE queries, it is not necessary. You can achieve the same result by rewriting the query as a LEFT JOIN by swapping the table order in the JOIN clause. Using a LEFT JOIN makes the query more readable as the table you need to update is the one “on the left”— it also avoids potential confusion, making it a preferred option in most cases.
  • A CROSS JOIN is supported in UPDATE queries, but it is rarely useful since it produces a Cartesian product between the tables. This means that each row from the first table will be combined with every row from the second table, which is typically not what you want when updating data.

That is why RIGHT and CROSS joins were not mentioned in the above MySQL UPDATE JOIN syntax. Now that you know the reason behind that, let’s move on to the examples of the UPDATE JOIN clause!

MySQL UPDATE JOIN: Examples

Now that you know how UPDATE with JOIN works in MySQL, you are ready to see it in action in two real-world examples.

Note: DbVisualizer, a top-rated and powerful MySQL database client, will be used in this section to make query execution and data exploration easier.

We will update data in the employees and departments sample tables. This is what the employees table looks like:

The data in the employees table in DbVisualizer
The data in the employees table in DbVisualizer

And here is the departments table:

The data in the departments table in DbVisualizer
The data in the departments table in DbVisualizer

Note that department_id in the employees table is a foreign key referencing the departments table, as you can see in the ERD-like references graph generated by DbVisualizer:

The references graph produced by DbVisualzier
The references graph produced by DbVisualzier

Now, let's join and update some data!

UPDATE INNER JOIN Example

Suppose you want to give all employees in the "Engineering" department a 10% salary raise. First, you need to retrieve all employees in that department. You can achieve that with the following INNER JOIN query:

Copy
        
1 SELECT * 2 FROM employees E 3 INNER JOIN departments D ON E.department_id = D.id 4 WHERE D.name = 'Engineering';

The result will be:

Executing the INNER JOIN query in DbVisualizer
Executing the INNER JOIN query in DbVisualizer

You can now update their salary with the MySQL UPDATE with JOIN below:

Copy
        
1 UPDATE employees E 2 INNER JOIN departments D ON E.department_id = D.id 3 SET E.salary = E.salary * 1.10 4 WHERE D.name = 'Engineering';

If you repeat the INNER JOIN query, you will now see:

Note the updated salaries
Note the updated salaries

Note how the salaries have increased as desired.

UPDATE LEFT JOIN Example

Assume you want to give a 5% salary increase to all employees who either do not have a department or whose department is “Sales.” To achieve that, you should use a LEFT JOIN query:

Copy
        
1 SELECT * 2 FROM employees E 3 LEFT JOIN departments D ON E.department_id = D.id 4 WHERE E.department_id IS NULL OR D.name = "Sales";

Execute it, and you will get:

Executing the LEFT JOIN query in DbVisualizer
Executing the LEFT JOIN query in DbVisualizer

You can now perform an update query with:

Copy
        
1 UPDATE employees E 2 LEFT JOIN departments D ON E.department_id = D.id 3 SET E.salary = E.salary * 1.05 4 WHERE E.department_id IS NULL OR D.name = "Sales";

The affected rows will now contain:

Note the updated salaries
Note the updated salaries

How NULL Values Impact Your JOIN Operations

Lastly, keep in mind that NULL values have an impact on JOIN operations, too. Remember—we have assumed that we give a 5% salary increase to all employees who either do not have a department or whose department is “Sales.”

That sentence means that some of you may elect to store NULL values as part of a column value, while some may opt for an empty value in the column. Both approaches are good—but if you use NULL values as part of your JOIN operations, it is important to keep in mind the following:

  • The result of a NULL being joined with any value (even if the other value is not NULL) will be NULL. That’s the case in SQL Server and other database management systems. Those who want to dig deeper into this can refer to the SAP Help Portal.
  • To prevent inaccurate results from being displayed in JOIN operations, always opt to use IS NULL instead of = NULL. NULL values can’t be equal to anything, even to themselves.
  • Understand that a NULL isn’t “an empty value” but rather “an unknown value” — to add to this, don’t use arithmetic operators to test for NULL values in a join (or anywhere else) either. For more information, refer to the documentation of your DBMS of choice.
  • If you use GROUP BY together with your JOIN operations, two NULL values will also be equal to each other.

Keep the above mentioned points in mind, et voilà! You will soon be a MySQL UPDATE JOIN master.

Conclusion

In this blog post, you learned how to use UPDATE statements with a JOIN clause. As demonstrated in two examples, the process becomes much easier with a feature-rich visual database client like DbVisualizer.

DbVisualizer offers many features, including visual query execution, data exploration, and table discovery. Additionally, it comes with advanced features like SQL formatting and ERD-like schema generation. Try DbVisualizer for free today!

FAQ

Is it possible to perform a MySQL INNER JOIN UPDATE query?

Yes, performing an INNER JOIN in a MySQL UPDATE query is possible. This allows you to update values in one table based on matching rows from another table. The INNER JOIN ensures that only rows with matching conditions in both tables are included in the update.

Is it possible to join 3 tables in an UPDATE query in MySQL?

Yes, you can join 3 or more tables in a MySQL UPDATE query. You can chain multiple JOIN clauses together to combine data from multiple tables before updating the target table. That gives you the ability to update values based on conditions from all tables in the JOIN clause.

Is it possible to write a MySQL UPDATE JOIN WHERE query?

Yes, this is not only possible, but a WHERE clause in a MySQL UPDATE JOIN query helps specify the conditions under which rows should be updated after joining tables. This ensures that only the rows meeting the criteria in the WHERE clause are updated, preventing the update from affecting all rows that match the JOIN condition.

Is RIGHT JOIN supported in MySQL UPDATE queries?

Yes, RIGHT JOIN is supported in MySQL UPDATE queries, but its use is uncommon. Rewriting the query using a LEFT JOIN is typically easier and more common.

Why use a database client to deal with JOIN queries?

DbVisualizer is a robust, highly-rated multi-database client that allows you to manage various databases from a single platform. One of its standout features is the ability to visually display data from queries and show the relationships between tables in a database. This simplifies the understanding of JOIN queries and enhances the overall experience of working with relational data. Grab a 21-day DbVisualizer free trial!

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Derived Table: Everything You Need to Know

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-02-06
title

Understanding MVCC in MySQL

author Lukas Vileikis tags DbVisualizer MySQL SQL 7 min 2025-02-05
title

How to Protect MySQL With Fail2Ban

author Antonello Zanini tags MySQL 12 min 2025-01-30
title

SQL Injection Cheat Sheet: SQLi 101

author Lukas Vileikis tags MySQL SECURITY SQL 10 min 2025-01-29
title

A Complete Guide to the ALTER TABLE DROP COLUMN Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-01-27
title

How to Use a Recursive CTE in SQL

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 5 min 2025-01-22
title

Guidelines for a Proper SQL Script: ACID, CRUD, and Other Things You Need to Know

author Lukas Vileikis tags DbVisualizer MySQL OPTIMIZATION SQL 6 min 2025-01-21
title

How to Display All Duplicate Records in a MySQL Table

author Antonello Zanini tags MySQL 6 min 2025-01-08
title

The Most Interesting Functions in MySQL – A Guide

author Lukas Vileikis tags MySQL 5 min 2024-12-27
title

MySQL Primary Key: What It Is, How It Works, and When to Use It

author Lukas Vileikis tags Data Visualization Tools DbVisualizer MySQL 8 min 2024-12-23

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.