kuda.ai

Thoughts on programming and music theory.

dark mode

Postgres: delete from t using jt

Created on April 4, 2026

postgres

DELETE FROM consumptions c
USING activities a
WHERE c.activity_id = a.id
  AND c.activity_id = $1
  AND a.invoice_id IS NULL;

similar, but maybe easier to understand:

DELETE FROM consumptions
WHERE activity_id = $1
  AND activity_id IN (
    SELECT id
    FROM activities
    WHERE invoice_id IS NULL
  );