Set operations and FILTER
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
combine result sets and compute several conditional aggregates in one pass.
Planned topics
UNIONremoves duplicates;UNION ALLkeeps them and is faster. Default toUNION ALL.INTERSECTandEXCEPTfind common and missing rows; both sides need matching columns.count(*) FILTER (WHERE status = 'PAID')replaces repeatedCASEexpressions.- One scan can produce many metrics.
Draft outline details
Demonstration
SELECT count(*) AS orders,
count(*) FILTER (WHERE status = 'PAID') AS paid,
count(*) FILTER (WHERE status = 'CANCELLED') AS cancelled
FROM orders;Check your understanding
Which is faster, UNION or UNION ALL, and why?
Show answer
UNION ALL; it skips duplicate removal.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.