Describe how you would implement soft deletes in a database, and write a query to fetch 'deleted' records.
Instruction: Explain the concept of soft deletes and demonstrate how to retrieve soft-deleted records from a table using a SQL query.
Updated
Official answer
A soft delete marks a row as inactive while retaining it. One pattern is a nullable deleted_at timestamp: NULL means active, and a recorded timestamp means deleted under the application's policy.
For PostgreSQL users(user_id integer, deleted_at timestamptz):
SELECT user_id, deleted_at
FROM users
WHERE deleted_at IS NOT NULL
ORDER BY deleted_at, user_id;
Ordinary active-record queries must consistently apply the opposite condition, with tenant and authorization filters still enforced. Soft-deleted data remains stored and can still be exposed through joins, exports or missed filters; this is not a privacy erasure mechanism.
Define uniqueness, child relationships, retention and restoration behavior. Restoring a row may conflict with a newer active row or changed permissions and is not always a simple timestamp reset. A suitable partial index can support a measured active-record access pattern, but indexing a low-cardinality flag is not universally beneficial. Keep audit attribution and authorized purge procedures appropriate to the data.