0
0
SQLquery~10 mins

Soft delete pattern concept in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Soft delete pattern concept
Start: User wants to delete a record
Update record's 'deleted' flag to TRUE
Record remains in table but marked deleted
Queries filter out records where 'deleted' is TRUE
Deleted records hidden from normal views
Optionally, restore by setting 'deleted' to FALSE
Soft delete means marking a record as deleted instead of removing it, so it stays in the table but is hidden from normal queries.
Execution Sample
SQL
UPDATE users
SET deleted = TRUE
WHERE id = 3;

SELECT * FROM users
WHERE deleted = FALSE;
Marks user with id=3 as deleted, then selects only users not marked deleted.
Execution Table
StepActionSQL ExecutedTable StateOutput
1Mark user id=3 as deletedUPDATE users SET deleted = TRUE WHERE id = 3;User 3: deleted=TRUE; others deleted=FALSENo output (update)
2Select users not deletedSELECT * FROM users WHERE deleted = FALSE;Same as aboveReturns all users except user 3
3Attempt to select all users without filterSELECT * FROM users;Same as aboveReturns all users including deleted ones
4Restore user id=3UPDATE users SET deleted = FALSE WHERE id = 3;User 3: deleted=FALSE; others deleted=FALSENo output (update)
5Select users not deleted after restoreSELECT * FROM users WHERE deleted = FALSE;Same as aboveReturns all users including user 3
💡 Execution stops after restoring user and selecting active users.
Variable Tracker
VariableStartAfter Step 1After Step 4Final
deleted flag for user 3FALSETRUEFALSEFALSE
Key Moments - 3 Insights
Why doesn't the DELETE statement remove the record?
Because soft delete uses UPDATE to set a 'deleted' flag instead of DELETE, so the record stays in the table but is marked as deleted (see execution_table step 1).
How do queries exclude deleted records?
By adding WHERE deleted = FALSE in SELECT queries, only records not marked deleted are returned (see execution_table step 2).
Can a deleted record be restored?
Yes, by updating the 'deleted' flag back to FALSE, the record becomes active again (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the 'deleted' flag value for user 3 after step 1?
ATRUE
BFALSE
CNULL
DUNKNOWN
💡 Hint
Check the 'Table State' column in execution_table row for step 1.
At which step does the SELECT query return all users including the deleted one?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Output' column in execution_table rows for steps 2 and 3.
If we forget to add WHERE deleted = FALSE in SELECT, what happens?
AQuery returns no records
BDeleted records are included in results
CQuery fails with error
DOnly deleted records are returned
💡 Hint
See execution_table step 3 where SELECT without filter returns all records.
Concept Snapshot
Soft delete means marking a record as deleted using a flag (e.g., deleted=TRUE) instead of removing it.
Use UPDATE to set the flag.
SELECT queries filter out deleted records with WHERE deleted=FALSE.
Deleted records remain in the table and can be restored by resetting the flag.
This preserves data and allows easy recovery.
Full Transcript
Soft delete pattern means when you want to delete a record, you don't remove it from the database. Instead, you mark it as deleted by setting a special flag, like a 'deleted' column to TRUE. This way, the record stays in the table but is hidden from normal queries. When you select data, you add a condition to only get records where deleted is FALSE. If you want to restore a deleted record, you just set the flag back to FALSE. This pattern helps keep data safe and recoverable. The example shows updating user id 3 to deleted=TRUE, then selecting only users not deleted, so user 3 is hidden. Later, user 3 is restored by setting deleted=FALSE again.