EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL UPDATE JOIN

PostgreSQL UPDATE JOIN

Sohel Sayyad
Article bySohel Sayyad
Priya Pedamkar
Reviewed byPriya Pedamkar

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
MICROSOFT POWER BI Course Bundle - 8 Courses in 1
34+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Course Bundle - 13 Courses in 1 | 3 Mock Tests
64+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Course Bundle - 6 Courses in 1
20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Course Bundle - 5 Courses in 1 | 1 Mock Test
 15+ Hour of HD Videos
5 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more