Sub topic 1
Combining result sets: the idea behind set operators
Prerequisites
You should be able to write basic SELECT queries with WHERE, understand tables, rows, and columns, and use DISTINCT and ORDER BY. You should also know that NULL represents a missing or unknown value.
The working model
A SQL query returns a result set. A result set is a temporary collection of rows produced by a query. A set operator combines the result sets from two or more SELECT statements into one result.
Think of each query as producing a list. A set operator describes how to combine those lists:
UNIONcombines rows from both queries and removes duplicate rows.UNION ALLcombines rows from both queries and keeps duplicates.INTERSECTkeeps rows found in both results.EXCEPTkeeps rows found in the first result but not the second.
These operators work vertically. They add or compare rows. This differs from a JOIN, which usually combines columns from related rows side by side.
SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers;The first query produces customer emails. The second produces subscriber emails. UNION creates one result containing each distinct email found in either result.
Combining compatible queries
The queries connected by a set operator must be compatible. They must return:
- The same number of columns.
- Corresponding columns in the same order.
- Data types that the database can combine or compare.
The column names in the final result usually come from the first SELECT. The names in the second query do not rename those output columns.
For example, these queries return one column each, so they are compatible:
SELECT customer_id FROM current_customers
UNION
SELECT customer_id FROM former_customers;These queries are not compatible because the first query returns two columns and the second returns one:
SELECT customer_id, email FROM customers
UNION
SELECT customer_id FROM newsletter_subscribers;The database may also reject incompatible data types, such as combining a date column with a value that cannot be interpreted as a date.
Why set operators are useful
Set operators are useful when the data sources have the same shape but represent different groups or conditions. For example, you might combine current and archived records, compare subscribed and unsubscribed users, or find IDs shared by two reports.
The order of the inputs matters for EXCEPT. This query finds newsletter subscribers who are not in the unsubscribe list:
SELECT email FROM newsletter_subscribers
EXCEPT
SELECT email FROM unsubscribed;Reversing the queries finds unsubscribed addresses that do not appear among subscribers. The operator is therefore directional.
Visual model
A common error is to think that UNION matches rows by a key, like a join. It does not. It compares complete rows across corresponding columns. If two rows have different values in any output column, they are different rows for duplicate handling.
Choosing the right operation
Use UNION ALL when every occurrence matters, such as when combining event logs. Use UNION when repeated identical rows should appear once. Use INTERSECT to find overlap, and EXCEPT to find the difference from the first result.
The later chapters examine duplicate handling, NULL, ordering, parentheses, and alternatives such as JOIN and EXISTS.
Exercise
Which set operator should you use to return every distinct product ID found in either the online or store sales result?
Show answer
Use UNION, provided both queries return one compatible product ID column.