typestar

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

  1. Run the same WHERE as a SELECT first to see what will go.
  2. A subquery can scope the delete to rows found elsewhere.
  3. Deleting by primary key is the safest form.

Keywords and builtins used here

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.

Type this snippet

Step 7 of 8 in Tables & rows, step 20 of 23 in Language basics.

← Previous Next →