How to do an update + join in PostgreSQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In PostgreSQL, performing an update operation combined with a join can be a powerful tool for modifying the data in one table based on values in another table. This article explores how to execute an UPDATE ... FROM statement involving a join in PostgreSQL, providing technical explanations, examples, and additional insights to enhance your understanding and application of this SQL technique.
Understanding the UPDATE ... FROM Syntax in PostgreSQL
PostgreSQL supports an extended syntax for the UPDATE statement which allows users to incorporate data from other tables. The typical syntax for an UPDATE ... FROM statement involving a join can be broken down as follows:
Here, target_table is the table you want to update, and source_table is the table containing the reference data you aim to use in your update. The condition usually links these tables.
Example Scenario
Suppose you have two tables: employees and departments. The employees table contains employee details including their department IDs, and the departments table includes names of the departments. You want to update the employees table to include the department name for each employee.
employees
| employee_id | employee_name | department_id | department_name |
| 1 | John Doe | 101 | NULL |
| 2 | Jane Smith | 102 | NULL |
departments
| department_id | department_name |
| 101 | Human Resources |
| 102 | Finance |
To update the employees table with the department names from the departments table, the SQL command would be:
Breakdown of the Example
- UPDATE employees
Specifies that the modification is to be made in theemployeestable. - SET department_name = departments.department_name
Determines that thedepartment_namecolumn in theemployeestable will be updated based on thedepartment_namefrom thedepartmentstable. - FROM departments
Indicates that the data for the update comes from thedepartmentstable. - WHERE employees.department_id = departments.department_id;
Provides the condition that thedepartment_idfrom both tables must match.
Key Considerations
- Data integrity: Make sure that the join condition accurately defines the relationship between the tables. Incorrect conditions can lead to wrong updates.
- Performance: Updating large tables can be resource-intensive. Consider performance aspects such as using indexes.
- Transaction Safety: It's advisable to perform such updates within a transaction (
BEGIN;andCOMMIT;) to ensure that changes can be rolled back in case of errors.
Summary Table of Key Points
| Component | Description |
UPDATE ... SET | Specifies the table and columns to update. |
FROM | Specifies the data source table for the update. |
WHERE | Establishes the conditions for the join. |
Advanced Usage
- Using multiple tables in FROM: You can join multiple tables in the FROM clause as needed.
- Using subqueries: Subqueries can be used in the FROM clause or the WHERE clause to pivot data before an update.
Conclusion
Updating tables in PostgreSQL using joins is a technique that can streamline database administration tasks and data synchronization. By understanding and leveraging the UPDATE ... FROM syntax, database administrators and developers can manipulate large datasets efficiently and ensure consistency across their database environments.
Related reading
- How to do binary search by table with known data order in specific fields SQL
- How to do bulk multi row inserts with JpaRepository?
- How to do Query in DynamoDB on the basis of HashKey and range Key?
- How to do raw mongodb operations in mongoose?
- How to do SQL Like in Linq?
- How to do this in Laravel, subquery where in
- How to drop a PostgreSQL database if there are active connections to it?
- How to drop a table if it exists?

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.