Challenge - 5 Problems
RETURNING Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What does this INSERT with RETURNING output?
Consider the table
users(id SERIAL PRIMARY KEY, name TEXT, age INT). What will be the output of this query?INSERT INTO users (name, age) VALUES ('Alice', 30) RETURNING id, name;PostgreSQL
INSERT INTO users (name, age) VALUES ('Alice', 30) RETURNING id, name;
Attempts:
2 left
💡 Hint
RETURNING lets you get back data from the rows you just inserted.
✗ Incorrect
The RETURNING clause returns the specified columns from the newly inserted row(s). Here, it returns the new user's id and name.
❓ query_result
intermediate2:00remaining
What does this UPDATE with RETURNING output?
Given the table
products(id INT PRIMARY KEY, price NUMERIC), what will this query output?UPDATE products SET price = price * 1.1 WHERE id = 5 RETURNING id, price;
PostgreSQL
UPDATE products SET price = price * 1.1 WHERE id = 5 RETURNING id, price;
Attempts:
2 left
💡 Hint
RETURNING shows the rows that were changed.
✗ Incorrect
RETURNING returns the columns of the rows that were updated. Here, only the product with id 5 is updated and returned.
📝 Syntax
advanced2:00remaining
Which query correctly uses RETURNING with DELETE?
You want to delete a user with id 10 and get back their name. Which query is correct?
Attempts:
2 left
💡 Hint
RETURNING comes right after the WHERE clause in DELETE.
✗ Incorrect
The correct syntax places RETURNING after the WHERE clause in DELETE statements.
❓ optimization
advanced2:00remaining
Why use RETURNING instead of a separate SELECT after INSERT?
You want to insert a row and get its generated id. Why is using RETURNING better than inserting then selecting?
Attempts:
2 left
💡 Hint
Think about how many queries you send to the database.
✗ Incorrect
RETURNING lets you get data from the inserted row in the same query, avoiding extra round trips and improving speed.
🧠 Conceptual
expert2:00remaining
What happens if RETURNING is used with a statement that affects zero rows?
If you run
UPDATE users SET age = age + 1 WHERE id = 999 RETURNING id; but no user has id 999, what is the output?PostgreSQL
UPDATE users SET age = age + 1 WHERE id = 999 RETURNING id;
Attempts:
2 left
💡 Hint
Think about what happens when no rows match the WHERE condition.
✗ Incorrect
RETURNING returns rows affected by the statement. If no rows match, it returns zero rows without error.