An INNER JOIN returns only the rows that have matching values in both tables.

Non-matching rows from either table are excluded.

Basic syntax:

SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
  • 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:

SELECT e.FirstName, e.LastName, d.DepartmentName
FROM Employees AS e
INNER JOIN Departments AS d
ON e.DepartmentID = d.DepartmentID;

Result:

FirstName LastName DepartmentName
John Doe IT
Jane Smith HR
Mike Brown IT

Sara is excluded because her DepartmentID is NULL.
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.
SELECT e.FirstName, d.DepartmentName
FROM Employees e
INNER JOIN Departments d
ON e.DepartmentID = d.DepartmentID;
  • 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:

SELECT e.FirstName, d.DepartmentName
FROM Employees e
INNER JOIN Departments d
ON e.DepartmentID = d.DepartmentID
AND e.EmployeeID > 1;

Result:

FirstName DepartmentName
Jane HR
Mike IT

Only employees with EmployeeID > 1 are included.

Step 5: INNER JOIN with WHERE clause

You can filter the results further using WHERE:

SELECT e.FirstName, d.DepartmentName
FROM Employees e
INNER JOIN Departments d
ON e.DepartmentID = d.DepartmentID
WHERE d.DepartmentName = 'IT';

Result:

FirstName DepartmentName
John IT
Mike IT

Step 6: INNER JOIN with multiple tables

You can join more than two tables:

SELECT e.FirstName, e.LastName, d.DepartmentName, s.Salary
FROM Employees e
INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID
INNER JOIN Salaries s ON e.EmployeeID = s.EmployeeID;

Returns only rows where all join conditions are met across all tables.

Categorized in:

SQL Server,