Challenge - 5 Problems
DELETE with RETURNING Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of DELETE with RETURNING on single row
Given the table
employees with columns id, name, and department, what will be the output of this query?DELETE FROM employees WHERE id = 3 RETURNING name;
Attempts:
2 left
💡 Hint
The RETURNING clause returns the deleted rows matching the condition.
✗ Incorrect
The query deletes the employee with id 3 and returns the name of that employee. If the employee with id 3 is John, the output is [{"name": "John"}].
📝 Syntax
intermediate2:00remaining
Identify the syntax error in DELETE with RETURNING
Which option contains a syntax error in the DELETE statement with RETURNING clause?
Attempts:
2 left
💡 Hint
Check the DELETE statement syntax carefully.
✗ Incorrect
Option B is missing the FROM keyword after DELETE, which causes a syntax error.
❓ optimization
advanced2:00remaining
Optimizing DELETE with RETURNING for large tables
You want to delete all rows from
logs older than 30 days and return their log_id. Which query is the most efficient?Attempts:
2 left
💡 Hint
Consider how many times the table is scanned.
✗ Incorrect
Option C deletes and returns the deleted rows in one operation, scanning the table only once. Option C scans twice, C has syntax error, and D does not return deleted rows.
🔧 Debug
advanced2:00remaining
Why does this DELETE with RETURNING return no rows?
Given the query:
It returns an empty result set, but you know some products have price > 100. What is the most likely reason?
DELETE FROM products WHERE price > 100 RETURNING *;
It returns an empty result set, but you know some products have price > 100. What is the most likely reason?
Attempts:
2 left
💡 Hint
Check the WHERE condition carefully.
✗ Incorrect
If no rows match the WHERE condition, DELETE deletes nothing and RETURNING returns an empty set.
🧠 Conceptual
expert2:00remaining
Understanding DELETE with RETURNING and transaction behavior
In PostgreSQL, if you run:
What happens to the rows and what is the output of the DELETE statement?
BEGIN;
DELETE FROM users WHERE active = false RETURNING id;
ROLLBACK;
What happens to the rows and what is the output of the DELETE statement?
Attempts:
2 left
💡 Hint
Think about how transactions and rollback work in PostgreSQL.
✗ Incorrect
The DELETE returns the ids of rows it would delete, but since the transaction is rolled back, no rows are actually removed from the table.