An INNER JOIN returns only the rows that have matching values in both tables.
Non-matching rows from either table are excluded.
Basic syntax:
- table1.common_column and table2.common_column are the columns used to match rows.
- You can use table aliases for simplicity.
Step 1: Example tables
Employees Table
| EmployeeID | FirstName | LastName | DepartmentID |
|---|---|---|---|
| 1 | John | Doe | 101 |
| 2 | Jane | Smith | 102 |
| 3 | Mike | Brown | 101 |
| 4 | Sara | White | NULL |
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 101 | IT |
| 102 | HR |
| 103 | Finance |
Step 2: Simple INNER JOIN
Retrieve employees with their department names:
Result:
| FirstName | LastName | DepartmentName |
|---|---|---|
| John | Doe | IT |
| Jane | Smith | HR |
| Mike | Brown | IT |
Sara is excluded because her
DepartmentIDisNULL.
Finance department is excluded because no employee belongs to it.
Step 3: INNER JOIN with table aliases
- Aliases make queries shorter and easier to read.
- e → Employees
- d → Departments
Result: Same as above.
Step 4: INNER JOIN with multiple columns
If the join condition depends on more than one column, use AND:
Result:
| FirstName | DepartmentName |
|---|---|
| Jane | HR |
| Mike | IT |
Only employees with
EmployeeID > 1are included.
Step 5: INNER JOIN with WHERE clause
You can filter the results further using WHERE:
Result:
| FirstName | DepartmentName |
|---|---|
| John | IT |
| Mike | IT |
Step 6: INNER JOIN with multiple tables
You can join more than two tables:
Returns only rows where all join conditions are met across all tables.
