If you've ever worked with databases, you've almost certainly encountered situations where you need to retrieve data from multiple tables at once. That's where SQL JOIN clauses come in. At first, the different types of joins can seem confusing, but they are a fundamental concept for anyone working with relational databases.
In this guide, we'll break down the most common types of SQL joins—INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN—using simple examples and visual aids to make the concepts crystal clear.
Related: Learn about SQL Common Table Expressions for advanced query techniques.
To understand joins, we need some data to work with. Let's imagine we have a small company with two tables: Employees and Departments.
Our Employees table stores information about each employee, including which department they belong to. Notice that some employees might not be assigned to a department yet (DepartmentID is NULL).
Employees Table:
| EmployeeID | Name | DepartmentID |
|---|---|---|
| 1 | Alice | 101 |
| 2 | Bob | 102 |
| 3 | Charlie | 101 |
| 4 | David | NULL |
Our Departments table lists the available departments. Notice that the "Marketing" department has no employees assigned to it yet.
Departments Table:
| DepartmentID | DepartmentName |
|---|---|
| 101 | Engineering |
| 102 | HR |
| 103 | Marketing |
Now, let's use these tables to explore the different types of joins.
An INNER JOIN returns only the rows where the join condition is met in both tables. It's the most common type of join. Think of it as finding the intersection of two sets.
Goal: Get a list of all employees who are assigned to a department.
SELECT E.Name, D.DepartmentName FROM Employees AS E INNER JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | Engineering |
| Bob | HR |
| Charlie | Engineering |
Explanation:
DepartmentID exists in the Departments table.DepartmentID is NULL and doesn't match any department.DepartmentID 103.A LEFT JOIN returns all rows from the left table (Employees in our case) and the matched rows from the right table (Departments). If there is no match in the right table, NULL is returned for the columns from the right table.
Goal: Get a list of all employees and their department, even if they aren't assigned to one yet.
SELECT E.Name, D.DepartmentName FROM Employees AS E LEFT JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | Engineering |
| Bob | HR |
| Charlie | Engineering |
| David | NULL |
Explanation:
Employees table are included in the result.NULL DepartmentID, is included, but his DepartmentName is NULL because there was no match in the Departments table.A RIGHT JOIN is the opposite of a LEFT JOIN. It returns all rows from the right table (Departments) and the matched rows from the left table (Employees). If there is no match in the left table, NULL is returned for the columns from the left table.
Goal: Get a list of all departments and the employees in them, including departments that have no employees.
SELECT E.Name, D.DepartmentName FROM Employees AS E RIGHT JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | Engineering |
| Charlie | Engineering |
| Bob | HR |
| NULL | Marketing |
Explanation:
Departments table are included.Name is NULL because no employee is assigned to it.Note: You can achieve the same result as a
RIGHT JOINby swapping the tables and using aLEFT JOIN. Many developers prefer to useLEFT JOINexclusively for consistency.
A FULL OUTER JOIN returns all rows when there is a match in either the left or the right table. It combines the functionality of both LEFT JOIN and RIGHT JOIN. If there's no match, the respective columns will be NULL.
Goal: Get a complete list of all employees and all departments, matching them where possible.
SELECT E.Name, D.DepartmentName FROM Employees AS E FULL OUTER JOIN Departments AS D ON E.DepartmentID = D.DepartmentID;
Result:
| Name | DepartmentName |
|---|---|
| Alice | Engineering |
| Bob | HR |
| Charlie | Engineering |
| David | NULL |
| NULL | Marketing |
Explanation:
NULL department name (like in the LEFT JOIN).NULL employee name (like in the RIGHT JOIN).Note: Some database systems like MySQL do not support
FULL OUTER JOINdirectly. You can emulate it by combining aLEFT JOINand aRIGHT JOINwith aUNION.
Understanding SQL JOINs is essential for effectively querying relational databases. Here's a quick summary:
INNER JOIN: Returns only the matching rows from both tables.LEFT JOIN: Returns all rows from the left table, and matched rows from the right table.RIGHT JOIN: Returns all rows from the right table, and matched rows from the left table.FULL OUTER JOIN: Returns all rows from both tables, matching them where possible.By choosing the right join for your needs, you can combine data from multiple tables in powerful and flexible ways. Happy querying!
Master SQL Common Table Expressions (CTEs) with this practical guide. Learn how to simplify complex queries, write recursive queries, and improve readability through real-world examples.
Learn the differences between offset and cursor-based pagination, their pros and cons, and how to implement both in Spring Boot applications with search capabilities.

Get instant AI-powered summaries of YouTube videos and websites. Save time while enhancing your learning experience.