Returning Modified Rows with RETURNING in PostgreSQL
📖 Scenario: You manage a small bookstore database. You want to update the prices of some books and see which books were changed immediately after the update.
🎯 Goal: Build a SQL query that updates book prices and returns the updated rows using the RETURNING clause.
📋 What You'll Learn
Create a table called books with columns id (integer), title (text), and price (numeric).
Insert three specific books with given prices.
Set a price increase threshold variable.
Write an UPDATE query that increases prices by 10% for books priced below the threshold.
Use RETURNING to get the updated rows.
💡 Why This Matters
🌍 Real World
In real databases, you often want to update data and see what changed right away without running a separate SELECT query.
💼 Career
Knowing how to use RETURNING saves time and reduces errors when working with live data in PostgreSQL.
Progress0 / 4 steps
1
Create the books table and insert data
Create a table called books with columns id (integer), title (text), and price (numeric). Then insert these three rows exactly: (1, 'Learn SQL', 25.00), (2, 'Mastering Python', 40.00), and (3, 'Data Science Basics', 30.00).
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add the rows.
2
Set the price increase threshold
Create a variable called price_threshold and set it to 35.00 to decide which books will get a price increase.
PostgreSQL
Hint
Use the \set command in psql to create a variable.
3
Write the UPDATE query with price increase
Write an UPDATE statement that increases the price by 10% for all books where the current price is less than the variable :price_threshold.
PostgreSQL
Hint
Use UPDATE with SET and a WHERE clause using the variable :price_threshold.
4
Add RETURNING clause to get updated rows
Modify the previous UPDATE query to include RETURNING id, title, price so it returns the updated rows with their new prices.
PostgreSQL
Hint
Add RETURNING id, title, price at the end of the UPDATE statement.
Practice
(1/5)
1. What does the RETURNING clause do in a PostgreSQL UPDATE statement?
easy
A. It delays the update until a commit is issued.
B. It prevents the update from modifying any rows.
C. It returns the rows that were updated immediately after the update.
D. It returns the number of rows in the table before the update.
Solution
Step 1: Understand the purpose of RETURNING
The RETURNING clause is used to get the data of rows affected by an INSERT, UPDATE, or DELETE immediately.
Step 2: Apply to UPDATE statement
When used with UPDATE, RETURNING shows the updated rows without needing a separate SELECT query.
Final Answer:
It returns the rows that were updated immediately after the update. -> Option C
Quick Check:
RETURNING shows changed rows = B [OK]
Hint: RETURNING shows changed rows right after modification [OK]
Common Mistakes:
Thinking RETURNING delays changes
Assuming RETURNING prevents updates
Confusing RETURNING with row count only
2. Which of the following is the correct syntax to return all columns of updated rows in PostgreSQL?
easy
A. UPDATE table_name SET col = val RETURNING ALL COLUMNS;
B. UPDATE table_name SET col = val RETURNING *;
C. UPDATE table_name SET col = val RETURNING ALL;
D. UPDATE table_name SET col = val RETURN *;
Solution
Step 1: Recall correct RETURNING syntax
PostgreSQL uses RETURNING followed by column names or * to return all columns.
Step 2: Identify correct option
Only UPDATE table_name SET col = val RETURNING *; uses RETURNING * which is valid syntax to return all columns.
Final Answer:
UPDATE table_name SET col = val RETURNING *; -> Option B
Quick Check:
RETURNING * returns all columns [OK]
Hint: Use RETURNING * to get all columns after update [OK]
Common Mistakes:
Using RETURN instead of RETURNING
Writing RETURNING ALL instead of RETURNING *
Adding extra keywords like ALL COLUMNS
3. Given the table users(id INT, name TEXT, age INT) with rows: (1, 'Alice', 30), (2, 'Bob', 25) What will this query return?
UPDATE users SET age = age + 1 WHERE id = 2 RETURNING id, age;
medium
A. (2, 26)
B. (2, 25)
C. (1, 31)
D. (1, 30)
Solution
Step 1: Identify updated row
The WHERE clause updates only the row with id = 2 (Bob).
Step 2: Calculate new age and returned columns
Bob's age increases from 25 to 26. RETURNING id, age returns (2, 26).
UPDATE products SET price = price * 1.1 RETURNING;
medium
A. RETURNING cannot be used with UPDATE statements.
B. The multiplication operator * is invalid in UPDATE.
C. Missing WHERE clause causes syntax error.
D. RETURNING clause requires columns or * to specify what to return.
Solution
Step 1: Check RETURNING syntax
RETURNING must be followed by column names or *; empty RETURNING is invalid syntax.
Step 2: Validate other parts
Multiplication and UPDATE without WHERE are valid; RETURNING with no columns is the error.
Final Answer:
RETURNING clause requires columns or * to specify what to return. -> Option D
Quick Check:
RETURNING needs columns or * [OK]
Hint: Always specify columns or * after RETURNING [OK]
Common Mistakes:
Leaving RETURNING empty
Thinking WHERE is mandatory for syntax
Believing RETURNING disallowed in UPDATE
5. You want to increase the salary by 10% for employees in department 5 and get their id and new salary. Which query correctly does this and returns the updated rows?
hard
A. UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary;
B. UPDATE employees SET salary = salary + 10 WHERE department_id = 5 RETURNING *;
C. UPDATE employees SET salary = salary * 1.1 RETURNING id, salary WHERE department_id = 5;
D. UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURN id, salary;
Solution
Step 1: Identify correct UPDATE with WHERE and RETURNING
The query must update only department 5 employees and return id and salary.
Step 2: Check syntax correctness
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary; uses correct syntax: UPDATE ... SET ... WHERE ... RETURNING id, salary;
Step 3: Eliminate incorrect options
Adding a fixed 10 instead of multiplying by 1.1 doesn't implement a 10% increase. Placing RETURNING before WHERE is invalid syntax. Using RETURN instead of RETURNING is wrong.
Final Answer:
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5 RETURNING id, salary; -> Option A
Quick Check:
Correct syntax with WHERE and RETURNING columns [OK]
Hint: RETURNING follows WHERE clause with columns listed [OK]
Common Mistakes:
Placing RETURNING before WHERE
Using RETURN instead of RETURNING
Adding fixed amount instead of percentage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.