DELETE rows in SQL
DELETE removes whole rows; there is no undo outside a transaction.
DELETE FROM sessions
WHERE expires_at < CURRENT_TIMESTAMP
AND user_id IN (
SELECT id
FROM users
WHERE deleted_at IS NOT NULL
);
How it works
- Run the same
WHEREas aSELECTfirst to see what will go. - A subquery can scope the delete to rows found elsewhere.
- Deleting by primary key is the safest form.
Keywords and builtins used here
ANDCURRENT_TIMESTAMPDELETEFROMINISNOTNULLSELECTWHERE
The run, in numbers
- Lines
- 7
- Characters to type
- 127
- Tokens
- 22
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 16 seconds.
Step 7 of 8 in Tables & rows, step 20 of 23 in Language basics.