SQL

DELETE with JOIN / USING

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Delete rows from one table conditional on a related table. PostgreSQL uses `USING`; MySQL uses join syntax in the DELETE. Run as a SELECT first to verify what you're about to remove.
SQL
Raw
-- ALWAYS preview with SELECT first — DELETE is one-way
SELECT o.*
FROM   orders o
JOIN   users u ON u.id = o.user_id
WHERE  u.status = 'banned';

-- PostgreSQL — DELETE ... USING
DELETE FROM orders o
USING  users u
WHERE  u.id = o.user_id
   AND u.status = 'banned';

-- MySQL — DELETE table FROM table JOIN table
DELETE o
FROM   orders o
JOIN   users u ON u.id = o.user_id
WHERE  u.status = 'banned';

-- Delete with subquery (works everywhere)
DELETE FROM orders
WHERE  user_id IN (SELECT id FROM users WHERE status = 'banned');

-- Limit how many rows you delete (MySQL has DELETE ... LIMIT N)
DELETE FROM events WHERE created_at < NOW() - INTERVAL 90 DAY LIMIT 10000;
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.