INNER JOIN
The INNER JOIN is the most commonly used join in SQL. It returns only the rows where there is a matching value in both tables.
If a row does not have a match in either table, it will not appear in the result.
What Does INNER JOIN Do?
INNER JOIN compares rows from two tables and returns only the rows that satisfy the join condition.
In simple words:
- Match rows from table A and table B
- Return only the matching records
- Ignore non-matching rows
Example Tables
employees
+----+----------+---------------+--------+ | id | name | department_id | salary | +----+----------+---------------+--------+ | 1 | Alice | 101 | 75000 | | 2 | Bob | 102 | 72000 | | 3 | Charlie | 101 | 60000 | | 4 | Diana | 104 | 80000 | +----+----------+---------------+--------+
departments
+---------------+----------------+ | department_id | department_name| +---------------+----------------+ | 101 | IT | | 102 | HR | | 103 | Finance | +---------------+----------------+
INNER JOIN Syntax
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
The ON condition defines how the rows are matched.
INNER JOIN Example
Retrieve employee names along with their department names.
SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
Result explanation:
- Alice → IT
- Bob → HR
- Charlie → IT
Diana is excluded because her department_id (104) does not exist in the departments table.
Why Some Rows Are Missing
INNER JOIN returns rows only when:
- The join column exists in both tables
- The values match exactly
If there is no match, the row is ignored. This behavior is intentional.
INNER JOIN with WHERE
INNER JOIN can be combined with WHERE for additional filtering.
Example: IT department employees only.
SELECT e.name, d.department_name, e.salary FROM employees e INNER JOIN departments d ON e.department_id = d.department_id WHERE d.department_name = 'IT';
INNER JOIN with Multiple Conditions
You can add conditions inside WHERE or directly in the JOIN.
ON e.department_id = d.department_id AND e.salary > 70000
This limits matches during the join itself.
Using Table Aliases (Best Practice)
Table aliases improve readability and reduce repetition.
employees edepartments d
Aliases are strongly recommended when using joins.
INNER JOIN vs WHERE Join (Old Style)
Older SQL syntax used WHERE for joins.
❌ Old style (not recommended):
SELECT e.name, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id;
✅ Modern style (recommended):
SELECT e.name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
Common Beginner Mistakes
- Forgetting the ON condition
- Joining unrelated columns
- Expecting unmatched rows to appear
- Confusing INNER JOIN with LEFT JOIN
When to Use INNER JOIN
Use INNER JOIN when:
- You only want matching records
- Unmatched data is irrelevant
- You need clean, consistent results
What’s Next?
In the next lesson, we will explore LEFT JOIN and see how it differs from INNER JOIN.