Inner Join in SQL: How It Works
Estimated study time: 6 minutes. The most commonly used join in SQL.
An INNER JOIN combines rows from two tables based on a related column, returning only the rows where a match exists in both tables. It's the join you'll reach for most often.
Basic Syntax
SELECT columns FROM TableA INNER JOIN TableB ON TableA.key = TableB.key;
A Practical Example
SELECT Employees.EmpName, Departments.DeptName FROM Employees INNER JOIN Departments ON Employees.DeptID = Departments.DeptID;
This returns every employee that has a matching department — an employee with a DeptID that doesn't exist in Departments (or is NULL) is left out entirely.
How It Actually Works
Conceptually, SQL Server compares every row of the first table against every row of the second table (though the actual execution plan is usually far smarter than a brute-force loop), keeping only pairs where the join condition evaluates to true.
Joining More Than Two Tables
You can chain multiple INNER JOINs to pull data from several related tables at once:
SELECT e.EmpName, d.DeptName, p.ProjectName FROM Employees e INNER JOIN Departments d ON e.DeptID = d.DeptID INNER JOIN Projects p ON e.EmpID = p.LeadEmpID;
JOIN in SQL Server is treated exactly the same as INNER JOIN.INNER JOIN vs Other Joins
Unlike a LEFT JOIN or RIGHT JOIN, an INNER JOIN never fills in NULLs for unmatched rows — it simply excludes them from the result. If you need unmatched rows preserved, an outer join is the right tool instead.
Seeing the Result Row by Row
Small sample tables make it obvious which rows survive an inner join.
Employees Departments
+-------+--------+--------+ +--------+-----------+
| EmpID | EmpName| DeptID | | DeptID | DeptName |
+-------+--------+--------+ +--------+-----------+
| 1 | Asha | 10 | | 10 | Sales |
| 2 | Ravi | 20 | | 20 | Finance |
| 3 | Meena | 10 | | 30 | Legal |
| 4 | Karan | NULL | +--------+-----------+
| 5 | Sara | 40 |
+-------+--------+--------+
Result of the join on DeptID
+---------+----------+
| EmpName | DeptName |
+---------+----------+
| Asha | Sales |
| Ravi | Finance |
| Meena | Sales |
+---------+----------+
Karan disappears because NULL never equals anything, not even another NULL. Sara disappears because department 40 does not exist. Legal disappears because nobody works there. Sales appears twice because two employees belong to it; an inner join returns one row per matching pair.
Useful Patterns in MySQL
Counting with GROUP BY
SELECT d.DeptName, COUNT(*) AS headcount
FROM Employees e
INNER JOIN Departments d ON e.DeptID = d.DeptID
GROUP BY d.DeptName
ORDER BY headcount DESC;
Departments with no employees are missing from this report. If you need Legal to show a count of zero, switch to a LEFT JOIN from Departments and count e.EmpID instead.
Joining on more than one column
SELECT o.OrderID, p.Price
FROM OrderItems o
INNER JOIN PriceList p
ON o.ProductID = p.ProductID
AND o.Region = p.Region;
USING and self joins
When both columns share a name, MySQL accepts JOIN Departments USING (DeptID) as a shorter form. You can also join a table to itself, for example to pair each employee with a manager stored in the same table, by giving it two aliases such as e and m.
ON Versus WHERE, and Checking Performance
For an inner join, a filter placed in ON or in WHERE gives the same rows. Keeping join conditions in ON and filters in WHERE simply makes the query easier to read. With outer joins the placement does change the result, so the habit pays off later.
On large tables, make sure the join columns are indexed. Prefix the query with EXPLAIN to see whether MySQL uses an index or scans the whole table:
CREATE INDEX idx_employees_dept ON Employees (DeptID);
EXPLAIN
SELECT e.EmpName, d.DeptName
FROM Employees e
INNER JOIN Departments d ON e.DeptID = d.DeptID;
Mistakes That Produce Wrong Results
- Forgetting the ON clause in old comma syntax such as
FROM Employees, Departments. Without a condition, every row pairs with every other row, a cross join. - Unexpected duplicates from one-to-many relationships. Joining orders to order items and then summing order totals counts each total several times.
- Ambiguous column names. If both tables have
DeptID, writee.DeptIDin the SELECT list or MySQL raises an error. - Mismatched data types between join columns, such as text versus integer, which can slow the query and give surprising matches.
- Losing rows silently. Before trusting a report, compare its row count with the source table to confirm no records were dropped by the join.