Postgres SQL cheat sheet¶
Official document
https://www.postgresqltutorial.com/postgresql-select/
Nice diagram from dunwu
Union , Except , Intersect¶
Union¶
-- Return only 1 row for duplicate data
SELECT * FROM top_rated_films
UNION
SELECT * FROM most_popular_films;
-- with all duplicate data
SELECT * FROM top_rated_films
UNION ALL
SELECT * FROM most_popular_films;
Except¶
Rows area not in table 2
SELECT * FROM table1
EXCEPT
SELECT * FROM table2;
Intersect¶
SELECT select_list
FROM A
INTERSECT
SELECT select_list
FROM B;