Combining Data: JOINs vs SET
The two ways to combine data in SQL — JOINs add columns side by side, SET operators stack rows — plus what a JOIN is, why we join, and the 'No JOIN' baseline.
Real data is never in one table. In our IPL database, players knows that Virat Kohli plays for RCB, but only teams knows that RCB means Royal Challengers Bengaluru from Bengaluru. To answer "Which city does each player play for?" you must combine the two tables. SQL gives you exactly two tools for this.
The two methods of combining data
Combining data means producing one result set from two or more tables (or queries). SQL does it in two directions: JOINs combine columns (tables placed side by side), and SET operators combine rows (query results stacked on top of each other).
| JOINs | SET operators | |
|---|---|---|
| Direction | Horizontal — A | B (side by side) | Vertical — A / B (stacked) |
| What grows | the number of columns | the number of rows |
| Needs | a key column to match rows (e.g. team) | the same number of columns with compatible types |
| Keywords | INNER, LEFT, RIGHT, FULL, CROSS JOIN | UNION, UNION ALL, EXCEPT, INTERSECT |
| IPL question | "Show each player with his team's full name and city" | "Show batters and bowlers in one list" |
A simple way to remember: JOIN = wider result, SET = longer result.
What is a SQL JOIN?
A JOIN is an operation in the
FROMclause that combines rows from two tables into one wider row, whenever a join condition (written afterON) is true — usually when a key column in one table equals a key column in the other.
- Key column — a column whose values identify the same "thing" in both tables. Here it is the team code:
players.teamandteams.team. - Join condition — the rule that decides which rows belong together:
ON p.team = t.team. - Combined row — the columns of the player row plus the columns of the matching team row, in one line.
Example — combine players with teams
sql
SELECT p.name, p.team, t.team_name, t.city
FROM players AS p
INNER JOIN teams AS t
ON p.team = t.team;| name | team | team_name | city |
|---|---|---|---|
| Virat Kohli | RCB | Royal Challengers Bengaluru | Bengaluru |
| Rohit Sharma | MI | Mumbai Indians | Mumbai |
| Jos Buttler | RR | Rajasthan Royals | Jaipur |
| MS Dhoni | CSK | Chennai Super Kings | Chennai |
| David Warner | DC | Delhi Capitals | Delhi |
| Jasprit Bumrah | MI | Mumbai Indians | Mumbai |
How it works: for every player, SQL looks into teams for the row whose team equals the player's team, and glues the two rows together. p and t are table aliases — short nicknames so we can write p.name instead of players.name.
Why do we join tables? (three reasons)
| # | Reason | What it means | IPL example | Typical join |
|---|---|---|---|---|
| 1 | Recombine data | Data was split into several tables (normalisation); put the big picture back together. | players + teams + cities in ONE result | INNER / LEFT / FULL |
| 2 | Data enrichment | Keep your main table and pull extra info from a reference / lookup table. | add team_name to each player | LEFT JOIN |
| 3 | Check for existence | Use the other table as a filter: keep (or drop) rows depending on whether a partner exists. | "Does this player have a valid team?" | INNER, or LEFT + WHERE ... IS NULL |
Join possibilities — which rows do you want?
Think of two overlapping circles: A (players) on the left, B (teams) on the right. The overlap is the rows that match.
| You want… | Venn picture in words | Join to use |
|---|---|---|
| Matching data — rows found in both tables | only the overlap | INNER JOIN |
| All data — everything from A (and from B where it matches) | the whole left circle (or both circles) | LEFT / RIGHT / FULL JOIN |
| Unmatching data — rows with no partner in the other table | the circle minus the overlap | ANTI JOINs (LEFT / RIGHT / FULL) |
No JOIN — when you don't need to combine
No JOIN means querying each table separately:
SELECT * FROM A; SELECT * FROM B;. You get two independent result sets, nothing is matched.
sql
SELECT * FROM players;
SELECT * FROM teams;The first returns all 6 players (A), the second all 7 teams (B). Use this when you just want to look at the tables, or when the two results answer two different questions. Don't join "just because" — every join costs work and can change row counts.
Note: Before writing any join, always look at both tables separately first (No JOIN). Knowing that teams has 7 rows and players has 6 helps you sanity-check every join result.
Common mistakes
- ❌ Thinking UNION "joins" tables. ✅ UNION stacks rows; it never matches on a key.
- ❌ Joining on columns that only look related (e.g.
p.name = t.team_name). ✅ Join on a real key that means the same thing in both tables. - ❌ Forgetting the alias prefix when both tables have a column with the same name (
team) — SQL Server raises "Ambiguous column name 'team'". ✅ Writep.teamort.team.
Interview tip
Q: What is the difference between a JOIN and a UNION? A: A JOIN combines columns from two tables by matching rows on a condition, so the result gets wider. A UNION combines the rows of two queries with the same column structure, so the result gets longer — no matching condition is involved.
Key points to remember
- There are two ways to combine data: JOINs combine columns (side by side), SET operators combine rows (stacked).
- A JOIN matches rows of two tables using a join condition after ON, usually key = key (p.team = t.team).
- We join for three reasons: recombine split data, enrich rows with lookup info, and check whether a partner row exists.
- Join possibilities: only matching rows (INNER), all rows of one/both sides (LEFT/RIGHT/FULL), or only unmatched rows (ANTI joins).
- 'No JOIN' simply means running SELECT on each table separately — two results, no combining.
- Use table aliases (p, t) and prefix columns to avoid 'Ambiguous column name' errors.
Try it yourself
No JOIN: look at both tables separately
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Your first JOIN: player + team name + city
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
JOIN (columns) vs UNION (rows) side by side
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: add the city to each player
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.
