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