In the world of databases, SQL JOINS are essential for combining data from multiple tables. Since most databases are designed with normalization principles, data is often spread across several related tables. The SQL JOINs provide a way to link these tables together, enabling you to query the database and retrieve information from more than one table in a single query.
A JOIN in SQL is a keyword used to combine data from two or more tables based on a related column. SQL JOINS allow you to combine records from two or more tables into a single result set based on a logical relationship between them.
The most commonly used types of SQL JOINs are:
The INNER JOIN is the most commonly used JOIN type. It returns only the rows that have matching values in both tables. If there is no match between the two tables, the row will not appear in the result set.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Consider two tables: employees
and departments
.
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
employees
and departments
tables.employee_id
, employee_name
, and their corresponding department_name
.Note: Only employees that belong to a department will appear in the results. Employees with no department assignment will be excluded.
A LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table (the first table), and the matched rows from the right table (the second table). If there is no match, NULL values are returned for columns from the right table.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
department_name
will be NULL
in the result set.Use Case: A LEFT JOIN is useful when you want to get all records from the left table, even if there are no matching records in the right table.
A RIGHT JOIN (or RIGHT OUTER JOIN) is similar to the LEFT JOIN, but it returns all rows from the right table (the second table) and the matching rows from the left table (the first table). If there is no match, NULL values are returned for columns from the left table.
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
NULL
.Use Case: A RIGHT JOIN is useful when you want to get all records from the right table, even if there are no matching records in the left table.
A FULL JOIN (or FULL OUTER JOIN) returns all rows when there is a match in either the left table or the right table. It combines the result of both a LEFT JOIN and a RIGHT JOIN. If there is no match, NULL values are returned for columns from the table that does not have the match.
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
FULL JOIN departments
ON employees.department_id = departments.department_id;
Explanation:
NULL
for the department_name
.NULL
for the employee_id
and employee_name
.Use Case: A FULL JOIN is useful when you want to ensure that all records from both tables are returned, regardless of whether there is a match.
A SELF JOIN is a join where a table is joined with itself. This can be useful when you need to compare rows within the same table.
SELECT a.column, b.column
FROM table_name a
INNER JOIN table_name b
ON a.column = b.column;
SELECT e1.employee_name AS employee, e2.employee_name AS manager
FROM employees e1
INNER JOIN employees e2
ON e1.manager_id = e2.employee_id;
Explanation:
employees
table is joined with itself.manager_id
of an employee corresponds to the employee_id
of their manager.You can combine multiple JOINS in a single query to link several tables together.
SELECT e.employee_name, d.department_name, p.project_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id
INNER JOIN projects p ON e.employee_id = p.employee_id;
Explanation:
employees
, departments
, and projects
tables.ON
condition can significantly improve the performance of JOIN queries.