β‘ Key Constraint: both queries must be union-compatible β same column count, compatible types; output names come from the first query.
π§ Minimal Working Example
SELECT drone_id FROM drone.drone -- set 1: all dronesMINUSSELECT drone_id FROM drone.rental -- set 2: drones ever rentedORDER BY drone_id; -- result: drones NEVER rented
Expected output:drone_ids in set 1 but not set 2 β the never-rented drones.
UNION ALL β all rows from both, keeps duplicates.
UNION β all rows from both, duplicates removed.
INTERSECT β rows appearing in both queries.
MINUS β rows in the first query not in the second.
Union-compatible β equal column count + compatible types; a single trailing ORDER BY applies to the whole result.
π Variations
Operator
Returns
Duplicates
UNION ALL
everything from both
kept
UNION
everything from both
removed
INTERSECT
rows in both
n/a
MINUS
in first, not second
n/a
UNION with labels β combine WHERE emp_type='F' (label 'Full Time') with WHERE emp_type='C' (label 'Casual'), then ORDER BY emp_no.
INTERSECT β SELECT emp_lname FROM drone.employee INTERSECT SELECT cust_lname FROM drone.customer β surnames in both.
βοΈ Practice
Practice 1: List surnames that appear in bothEMPLOYEE (emp_lname) and CUSTOMER (cust_lname), ordered.
Reference solution
SELECT emp_lname FROM drone.employeeINTERSECTSELECT cust_lname FROM drone.customerORDER BY emp_lname;
Key move: INTERSECT keeps the overlap; both sides are single, type-compatible columns.
β οΈ Common Mistakes
π‘ Not union-compatible = error β mismatched column counts or incompatible types are rejected; align the SELECT lists.
π‘ One ORDER BY, at the very end β ordering belongs to the combined result, using the first queryβs column names.