SQL Server → PostgreSQL

How to Convert DELETE with JOIN from SQL Server to PostgreSQL

PostgreSQL does not allow JOIN directly in DELETE. Learn USING, NOT EXISTS, and how to find every breaking point in your script.

You ran DELETE t1 FROM Table1 t1 JOIN Table2... on PostgreSQL and got a syntax error. That is expected: T-SQL allows aliases and JOIN directly in DELETE; PostgreSQL does not.

The practical rule: in Postgres use DELETE ... USING other_tables or rewrite with a subquery / NOT EXISTS when the JOIN is LEFT and you filter orphan rows.

Before (SQL Server)

DELETE t
FROM Items t
INNER JOIN Orders p ON t.OrderId = p.Id
WHERE p.Status = 'CANCELLED';

After (PostgreSQL — USING)

DELETE FROM items t
USING orders p
WHERE t.order_id = p.id
  AND p.status = 'CANCELLED';

DELETE with LEFT JOIN

When the filter is IS NULL on the right side (anti-join), USING does not reproduce the same semantics. The safe pattern is NOT EXISTS.

SQL Server

DELETE t FROM Items t
LEFT JOIN Stock e ON t.Id = e.ItemId
WHERE e.ItemId IS NULL;

PostgreSQL

DELETE FROM items t
WHERE NOT EXISTS (
  SELECT 1 FROM stock e WHERE e.item_id = t.id
);

Analisador de Impacto

Have more JOIN-heavy queries to convert? Paste your script into the Impact Analyzer and see where else your code will break.

Abrir Análise de Projeto →