INNER JOIN
Return only the rows that have a match in both tables — the most common join.
"Show me every player together with his team's full name." Only players whose team really exists in the teams table can be shown with a team name — that is exactly what INNER JOIN does.
Definition
INNER JOIN returns only the rows where the join condition is true in both tables. Rows from either table that have no matching partner are discarded.
- Only matching rows — in Venn terms, just the overlap of circle A and circle B.
- Both tables — a row survives only if it finds at least one partner on the other side.
- Discarded — unmatched rows simply disappear; they don't show up with NULLs (that's what outer joins are for).
- If one row matches several rows on the other side, you get one result row per match (MI appears twice below because MI has two players).
Syntax
sql
SELECT a.column_name, b.column_name
FROM table_a AS a
INNER JOIN table_b AS b
ON a.key_column = b.key_column;| Part | Meaning |
|---|---|
FROM table_a AS a | the first (left) table and its alias |
INNER JOIN table_b AS b | the second (right) table to combine with |
ON a.key = b.key | the join condition — which rows belong together |
INNER | optional: plain JOIN means INNER JOIN in SQL Server |
Example — players with their team names
sql
SELECT p.name, p.team, t.team AS t_team, t.team_name
FROM players AS p
INNER JOIN teams AS t
ON p.team = t.team;| name | team | t_team | team_name |
|---|---|---|---|
| Virat Kohli | RCB | RCB | Royal Challengers Bengaluru |
| Rohit Sharma | MI | MI | Mumbai Indians |
| Jos Buttler | RR | RR | Rajasthan Royals |
| MS Dhoni | CSK | CSK | Chennai Super Kings |
| David Warner | DC | DC | Delhi Capitals |
| Jasprit Bumrah | MI | MI | Mumbai Indians |
How it works:
FROM players— take each player row.INNER JOIN teams ON p.team = t.team— find the team row(s) with the same code.- KKR and GT have no players, so they find no partner and are dropped.
SELECT— pick the columns to show.
So 7 teams and 6 players produce 6 rows: every player matched, the 2 lonely teams vanished.
Order of tables does NOT matter
players INNER JOIN teams and teams INNER JOIN players return the same rows (only the column order you write in SELECT changes, and the display order of rows is not guaranteed without ORDER BY). The overlap of A and B is the same as the overlap of B and A.
Example — INNER JOIN as an existence check + filter
sql
SELECT p.name, p.runs, t.city
FROM players AS p
INNER JOIN teams AS t
ON p.team = t.team
WHERE t.city = 'Mumbai';| name | runs | city |
|---|---|---|
| Rohit Sharma | 500 | Mumbai |
| Jasprit Bumrah | 20 | Mumbai |
Execution order: FROM + JOIN ... ON build the combined rows → WHERE filters them → SELECT picks columns. You can filter on columns from either table after joining.
Example — what happens to rows without a partner
To see INNER JOIN drop players, we need players whose team is not in teams. The playground examples below add two such players in their setup:
| id | name | team | why it's special |
|---|---|---|---|
| 7 | Shubman Gill | GT2 | typo — no team called GT2 exists |
| 8 | Ishan Kishan | NULL | team not known yet |
Running the same INNER JOIN still returns only the 6 original players. Gill and Kishan are discarded because 'GT2' matches nothing and NULL never equals anything — not even another NULL.
Common mistakes
- ❌ Expecting INNER JOIN to keep everyone from the first table. ✅ It keeps only matched rows; use LEFT JOIN to keep all of A.
- ❌ Forgetting
ON→ SQL Server error "Incorrect syntax near ';'". ✅ Every INNER/LEFT/RIGHT/FULL JOIN needs anONcondition. - ❌ Joining on a non-unique column and being surprised by duplicate rows. ✅ Know which side is "one" and which is "many" (one team → many players).
- ❌ Old comma style
FROM players, teams WHERE p.team = t.team. ✅ It works, but forgetting the WHERE silently becomes a CROSS JOIN. Prefer explicitINNER JOIN ... ON.
Interview tip
Q: If table A has 6 rows and table B has 7, how many rows can an INNER JOIN return? A: Anywhere from 0 (no matches) upward — it is not capped at 6 or 7. If keys repeat on both sides, each match produces a row, so it can be as large as 6 × 7 = 42 in the extreme case.
Key points to remember
- INNER JOIN returns only rows where the ON condition is true in both tables — the Venn overlap.
- Unmatched rows from either table are discarded; they never appear with NULLs.
- JOIN without a keyword means INNER JOIN in SQL Server.
- The order of tables does not matter for INNER JOIN — same rows either way.
- NULL keys never match (NULL = NULL is not true), so rows with a NULL key are dropped.
- One-to-many keys repeat the 'one' side: MI appears once per MI player.
- Execution order: FROM/JOIN → WHERE → SELECT → ORDER BY.
Try it yourself
INNER JOIN players with teams
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Unmatched players are dropped (setup adds GT2 and NULL players)
This example runs a small setup step first — click to see it
INSERT INTO players (id, name, team, country, runs) VALUES (7, 'Shubman Gill', 'GT2', 'India', 600), (8, 'Ishan Kishan', NULL, 'India', 450);
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Order of tables does not matter
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: Indian players with their team names
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.
