SQL Joins
SQL Joins are used to combine rows from two or more tables based on a related column between them. Here are the most common types of SQL joins:
- INNER JOIN
- LEFT JOIN (or LEFT OUTER JOIN)
- RIGHT JOIN (or RIGHT OUTER JOIN)
- FULL JOIN (or FULL OUTER JOIN)
- CROSS JOIN
Let's use two example tables to illustrate these joins:
Tables
Table: employees
employee_id | employee_name | department_id |
---|---|---|
1 | John Doe | 101 |
2 | Jane Smith | 102 |
3 | Emily Davis | 103 |
4 | Michael Brown | NULL |
Table: departments
department_id | department_name |
---|---|
101 | HR |
102 | IT |
103 | Sales |
104 | Marketing |
1. INNER JOIN
Returns only the rows that have matching values in both tables.
Query:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
Output:
employee_id | employee_name | department_name |
---|---|---|
1 | John Doe | HR |
2 | Jane Smith | IT |
3 | Emily Davis | Sales |
2. LEFT JOIN (or LEFT OUTER JOIN)
Returns all rows from the left table, and the matched rows from the right table. If no match is found, NULLs are returned for columns from the right table.
Query:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;
Output:
employee_id | employee_name | department_name |
---|---|---|
1 | John Doe | HR |
2 | Jane Smith | IT |
3 | Emily Davis | Sales |
4 | Michael Brown | NULL |
3. RIGHT JOIN (or RIGHT OUTER JOIN)
Returns all rows from the right table, and the matched rows from the left table. If no match is found, NULLs are returned for columns from the left table.
Query:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;
Output:
employee_id | employee_name | department_name |
---|---|---|
1 | John Doe | HR |
2 | Jane Smith | IT |
3 | Emily Davis | Sales |
NULL | NULL | Marketing |
4. FULL JOIN (or FULL OUTER JOIN)
Returns all rows when there is a match in either left or right table. Rows without a match in the other table will contain NULLs.
Query:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
FULL OUTER JOIN departments ON employees.department_id = departments.department_id;
Output:
employee_id | employee_name | department_name |
---|---|---|
1 | John Doe | HR |
2 | Jane Smith | IT |
3 | Emily Davis | Sales |
4 | Michael Brown | NULL |
NULL | NULL | Marketing |
5. CROSS JOIN
Returns the Cartesian product of both tables, i.e., all possible combinations of rows.
Query:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
CROSS JOIN departments;
Output:
employee_id | employee_name | department_name |
---|---|---|
1 | John Doe | HR |
1 | John Doe | IT |
1 | John Doe | Sales |
1 | John Doe | Marketing |
2 | Jane Smith | HR |
2 | Jane Smith | IT |
2 | Jane Smith | Sales |
2 | Jane Smith | Marketing |
3 | Emily Davis | HR |
3 | Emily Davis | IT |
3 | Emily Davis | Sales |
3 | Emily Davis | Marketing |
4 | Michael Brown | HR |
4 | Michael Brown | IT |
4 | Michael Brown | Sales |
4 | Michael Brown | Marketing |
These examples illustrate how SQL joins work and how they can be used to combine data from multiple tables based on specific conditions.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.