This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a simple example of UPDATE JOIN statement in PostgreSQL.
Many of the database developers are exploring the PostgreSQL so UPDATE a table from another table which is a very common requirement so I am sharing a simple example.
Create two sample tables with data:
1 2 3 4 5 |
CREATE TABLE ABC (ID INT, Name CHARACTER VARYING); CREATE TABLE XYZ (ID INT, Name CHARACTER VARYING); INSERT INTO ABC VALUES (1,'aaa'),(2,'bbb'),(3,'ccc'); INSERT INTO XYZ VALUES (1,'zzz'),(2,'yyy'),(3,'xxx'); |
UPDATE JOIN Statement:
1 2 3 4 |
UPDATE XYZ SET Name = ABC.Name FROM ABC WHERE XYZ.ID = ABC.ID; |
Check the result:
1 2 3 4 5 6 7 |
SELECT *FROM XYZ; id | name ---------------------- 1 | aaa 2 | bbb 3 | ccc |