UNION appends the rows of one query to another: SELECT … UNION SELECT …. Both queries must return the same number of columns with compatible types. Plain UNION removes duplicate rows across the combined result (a set operation); UNION ALL keeps everything, which is cheaper because no dedup pass runs. Rule of thumb: use UNION ALL unless you specifically need duplicates gone.
--how it works
1
Two queries, one shape
Each SELECT returns its own result — the column counts must match and types must be compatible row-by-row.
2
The engine stacks them
Rows of the second result are appended to the first; column names come from the first SELECT.
3
Dedup is the difference
UNION sorts/hashes the combined rows to remove duplicates; UNION ALL skips that pass and returns everything.
4
Order the combined result
ORDER BY / LIMIT after the last SELECT apply to the union as a whole — never to the individual branches.
5
Choose by intent
Need distinct options for a filter dropdown? UNION. Logging every event from two tables, duplicates allowed? UNION ALL.
Two SELECTs run: employees returns six department rows, departments returns three dept_id rows.
--example
query.sql
1SELECTdepartmentFROMemployees
2UNION
3SELECTdept_idFROMdepartments
4ORDERBYdepartment;
Result
department
Engineering
Marketing
Sales
Employees mention three departments, the departments table lists the same three — UNION collapses the nine raw rows into three distinct names. With UNION ALL you would get all nine rows back.
--variants
UNION — distinct departments
Six raw rows collapse to three distinct names.
query.sql
1SELECTdepartmentFROMemployees
2UNION
3SELECTdept_idFROMdepartments
4ORDERBYdepartment;
Result
department
Engineering
Marketing
Sales
1Employees mention Engineering ×3, Sales ×2, Marketing ×1; departments adds the same three names.
2The dedup pass (a sort or hash) keeps one row per value.
3Same result as SELECT DISTINCT over the combined rows — spelled by the operator.
UNION ALL — everything, fast
No dedup pass, so every row from both sides survives.
query.sql
1SELECTdepartmentFROMemployees
2UNIONALL
3SELECTdept_idFROMdepartments
4ORDERBYdepartment;
Result
department
Engineering
Engineering
Engineering
Engineering
Marketing
Marketing
Sales
Sales
Sales
13 + 3 department names and 3 dept_id rows — all 9 survive (duplicates included).
2The plan is a plain Append node — the cheapest set operation there is.
3When you will DISTINCT later, or duplicates are impossible by design, always pick UNION ALL.
Shape mismatch — compile error
Column counts must line up; the check happens before execution.
query.sql
1SELECTname,salaryFROMemployees
2UNION
3SELECTdept_idFROMdepartments;
Result
error
ERROR: each UNION query must have the same number of columns
1Two columns cannot stack onto one — the statement fails to plan.
2Pad with NULL or literal placeholders when a branch lacks a column: SELECT dept_id, NULL FROM departments.
3Type mismatches (date vs number) fail the same way — CAST one side to align.
Classic pattern — unified activity feed
Merge two event types into one chronological stream.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses →Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free →Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books →