Challenge - 5 Problems
Returning Rows Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What rows are returned by this UPDATE with RETURNING?
Consider a table
What rows will be returned by this query?
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;
Attempts:
2 left
💡 Hint
RETURNING returns only rows that were updated.
✗ Incorrect
Only employees with salary less than 6500 are updated. Alice (5000) and Bob (6000) get their salary increased by 500. Carol (7000) is not updated, so not returned.
❓ query_result
intermediate2:00remaining
What does this DELETE with RETURNING output?
Given a table
What rows will be returned by this query?
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;
Attempts:
2 left
💡 Hint
RETURNING returns deleted rows matching the WHERE condition.
✗ Incorrect
Only the product with stock 0 is deleted and returned. Others remain.
📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
RETURNING must be followed by column names without parentheses or extra keywords.
✗ Incorrect
Option D uses correct syntax: RETURNING column list without parentheses or FROM clause. Others are invalid syntax.
❓ query_result
advanced2:00remaining
What is the output of this INSERT with RETURNING?
Given a table
What will this query return?
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;Attempts:
2 left
💡 Hint
RETURNING returns inserted rows including generated serial keys.
✗ Incorrect
The serial
order_id starts at 1 and increments. The query inserts two rows and returns their order_id and amount.🧠 Conceptual
expert2:00remaining
Why use RETURNING in data modification queries?
Which is the main advantage of using
RETURNING in UPDATE, DELETE, or INSERT statements in PostgreSQL?Attempts:
2 left
💡 Hint
Think about how to get data about changed rows efficiently.
✗ Incorrect
RETURNING lets you get the changed rows' data right away, avoiding extra queries. It does not affect transaction control or locking.