DELETE with RETURNING clause in PostgreSQL - Time & Space Complexity
When we delete rows from a table and ask the database to return the deleted rows, it takes some time to find and remove those rows.
We want to understand how the time needed grows as the number of rows to delete grows.
Analyze the time complexity of the following code snippet.
DELETE FROM employees
WHERE department_id = 5
RETURNING employee_id, name;
This code deletes all employees in department 5 and returns their IDs and names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the table or index to find rows matching the condition.
- How many times: Once for each row in the table or index scanned.
As the number of rows to delete grows, the database must check more rows and delete more data.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks and deletes |
| 100 | About 100 row checks and deletes |
| 1000 | About 1000 row checks and deletes |
Pattern observation: The work grows roughly in direct proportion to the number of rows deleted.
Time Complexity: O(n)
This means the time to delete and return rows grows linearly with how many rows match the condition.
[X] Wrong: "Deleting rows with RETURNING is instant no matter how many rows match."
[OK] Correct: The database must find and remove each matching row and prepare the returned data, so more rows mean more work and more time.
Understanding how DELETE with RETURNING scales helps you explain database behavior clearly and shows you think about efficiency in real tasks.
"What if we added an index on department_id? How would the time complexity change?"
Practice
RETURNING clause do in a DELETE statement in PostgreSQL?Solution
Step 1: Understand DELETE operation
The DELETE statement removes rows from a table based on a condition.Step 2: Role of RETURNING clause
The RETURNING clause outputs the rows that were deleted, showing their data immediately.Final Answer:
It returns the rows that were deleted. -> Option DQuick Check:
RETURNING shows deleted rows [OK]
- Thinking RETURNING prevents deletion
- Confusing RETURNING with UPDATE
- Assuming RETURNING counts rows
users where age < 18 and return all deleted rows?Solution
Step 1: Check DELETE syntax
The correct DELETE syntax is: DELETE FROM table WHERE condition.Step 2: Use RETURNING clause correctly
RETURNING * must come after WHERE clause to return all columns of deleted rows.Final Answer:
DELETE FROM users WHERE age < 18 RETURNING *; -> Option AQuick Check:
Correct DELETE syntax with RETURNING * [OK]
- Omitting FROM keyword
- Placing RETURNING before WHERE
- Using RETURNING all instead of *
products with rows:{id:1, name:'Pen', stock:10}, {id:2, name:'Pencil', stock:0}, {id:3, name:'Eraser', stock:5}What will this query return?
DELETE FROM products WHERE stock = 0 RETURNING id, name;
Solution
Step 1: Identify rows matching condition
Only the product with stock = 0 is {id:2, name:'Pencil'}.Step 2: DELETE removes matching rows and RETURNING outputs specified columns
The query deletes that row and returns its id and name.Final Answer:
[{id:2, name:'Pencil'}] -> Option CQuick Check:
Deleted rows with stock=0 returned [OK]
- Returning all rows instead of matching ones
- Expecting deleted rows to remain in table
- Confusing syntax causing errors
DELETE FROM orders RETURNING * WHERE status = 'cancelled';
Solution
Step 1: Check clause order in DELETE
In PostgreSQL, WHERE clause must come before RETURNING clause.Step 2: Identify error in query
The query places RETURNING * before WHERE, which is invalid syntax.Final Answer:
The WHERE clause must come before RETURNING. -> Option AQuick Check:
WHERE before RETURNING in DELETE [OK]
- Placing RETURNING before WHERE
- Thinking RETURNING is disallowed
- Ignoring clause order rules
staff who have salary < 30000 and return their id and name. Which query correctly does this and also returns the count of deleted rows?Solution
Step 1: Delete rows with condition and return columns
Using DELETE ... WHERE salary < 30000 RETURNING id, name returns deleted rows.Step 2: Use WITH clause to capture deleted rows and count them
WITH deleted AS (...) lets us select COUNT(*) from deleted rows in one query.Final Answer:
WITH deleted AS (DELETE FROM staff WHERE salary < 30000 RETURNING id, name) SELECT COUNT(*) FROM deleted; -> Option BQuick Check:
Use WITH + RETURNING to delete and count [OK]
- Trying to use COUNT(*) inside RETURNING
- Placing WHERE after RETURNING
- Separating DELETE and count without capturing deleted rows
Ready to prove it?
Pass the test and this pattern moves unknown
