Skip to content

Instantly share code, notes, and snippets.

@omerkaya1
Created April 6, 2023 07:20
Show Gist options
  • Save omerkaya1/4b65fab546e5e230c54be67f64ce6d1a to your computer and use it in GitHub Desktop.
Save omerkaya1/4b65fab546e5e230c54be67f64ce6d1a to your computer and use it in GitHub Desktop.
Delete records from multiple tables in one query with CTE
WITH
first AS (
DELETE FROM table1 WHERE condition1
RETURNING *
),
second AS (
DELETE FROM table2 WHERE condition2
RETURNING *
),
-- add more analogous delete statements
deleted_counts AS (
SELECT
(SELECT COUNT(*) FROM table1) AS count_table1,
(SELECT COUNT(*) FROM table2) AS count_table2
)
SELECT * FROM deleted_counts;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment