Created
April 6, 2023 07:20
-
-
Save omerkaya1/4b65fab546e5e230c54be67f64ce6d1a to your computer and use it in GitHub Desktop.
Delete records from multiple tables in one query with CTE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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