0
0
PostgreSQLquery~20 mins

Returning modified rows with RETURNING in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Returning Rows Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What rows are returned by this UPDATE with RETURNING?
Consider a table employees with columns id, name, and salary. The table has these rows:

(1, 'Alice', 5000), (2, 'Bob', 6000), (3, 'Carol', 7000)

What rows will be returned by this query?

UPDATE employees SET salary = salary + 500 WHERE salary < 6500 RETURNING id, salary;
A(1, 5500), (2, 6500)
B(1, 5500), (2, 6500), (3, 7000)
C(3, 7000)
DNo rows returned
Attempts:
2 left
💡 Hint
RETURNING returns only rows that were updated.
query_result
intermediate
2:00remaining
What does this DELETE with RETURNING output?
Given a table products with columns product_id and stock containing:

(101, 10), (102, 0), (103, 5)

What rows will be returned by this query?

DELETE FROM products WHERE stock = 0 RETURNING product_id;
A(102)
B(101), (103)
C(101), (102), (103)
DNo rows returned
Attempts:
2 left
💡 Hint
RETURNING returns deleted rows matching the WHERE condition.
📝 Syntax
advanced
2:00remaining
Which UPDATE with RETURNING syntax is correct?
You want to increase the price by 10 for all rows in items and return the item_id and new price. Which query is syntactically correct?
AUPDATE items SET price = price + 10 RETURNING * FROM items;
BUPDATE items SET price = price + 10 RETURN item_id, price;
CUPDATE items SET price = price + 10 RETURNING (item_id, price);
DUPDATE items SET price = price + 10 RETURNING item_id, price;
Attempts:
2 left
💡 Hint
RETURNING must be followed by column names without parentheses or extra keywords.
query_result
advanced
2:00remaining
What is the output of this INSERT with RETURNING?
Given a table orders with columns order_id (serial primary key), customer, and amount, currently empty.

What will this query return?

INSERT INTO orders (customer, amount) VALUES ('John', 100), ('Jane', 150) RETURNING order_id, amount;
A(NULL, 100), (NULL, 150)
B(0, 100), (1, 150)
C(1, 100), (2, 150)
DNo rows returned
Attempts:
2 left
💡 Hint
RETURNING returns inserted rows including generated serial keys.
🧠 Conceptual
expert
2:00remaining
Why use RETURNING in data modification queries?
Which is the main advantage of using RETURNING in UPDATE, DELETE, or INSERT statements in PostgreSQL?
AIt speeds up the query execution by skipping transaction logging.
BIt allows retrieving the affected rows immediately without a separate SELECT query.
CIt automatically commits the transaction after the modification.
DIt prevents other users from accessing the modified rows.
Attempts:
2 left
💡 Hint
Think about how to get data about changed rows efficiently.