0
0
PostgreSQLquery~20 mins

DELETE with RETURNING clause in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DELETE with RETURNING Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"name": "John"}]
B[]
C[{"name": "Alice"}]
D[{"id": 3, "name": "John", "department": "Sales"}]
Attempts:
2 left
💡 Hint
The RETURNING clause returns the deleted rows matching the condition.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in DELETE with RETURNING
Which option contains a syntax error in the DELETE statement with RETURNING clause?
ADELETE FROM orders WHERE order_id = 10 RETURNING *;
BDELETE orders WHERE order_id = 10 RETURNING *;
CDELETE FROM orders WHERE order_id = 10 RETURNING order_id;
DDELETE FROM orders WHERE order_id = 10 RETURNING order_date;
Attempts:
2 left
💡 Hint
Check the DELETE statement syntax carefully.
optimization
advanced
2: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?
ADELETE FROM logs RETURNING log_id WHERE log_date < NOW() - INTERVAL '30 days';
BSELECT log_id FROM logs WHERE log_date < NOW() - INTERVAL '30 days'; DELETE FROM logs WHERE log_date < NOW() - INTERVAL '30 days';
CDELETE FROM logs WHERE log_date < NOW() - INTERVAL '30 days' RETURNING log_id;
DDELETE FROM logs WHERE log_date < NOW() - INTERVAL '30 days';
Attempts:
2 left
💡 Hint
Consider how many times the table is scanned.
🔧 Debug
advanced
2:00remaining
Why does this DELETE with RETURNING return no rows?
Given the query:
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?
AThe query deletes rows but does not return any columns.
BRETURNING clause is not supported in DELETE statements.
CThe table <code>products</code> is empty.
DThe WHERE condition is incorrect or does not match any rows.
Attempts:
2 left
💡 Hint
Check the WHERE condition carefully.
🧠 Conceptual
expert
2:00remaining
Understanding DELETE with RETURNING and transaction behavior
In PostgreSQL, if you run:
BEGIN;
DELETE FROM users WHERE active = false RETURNING id;
ROLLBACK;

What happens to the rows and what is the output of the DELETE statement?
AThe DELETE returns the ids of deleted rows, but no rows are actually removed after rollback.
BThe DELETE returns no rows because the transaction is rolled back immediately.
CThe DELETE deletes rows permanently and returns their ids despite rollback.
DThe DELETE causes an error because RETURNING cannot be used in transactions.
Attempts:
2 left
💡 Hint
Think about how transactions and rollback work in PostgreSQL.