Joins in SQL: Types, Syntax, Examples & Use Cases
Estimated study time: 12 minutes. Every join type in one place, with syntax and examples.
Joins combine rows from two or more tables based on a related column. SQL Server supports several join types, each suited to a different question about your data.
INNER JOIN
Returns only rows with a match in both tables.
SELECT e.EmpName, d.DeptName FROM Employees e INNER JOIN Departments d ON e.DeptID = d.DeptID;
LEFT JOIN
Returns all rows from the left table, matched or not.
SELECT e.EmpName, d.DeptName FROM Employees e LEFT JOIN Departments d ON e.DeptID = d.DeptID;
RIGHT JOIN
Returns all rows from the right table, matched or not.
SELECT e.EmpName, d.DeptName FROM Employees e RIGHT JOIN Departments d ON e.DeptID = d.DeptID;
FULL OUTER JOIN
Returns all rows from both tables, filling in NULLs where there's no match on either side.
SELECT e.EmpName, d.DeptName FROM Employees e FULL OUTER JOIN Departments d ON e.DeptID = d.DeptID;
SELF JOIN
Joins a table to itself, useful for hierarchical or comparative data like manager-employee relationships.
SELECT a.EmpName AS Employee, b.EmpName AS Manager FROM Employees a INNER JOIN Employees b ON a.ManagerID = b.EmpID;
CROSS JOIN
Returns the Cartesian product — every row from the first table paired with every row from the second.
SELECT Colors.Color, Sizes.Size FROM Colors CROSS JOIN Sizes;
Seeing the Results on Sample Data
Join types are easiest to understand when you can see the rows they return. Use these two small tables:
| EmpID | EmpName | DeptID |
|---|---|---|
| 1 | Asha | 10 |
| 2 | Ravi | 20 |
| 3 | Neha | NULL |
| 4 | Karan | 10 |
| DeptID | DeptName |
|---|---|
| 10 | Sales |
| 20 | IT |
| 30 | HR |
- INNER JOIN returns 3 rows: Asha–Sales, Ravi–IT, Karan–Sales. Neha has no department and HR has no employees, so both are left out.
- LEFT JOIN returns 4 rows: the three above plus Neha with a NULL department.
- RIGHT JOIN returns 4 rows: the three matches plus HR with a NULL employee name.
- FULL OUTER JOIN returns 5 rows: every match, Neha, and HR.
Finding Rows With No Match
A very common real task is "find records that have nothing linked to them", such as departments without staff or customers who never ordered. Use a LEFT JOIN and keep only the rows where the right side is NULL:
SELECT d.DeptName
FROM Departments d
LEFT JOIN Employees e ON e.DeptID = d.DeptID
WHERE e.EmpID IS NULL; -- returns HR
NOT EXISTS gives the same result and many people find it easier to read.
FULL OUTER JOIN in MySQL
SQL Server and PostgreSQL support FULL OUTER JOIN directly, but MySQL does not. You can produce the same result by combining a LEFT JOIN with the unmatched rows of a RIGHT JOIN:
SELECT e.EmpName, d.DeptName
FROM Employees e
LEFT JOIN Departments d ON e.DeptID = d.DeptID
UNION ALL
SELECT e.EmpName, d.DeptName
FROM Employees e
RIGHT JOIN Departments d ON e.DeptID = d.DeptID
WHERE e.EmpID IS NULL;
The WHERE in the second query keeps only departments with no employees, so matched rows are not listed twice. Using plain UNION also removes duplicates, but it can wrongly merge genuine duplicate rows.
The ON Versus WHERE Trap in Outer Joins
With an INNER JOIN, a filter gives the same result in ON or WHERE. With a LEFT JOIN it does not:
-- Keeps all 4 employees; DeptName is filled only for Sales staff
SELECT e.EmpName, d.DeptName
FROM Employees e
LEFT JOIN Departments d
ON e.DeptID = d.DeptID AND d.DeptName = 'Sales';
-- Returns only Asha and Karan: the WHERE removes the NULL rows
SELECT e.EmpName, d.DeptName
FROM Employees e
LEFT JOIN Departments d ON e.DeptID = d.DeptID
WHERE d.DeptName = 'Sales';
If you filter the right-hand table in WHERE, your LEFT JOIN quietly behaves like an INNER JOIN.
Other mistakes worth knowing
- Unexpected duplicates: joining a customer to many orders repeats the customer on every row. Aggregate first or use
DISTINCTcarefully. - Forgetting the join condition: leaving out ON in some databases produces a cross join with a huge number of rows.
- Slow joins: index the columns used in ON, usually the foreign key columns.
- Ambiguous names: always prefix columns with a table alias when both tables share a column name.