How UPDATE JOIN Works in PostgreSQL?
Last Updated :
03 Oct, 2024
In PostgreSQL, updating records in one table based on values in another is a common scenario in relational databases. While PostgreSQL does not have a direct UPDATE JOIN
syntax like other databases (e.g., MySQL). Hence, it provides a powerful alternative by using the FROM clause in the UPDATE
statement. This allows us to update one table based on the conditions and matching rows of another table.
UPDATE JOIN in PostgreSQL
- The UPDATE JOIN in PostgreSQL helps us to update rows in one table using values from another table by joining them. This is useful when you need to update data in one table based on information from a related table.
- UPDATE JOIN in PostgreSQL is used to update existing rows in one table by referring to another table based on a JOIN operation. That is extremely helpful in the relational database, especially when the tables are joined based on foreign keys.
- PostgreSQL does not support syntax like that of MySQL's UPDATE JOIN. We have to use an UPDATE statement with a subquery including the JOIN clause.
Syntax:
UPDATE table1
SET column1 = new_value
FROM table2
WHERE table1.id = table2.id;
Key Terms:
- table1: The table you want to update.
- table2: The table from which the values are joined.
- column1: The column in table1 that you want to update.
- id: The column used to match rows between table1 and table2.
Examples of PostgreSQL UPDATE JOIN
Let’s take an example where we have two tables. We want to update the customer name in the orders table based on the customer’s id from the customers table.
- orders: Contains order details.
- customers: Contains customer information.
1. Customers Table
The query creates a customers table with three columns: id
, name
, and email
, where the id
is a primary key. It then inserts three rows of data, adding customer names and email addresses for John Doe, Jane Smith, and Alice Johnson.
Query:
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
INSERT INTO customers (name, email)
VALUES
('John Doe', '[email protected]'),
('Jane Smith', '[email protected]'),
('Alice Johnson', '[email protected]');
Output:
2. Orders Table
The query creates an orders table with four columns: order_id
(a primary key ), customer_id
, customer_name
, and customer_email
.
It then inserts three rows into the orders table, assigning customer_id
values, while leaving the customer_name
and customer_email
fields as NULL
.
Query:
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INT,
customer_name VARCHAR(100),
customer_email VARCHAR(100) );
INSERT INTO orders (customer_id, customer_name, customer_email)
VALUES (1, NULL, NULL), (2, NULL, NULL), (3, NULL, NULL);
Output:
order_id | customer_id | customer_name | customer_email |
---|
1 | 1 | NULL | NULL |
2 | 2 | NULL | NULL |
3 | 3 | NULL | NULL |
Explanation:
- Table Creation: two tables are created namely, customers and orders, with extra columns named email and customer_email where all the email addresses will be stored.
- Data Insertion: Here, the data is inserted into the customers table with name and email, orders table with customer_id only; others customer_name and customer_email are initialized with NULL.
- Update Query: This UPDATE query takes an INNER JOIN of orders and customers on customer_id=orders.id and id=customers.id. It updates the customer_name and customer_email columns in orders with corresponding values from the customers table:.
Example 1 : Updating Single Column with JOIN
The UPDATE query successfully updates the customer_name in the orders table based on the name from the customers table where the customer_id matches the id in customers.
Query:
UPDATE orders
SET customer_name = customers.name
FROM customers
WHERE orders.customer_id = customers.id;
Output:
order_id | customer_id | customer_name |
---|
1 | 1 | John Doe |
2 | 2 | Jane Smith |
3 | 3 | Alice Johnson |
Explanation:
- Here, the orders table is updated with the customer_name from the customers table. It uses the FROM clause to reference the customers table.
- The orders table is updated where the customer_id in the orders table matches the id in the customers table.
- This ensures that the customer_name in the orders table will always reflect the correct name from the customers table when the IDs match.
Example 2: Updating Multiple Columns with JOIN
Now, let’s update both the customer_name
and customer_email
in the orders table based on data from the customers table.
- Modify the
orders
table to include a customer_email
field.
- Modify the
customers
table to include an email
field.
1. Adding Email Columns:
ALTER TABLE customers ADD COLUMN email VARCHAR(100);
ALTER TABLE orders ADD COLUMN customer_email VARCHAR(100);
Output:
Explanation:
- ALTER TABLE customers ADD COLUMN email VARCHAR(100); adds a column named email, to the customers table which will store an email address of up to 100 characters.
- ALTER TABLE orders ADD COLUMN customer_email VARCHAR(100); adds a column to the orders for customer_email
2. Inserting Data into Orders:
INSERT INTO orders (customer_id, customer_name, customer_email)
VALUES (1, NULL, NULL), (2, NULL, NULL), (3, NULL, NULL);
Output:
order_id | customer_id | customer_name | customer_email |
---|
1 | 1 | NULL | NULL |
2 | 2 | NULL | NULL |
3 | 3 | NULL | NULL |
Explanation:
This query inserts three rows into the orders table, setting the customer_id
values to 1, 2, and 3, while leaving the customer_name
and customer_email
fields as NULL
for all rows. The order_id
is automatically generated because it's set as a SERIAL primary key.
3. Update Query for Multiple Columns:
UPDATE orders
SET customer_name = customers.name,
customer_email = customers.email
FROM customers
WHERE orders.customer_id = customers.id;
Output:
Explanation:
- This SQL query updates the orders table using customer_name and customer_email, based on data in the customers table by joining the orders table with the customers table based on the customer_id in orders and id in customers.
- Where those IDs match, it updates the customer_name with customers.name and the customer_email with customers.email so that both fields in the orders table reflect the correct information about the customer.
Conclusion
In summary, we can update records in one table using values from another related table in PostgreSQL by using the FROM clause in an UPDATE statement. Even though PostgreSQL doesn’t have a direct UPDATE JOIN like some other databases, it can still achieve the same result using SQL joins.
This is useful when we need to keep data in multiple tables in sync or update several columns at once. Understanding how to use UPDATE JOIN is important to ensure our data stays accurate and consistent across related tables.
Similar Reads
Python PostgreSQL - Join
In this article, we are going to see join methods in PostgreSQL using pyscopg2 in Python. Let's see the type of joins supported in PostgreSQL. Types of join:Inner joinFull join (outer join)Left joinRight joinCross join Tables for demonstration: Table 1: Employee table Table 2: Dept table The psycopg
3 min read
How Inner Join works in LINQ to SQL
LINQ (Language Integrated Query) in C# provides a powerful way to query data from various data sources, including databases. In LINQ to SQL, which is specifically designed for working with relational databases, an inner join is a common operation used to combine records from two tables based on a sp
3 min read
Understanding LATERAL Joins in PostgreSQL
LATERAL joins in PostgreSQL are an advanced join technique that allows subqueries to reference columns from tables that appear earlier in the FROM clause of a query. This unique functionality makes it possible to write dynamic subqueries that rely on data from preceding tables, allowing for greater
5 min read
How to Use Full Outer Join in MySQL
The FULL OUTER JOIN or FULL JOIN in MySQL is a powerful technique to fetch all rows from both tables, including matched and unmatched records. In this article, we will explore how to use FULL OUTER JOIN with practical examples and MySQL queries. Although MySQL doesn't natively support FULL OUTER JOI
4 min read
SQL Server UPDATE JOIN
In the expansive realm of SQL Server, the UPDATE JOIN operation emerges as a potent tool for modifying data within tables by combining information from multiple sources. Unlike a traditional UPDATE statement that modifies a single table, UPDATE JOIN enables the modification of records based on condi
6 min read
How to Join to First Row in SQL Server
Joining the first row in SQL Server can be a common requirement in various scenarios, such as when we need to retrieve specific data associated with the first occurrence of a particular group or when you want to fetch additional details related to the first record in a result set. In this article, W
4 min read
PostgreSQL - SELF JOIN
In PostgreSQL, a SELF JOIN is a powerful technique that allows us to join a table with itself. This type of join is particularly useful for comparing rows within the same table, such as establishing hierarchical relationships or identifying duplicate records. Unlike other joins, there is no specific
4 min read
What is an Index in PostgreSQL?
PostgreSQL is a powerful and reliable open-source relational database management system (RDBMS) known for its extensive features, including robustness and scalability. One key feature of PostgreSQL that contributes to its high performance is indexing. Proper use of indexes can significantly improve
5 min read
PL/SQL UPDATE JOIN
In PL/SQL, an UPDATE operation with a JOIN allows you to modify records in one table based on the data retrieved from another table. This operation is crucial for maintaining the integrity and consistency of data across related tables. Using a JOIN in an UPDATE statement helps ensure that changes in
5 min read
How to Join First Row in SQL?
SQL (Structured Query Language) is a powerful tool used to manage and manipulate data in relational databases. A common requirement when dealing with data relationships is to fetch only the first row from a table based on specific conditions or criteria. This can be particularly useful when joining
6 min read