SET Operators: Syntax & Rules
What SET operators are, their syntax, and the five rules every UNION / EXCEPT / INTERSECT query must follow.
The batters table and the bowlers table have the same shape: name, team. To get one list of all players, you don't need a key or a join — you just stack one result under the other. That's what SET operators do.
Definition
SET operators combine the results of two or more SELECT queries into a single result set by stacking their rows. The queries must return the same number of columns with compatible data types.
| Operator | Returns | Venn picture in words |
|---|---|---|
| UNION | all distinct rows from both queries | both circles, overlap counted once |
| UNION ALL | all rows, duplicates included | both circles, overlap counted twice |
| EXCEPT (MINUS) | distinct rows in the 1st query not in the 2nd | left circle minus the overlap |
| INTERSECT | distinct rows found in both queries | only the overlap |
Unlike joins, SET operators compare whole rows (all columns together), not a single key.
Syntax
sql
SELECT name, team FROM batters WHERE team = 'MI' -- 1st SELECT statement
UNION -- SET operator
SELECT name, team FROM bowlers WHERE team = 'MI' -- 2nd SELECT statement
ORDER BY name; -- only ONCE, at the very end| name | team |
|---|---|
| Hardik Pandya | MI |
| Jasprit Bumrah | MI |
| Rohit Sharma | MI |
Each SELECT can have its own WHERE, JOIN, GROUP BY, HAVING. The single ORDER BY sorts the combined result.
The 5 SET rules
| # | Rule | Example / explanation |
|---|---|---|
| 1 | SET operators can be used with almost any clause in each query | each SELECT may use WHERE, JOIN, GROUP BY, HAVING |
| 2 | ORDER BY is allowed only once, at the end of the whole query | it sorts the final combined result |
| 3 | Each query must have the same number of columns | name, team + name, team ✅ ; name, team + name ❌ |
| 4 | Column data types must be compatible | text with text, numbers with numbers |
| 5 | Column names come from the first query | aliases in the 2nd query are ignored |
Rule 3 in action — different column counts
sql
SELECT name, team FROM batters
UNION
SELECT name FROM bowlers;SQL Server: "All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists." The query fails.
Rule 4 — compatible types, and the hidden trap
SELECT runs FROM players UNION SELECT name FROM batters fails in SQL Server: it tries to convert 'Virat Kohli' to INT ("Conversion failed when converting the varchar value…").
But matching types do not guarantee matching meaning:
sql
SELECT name FROM batters
UNION
SELECT team FROM bowlers;This runs without error and mixes player names with team codes (CSK, GT, MI…) in one column — garbage. SQL checks position and type, never meaning. Always list columns in the same order in every query.
Rule 5 — names come from the first query
sql
SELECT name AS player, team FROM batters
UNION
SELECT name, team FROM bowlers
ORDER BY player;The result column is called player (from the 1st query), and ORDER BY must use that name.
Using a SET operator inside another clause
A set query can be a subquery, e.g. in WHERE:
sql
SELECT name, team, runs
FROM players
WHERE name IN (SELECT name FROM batters
UNION
SELECT name FROM bowlers)
ORDER BY runs DESC;| name | team | runs |
|---|---|---|
| Virat Kohli | RCB | 750 |
| Rohit Sharma | MI | 500 |
| MS Dhoni | CSK | 350 |
| Jasprit Bumrah | MI | 20 |
Common mistakes
- ❌
ORDER BYafter the first SELECT → SQL Server error. ✅ OneORDER BYat the very end. - ❌ Columns in a different order (
name, teamvsteam, name). ✅ Same order in every query. - ❌ Using
SELECT *on both sides — works only while both tables have identical columns. ✅ List columns explicitly.
SQL Server vs MySQL / PostgreSQL / Oracle
| Operator | SQL Server | PostgreSQL | MySQL | Oracle |
|---|---|---|---|---|
| UNION / UNION ALL | ✅ | ✅ | ✅ | ✅ |
| EXCEPT | ✅ | ✅ | ✅ (8.0.31+) | uses MINUS |
| INTERSECT | ✅ | ✅ | ✅ (8.0.31+) | ✅ |
Interview tip
Q: Where does the column name of a UNION result come from? A: From the first SELECT. Aliases in later queries are ignored, and ORDER BY must use the first query's names.
Key points to remember
- SET operators stack the rows of two or more SELECT results into one result set.
- UNION = distinct rows from both; UNION ALL = all rows; EXCEPT = in first not in second; INTERSECT = in both.
- Each query must have the same number of columns, with compatible data types, in the same order.
- ORDER BY is allowed only once — at the very end of the whole query.
- Column names of the result come from the first query.
- SQL checks column position and type, not meaning — mismatched columns can silently produce garbage.
- SET operators compare entire rows, not a single key column.
Try it yourself
Syntax: two SELECTs + UNION + one ORDER BY
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Rule 5: names come from the first query
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
SET operator inside a WHERE subquery
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: break and fix rule 3
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
