0
0
SQLquery~20 mins

Soft delete pattern concept in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Soft Delete Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Identify active records with soft delete
Given a table users with columns id, name, and deleted_at (which is NULL if the record is active), which query returns only active users?
SQL
SELECT * FROM users WHERE deleted_at IS NULL;
ASELECT * FROM users WHERE deleted_at = '2023-01-01';
BSELECT * FROM users WHERE deleted_at IS NOT NULL;
CSELECT * FROM users WHERE deleted_at IS NULL;
DSELECT * FROM users WHERE deleted_at = '';
Attempts:
2 left
💡 Hint
Think about how soft delete marks records as deleted by setting a timestamp.
🧠 Conceptual
intermediate
2:00remaining
Purpose of soft delete timestamp
Why do we use a deleted_at timestamp column in soft delete instead of just deleting the record?
ATo keep a record of when the item was deleted and allow recovery if needed.
BTo encrypt the record data for security.
CTo speed up queries by indexing the deletion time.
DTo permanently remove the record from the database immediately.
Attempts:
2 left
💡 Hint
Think about why we might want to keep deleted data around.
📝 Syntax
advanced
2:00remaining
Correct SQL to soft delete a record
Which SQL statement correctly soft deletes a user with id = 5 by setting deleted_at to the current timestamp?
AINSERT INTO users (deleted_at) VALUES (CURRENT_TIMESTAMP) WHERE id = 5;
BDELETE FROM users WHERE id = 5;
CUPDATE users SET deleted_at = NULL WHERE id = 5;
DUPDATE users SET deleted_at = CURRENT_TIMESTAMP WHERE id = 5;
Attempts:
2 left
💡 Hint
Soft delete means updating the timestamp, not deleting the row.
optimization
advanced
2:00remaining
Optimizing queries with soft delete
To improve performance of queries filtering active records (deleted_at IS NULL), which index is best to add?
ACreate an index on the <code>id</code> column only.
BCreate an index on the <code>deleted_at</code> column.
CCreate a full-text index on the <code>name</code> column.
DCreate no index; rely on sequential scans.
Attempts:
2 left
💡 Hint
Think about which column is used in the WHERE clause to filter active records.
🔧 Debug
expert
2:00remaining
Why does this soft delete query fail to exclude deleted records?
Consider this query to select active users:
SELECT * FROM users WHERE deleted_at = NULL;
Why does it not return any rows even if some users are active?
ABecause comparing with NULL using '=' always returns false; use IS NULL instead.
BBecause the table has no rows at all.
CBecause the query syntax is invalid and causes an error.
DBecause the <code>deleted_at</code> column has no NULL values.
Attempts:
2 left
💡 Hint
Remember how SQL treats NULL comparisons.