MySQL - UPDATE query based on SELECT Query
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MySQL provides a powerful and efficient way to manage and manipulate data within databases through various SQL queries. One of the common requirements when working with databases is to update existing records based on conditions derived from a SELECT query. This often involves using data from one or more tables to update records in another table.
Using UPDATE Query Based on SELECT Query
The UPDATE ... SELECT construct allows users to modify existing records in a table using the results from a SELECT statement. This is particularly useful for sophisticated database operations such as copying values from one table to another or modifying data using results within the same table.
Basic Syntax
The basic syntax of an UPDATE based on a SELECT query is as follows:
In this structure:
table_name: The table you want to update.column1,column2: Fields in thetable_nametable to be updated.another_table: The table from which you will select the new values.value1,value2: Values that are retrieved by theSELECTstatement.conditions: Conditions to match records for both updating and selecting.
Example Scenario
Consider two tables, employees and departments:
employees Table:
| emp_id | name | dept_id | salary |
| 1 | Alice | 101 | 75000 |
| 2 | Bob | 102 | 60000 |
| 3 | Charlie | 103 | 65000 |
departments Table:
| dept_id | dept_name | location |
| 101 | HR | New York |
| 102 | Finance | London |
| 103 | IT | Toronto |
Now, suppose you want to increase the salary of all employees working in the IT department by 10%. You could use the UPDATE ... SELECT query as follows:
In this example, the SELECT statement finds the dept_id of the 'IT' department, and the UPDATE statement uses that to raise the salary for the relevant employees.
Using JOIN for More Complex Scenarios
For more complex updates involving relationships between tables, you can use JOIN within the UPDATE statement. This can be extremely beneficial when values need to be updated based on matching conditions across multiple tables.
Example with JOIN
Updating salaries based on department performance factors, where performance data is stored in another table:
performance Table:
| dept_id | performance_factor |
| 101 | 1.05 |
| 102 | 1.02 |
| 103 | 1.08 |
Update employee salaries based on their department's performance factor:
This query joins the employees and performance tables based on dept_id, and updates the salary of each employee based on their department’s performance_factor.
Summary Table
| Feature | Description |
| Basic Syntax | UPDATE ... SET ... WHERE ... |
| Using Simple Subquery | Updates table using values from a single or related subquery |
| Using JOIN for Complex Updates | Use JOIN to update using related tables with common fields |
| Updates Based on Matching Conditions | Conditions apply to both the selection and update processes |
| Real-world Use Cases | Reflects practical data manipulation needs in businesses |
Additional Considerations
- Performance: Be cautious when updating large datasets, as complex joins and subqueries can be resource-intensive. It can be useful to ensure indexes are used effectively.
- ACID Compliance: MySQL ensures updates are atomic. If an update fails halfway, changes are rolled back to maintain database integrity.
- Error Handling: Be prepared to handle exceptions or errors arising from update operations, especially where conditional updates depend on data integrity across tables.
With prudent use of UPDATE queries based on SELECT results, database administrators and developers can effectively manage dynamic data manipulation tasks within MySQL, paving the way for enhanced data-driven decision-making.
Related reading
- MySQL - Using COUNT in the WHERE clause
- MySQL - why not index every field?
- MySQL 5.0 indexes - Unique vs Non Unique
- MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
- MySQL Access denied for user 'test''localhost' using password YES except root user
- Mysql adding user for remote access
- MySQL Alternatives to ORDER BY RAND
- MySQL and GROUP_CONCAT maximum length

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.