SQL Joins Guide
SQL joins are essential when combining data from multiple tables in a relational database. It is one of the popular SQL topics asked in Data Science Interviews. So, if you want to learn about SQL joins in detail, this article is for you. In this article, I’ll take you through a guide to SQL joins.
SQL Joins Guide
SQL Joins are used to combine rows from two or more tables based on a related column between them. The most common types of SQL joins are:
- INNER JOIN
- LEFT JOIN (LEFT OUTER JOIN)
- RIGHT JOIN (RIGHT OUTER JOIN)
- FULL JOIN (FULL OUTER JOIN)
- CROSS JOIN
Let’s understand all these SQL joins step by step. To understand all these joins, we will be using the tables shown below.
| EmployeeID | EmployeeName | DepartmentID |
|---|---|---|
| 1 | John | 1 |
| 2 | Sarah | 2 |
| 3 | Mike | 1 |
| 4 | Linda | NULL |
| DepartmentID | DepartmentName |
|---|---|
| 1 | HR |
| 2 | IT |
| 3 | Marketing |
Now, let’s go through all types of SQL joins.
INNER JOIN
An INNER JOIN returns records that have matching values in both tables.
Here’s an implementation of INNER JOIN on our example tables:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
| EmployeeName | DepartmentName |
|---|---|
| John | HR |
| Sarah | IT |
| Mike | HR |
Only the rows with matching DepartmentID in both tables are included in the result.
LEFT JOIN (LEFT OUTER JOIN)
A LEFT JOIN returns all records from the left table (Employees), and the matched records from the right table (Departments). If there is no match, the result is NULL from the right side.
Here’s an implementation of LEFT JOIN on our example tables:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
| EmployeeName | DepartmentName |
|---|---|
| John | HR |
| Sarah | IT |
| Mike | HR |
| Linda | NULL |
All records from the Employees table are returned, and for Linda, there is no corresponding DepartmentID, so DepartmentName is NULL.
RIGHT JOIN (RIGHT OUTER JOIN)
A RIGHT JOIN returns all records from the right table (Departments), and the matched records from the left table (Employees). If there is no match, the result is NULL from the left side.
Here’s an implementation of RIGHT JOIN on our example tables:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
| EmployeeName | DepartmentName |
|---|---|
| John | HR |
| Sarah | IT |
| Mike | HR |
| NULL | Marketing |
All records from the Departments table are returned. For the Marketing department, there is no corresponding EmployeeID, so EmployeeName is NULL.
FULL JOIN (FULL OUTER JOIN)
A FULL JOIN returns all records when there is a match in either the left or right table. If there is no match, the result is NULL on the side that doesn’t have a match.
Here’s an implementation of FULL JOIN on our example tables:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
| EmployeeName | DepartmentName |
|---|---|
| John | HR |
| Sarah | IT |
| Mike | HR |
| Linda | NULL |
| NULL | Marketing |
All records from both tables are returned, with NULL in places where there is no match.
CROSS JOIN
A CROSS JOIN returns the Cartesian product of the two tables, i.e., it returns all possible combinations of rows from the two tables.
Here’s an implementation of CROSS JOIN on our example tables:
SELECT Employees.EmployeeName, Departments.DepartmentName
FROM Employees
CROSS JOIN Departments;
| EmployeeName | DepartmentName |
|---|---|
| John | HR |
| John | IT |
| John | Marketing |
| Sarah | HR |
| Sarah | IT |
| Sarah | Marketing |
| Mike | HR |
| Mike | IT |
| Mike | Marketing |
| Linda | HR |
| Linda | IT |
| Linda | Marketing |
Every row from Employees is paired with every row from Departments, which results in all possible combinations.
Summary
So, here’s how SQL joins work:
- INNER JOIN: Only returns rows where there is a match in both tables.
- LEFT JOIN: Returns all rows from the left table, with matching rows from the right table. If no match, returns NULL.
- RIGHT JOIN: Returns all rows from the right table, with matching rows from the left table. If no match, returns NULL.
- FULL JOIN: Returns all rows when there is a match in either table. If no match, returns NULL on the side that doesn’t have a match.
- CROSS JOIN: Returns all possible combinations of rows between the two tables.