EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL UPDATE JOIN
 

PostgreSQL UPDATE JOIN

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 12, 2023

PostgreSQL UPDATE JOIN

 

 

Introduction to PostgreSQL UPDATE JOIN

We can update the records stored within the table in various ways; PostgreSQL provides UPDATE JOIN to do the same. In some cases, we need to update one table’s records based on another table’s records. To join another table in the statement, we have to define the PostgreSQL FROM clause with the joined table and specify the PostgreSQL WHERE clause with a JOIN condition. We must add the SET clause and specify the PostgreSQL FROM clause immediately after it.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

Consider the following syntax:

UPDATE table1
SET table1.col1 = expression
FROM table2
WHERE table1.col2 = table2.col2;

Explanation: Join another table in the statement using the PostgreSQL FROM clause. Add the SET clause and specify the PostgreSQL FROM clause immediately after it.

How UPDATE JOIN works in PostgreSQL?

Consider the syntax defined in the above section to understand the working of the PostgreSQL UPDATE JOIN. As per the syntax, we are updating the values of the table1 by using the values from the table2. Here we have specified a JOIN condition on col2 of table1 and table2. So if every row in table1 and table2 contains matching/same value, then the UPDATE statement updates the col1 column value in table1 and sets the value per the expression defined in the set clause.

Examples

Below are some examples mentioned:

Example #1

To understand the basic example, we will create two tables of name ‘table1’ and ‘table2’ by using the CREATE TABLE statement as follows:

Code:

CREATE TABLE table1
(
t_ID INT,
t_Name VARCHAR(80)
);
CREATE TABLE table2
(
t_ID INT,
t_Name VARCHAR(80)
);

We will insert some data into the table1 table using the INSERT INTO statement as follows.

INSERT INTO table1 VALUES
(1,'DDD'),
(2,'EEE'),
(3,'FFF');

Output:

select * from table1;

Insert Into statement

Now, we will insert some data into the table2 table using the INSERT INTO statement.

INSERT INTO table2 VALUES
(1,'PPP'),
(2,'QQQ'),
(3,'RRR');

Illustrate the result of the above INSERT INTO statement by using the following SQL statement and snapshot.

select * from table2;

Table 2

Now we will use the PostgreSQL UPDATE JOIN Statement to update the values of table2 if the t_ID field is matching/same with the table2.

UPDATE table2
SET t_Name = table1.t_Name
FROM table1
WHERE table1.t_ID = table2.t_ID;

Illustrate the result of the above statement by using the following SQL statement and snapshot.

select * from table2;

PostgreSQL UPDATE Join3

Example #2

We will create two tables of name ‘student’ and ‘department’ by using the CREATE TABLE statement as follows to understand the examples:

Code:

create table student
(
stud_id serial PRIMARY KEY,
stud_fname VARCHAR(80) NOT NULL,
stud_lname VARCHAR(80) NOT NULL,
stud_total_marks int NOT NULL,
department_id int NOT NULL
);
create table department
(
department_id serial PRIMARY KEY,
department_name VARCHAR(80) NOT NULL
);

We will insert some data into the department table using the INSERT INTO statement as follows.

INSERT INTO department(department_name)
VALUES
('Computer'),
('Electrical'),
('IT'),
('Civil'),
('Chemical'),
('Mechanical');

Output:

select * from department;

PostgreSQL UPDATE Join4

We will insert some data into the student table using the INSERT INTO statement as follows.

INSERT INTO student(stud_fname, stud_lname, stud_total_marks, department_id)
VALUES
('Smith','Johnson',576,1),
('Williams','Jones',678,1),
('Harper','James',876,2),
('Jack','Liam',786,2),
('Harry','Mason',879,3),
('Jacob','Oscar',765,3),
('Michael','Charlie',878,4),
('William','Joe',787,4),
('Oliver','John',676,5),
('Jack','Richard',686,5),
('Harry','Joseph',696,5),
('George','Thomas',797,6),
('Brown','Charles',785,6);

Illustrate the result of the above INSERT INTO statement by using the following SQL statement and snapshot.

select * from student;

PostgreSQL UPDATE Join5

Now consider the example where we have to give extra 20 marks to each student except the Civil department, whose department_id is 4. So we will use the UPDATE JOIN statement as follows:

UPDATE student
SET stud_total_marks = stud_total_marks + 20
FROM
department
WHERE
student.department_id = department.department_id AND department.department_id <> 4;

Illustrate the result of the above statement by using the following SQL statement and snapshot.

select * from student;

PostgreSQL UPDATE Join6

Conclusion

From the above article, we hope you understand how to use the PostgreSQL UPDATE JOIN and how the PostgreSQL UPDATE JOIN works. Also, we have added several examples of PostgreSQL UPDATE JOIN to understand it in detail.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL UPDATE JOIN” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL NOT NULL
  2. PostgreSQL Timestamp
  3. Foreign Key in PostgreSQL
  4. PostgreSQL Subquery

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW