What Are SQL JOINs?
In relational databases, data is often spread across multiple tables. SQL JOINs allow you to combine rows from two or more tables based on related columns, enabling comprehensive analysis and reporting.

π― Learning Objectives: SQL JOIN
By the end of this tutorial, you will:
β
Understand INNER, LEFT, RIGHT, and FULL OUTER JOIN
β
Apply JOINs to real-world scenarios
β
Use JOINs with filters, aliases, and multiple conditions
β
Visualize how data is combined across tables
π§ Base Scenario (Employee – Department Relationship)
Weβll use two real-world inspired tables:
π Table 1: Employees
EmpID | Name | DeptID | Salary |
1 | Alice | 101 | 60000 |
2 | Bob | 102 | 50000 |
3 | Charlie | NULL | 40000 |
4 | David | 104 | 55000 |
5 | Eva | 103 | 65000 |
π Table 2: Departments
DeptID | DeptName | Location |
101 | HR | New York |
102 | IT | San Diego |
103 | Finance | Chicago |
106 | Marketing | Boston |
β Note: DeptIDs 104 (used by David) and 106 (no employee) are unmatched across tables.
1οΈβ£ INNER JOIN: SQL JOIN
π Definition
Returns only the rows where there is a match in both tables.
π§ Analogy
Youβre preparing a report of employees with assigned departments. If the department doesn’t exist, they are excluded.
π» SQL Query
SELECT E.Name, D.DeptName, D.Location
FROM Employees E
INNER JOIN Departments D ON E.DeptID = D.DeptID;
β Output
Name | DeptName | Location |
Alice | HR | New York |
Bob | IT | San Diego |
Eva | Finance | Chicago |
β David (invalid DeptID = 104) and Charlie (no DeptID) are excluded.
π Visual Metaphor
[Employees] β© [Departments]2οΈβ£ LEFT JOIN (LEFT OUTER JOIN): SQL JOIN
π Definition
Returns all records from the left table (Employees) and matching records from the right table (Departments). Unmatched departments β NULL.
π§ Analogy
You want a list of all employees, even if their department is not known or doesn’t exist.
π» SQL Query
SELECT E.Name, D.DeptName, D.Location
FROM Employees E
LEFT JOIN Departments D ON E.DeptID = D.DeptID;
β Output
Name | DeptName | Location |
Alice | HR | New York |
Bob | IT | San Diego |
Charlie | NULL | NULL |
David | NULL | NULL |
Eva | Finance | Chicago |
β All employees are listed, even those with missing or invalid DeptIDs.
π Visual Metaphor
[Employees] β [Departments]Read also: How to Learn C++: A Beginnerβs Guide for 2025, Best way To Learn C++
3οΈβ£ RIGHT JOIN (RIGHT OUTER JOIN): SQL JOIN
π Definition
Returns all records from the right table (Departments) and the matching rows from the left table (Employees). Unmatched employees β NULL.
π§ Analogy
You want a list of all departments, even if no employee belongs to them.
π» SQL Query
SELECT E.Name, D.DeptName, D.Location
FROM Employees E
RIGHT JOIN Departments D ON E.DeptID = D.DeptID;
β Output
Name | DeptName | Location |
Alice | HR | New York |
Bob | IT | San Diego |
Eva | Finance | Chicago |
NULL | Marketing | Boston |
β All departments included. Marketing has no employees β NULL in Name.
π Visual Metaphor
[Employees] β [Departments]4οΈβ£ FULL JOIN (FULL OUTER JOIN): SQL JOIN
π Definition
Returns all records from both tables. Matched where possible, and NULL for non-matches.
π§ Analogy
You want a complete picture β all employees and all departments, whether related or not.
π» SQL Query
SELECT E.Name, D.DeptName, D.Location
FROM Employees E
FULL OUTER JOIN Departments D ON E.DeptID = D.DeptID;
β Output
Name | DeptName | Location |
Alice | HR | New York |
Bob | IT | San Diego |
Eva | Finance | Chicago |
Charlie | NULL | NULL |
David | NULL | NULL |
NULL | Marketing | Boston |
π Visual Metaphor
[Employees] βͺ [Departments] (on DeptID)π§ͺ Advanced Examples
π― 1. Find employees without a department
SELECT Name
FROM Employees
LEFT JOIN Departments ON Employees.DeptID = Departments.DeptID
WHERE Departments.DeptID IS NULL;
β Result:
Name |
Charlie |
David |
π― 2. Find departments with no employees
SELECT DeptName
FROM Departments
LEFT JOIN Employees ON Departments.DeptID = Employees.DeptID
WHERE Employees.EmpID IS NULL;
β Result:
DeptName |
Marketing |
π― 3. Join with additional conditions (e.g., high earners in Finance)
SELECT E.Name, D.DeptName, E.Salary
FROM Employees E
INNER JOIN Departments D ON E.DeptID = D.DeptID
WHERE D.DeptName = ‘Finance’ AND E.Salary > 60000;
β Result:
Name | DeptName | Salary |
Eva | Finance | 65000 |
π JOIN Comparison Summary

JOIN Type | Left Table Included | Right Table Included | NULLs Possible |
INNER JOIN | β if match | β if match | β |
LEFT JOIN | β always | β if match | β Right table |
RIGHT JOIN | β if match | β always | β Left table |
FULL OUTER | β always | β always | β Both sides |
π¦ Practice: Table Creation & Sample Data
CREATE TABLE Employees (
EmpID INT,
Name VARCHAR(50),
DeptID INT,
Salary INT
);
INSERT INTO Employees VALUES
(1, ‘Alice’, 101, 60000),
(2, ‘Bob’, 102, 50000),
(3, ‘Charlie’, NULL, 40000),
(4, ‘David’, 104, 55000),
(5, ‘Eva’, 103, 65000);
CREATE TABLE Departments (
DeptID INT,
DeptName VARCHAR(50),
Location VARCHAR(50)
);
INSERT INTO Departments VALUES
(101, ‘HR’, ‘New York’),
(102, ‘IT’, ‘San Diego’),
(103, ‘Finance’, ‘Chicago’),
(106, ‘Marketing’, ‘Boston’);
π Final Thoughts
Understanding JOINs is fundamental to mastering SQL. Here’s a quick recap:
- INNER JOIN: Only matching records β strict.
- LEFT JOIN: All from the left, fill blanks from the right.
- RIGHT JOIN: All from the right, fill blanks from the left.
- FULL OUTER JOIN: Show everything β match where possible.

π Mastering JOINs allows you to build powerful queries, extract hidden insights, and unlock the true potential of your data.