SQL JOINS are used to combine rows from two or more tables based on a related column between them. They allow you to retrieve data from multiple tables and display it in a single result set. There are different types of joins, each serving a specific purpose. Letās break down the common types of SQL joins:
1.Ā INNER JOIN
An INNER JOIN returns records that have matching values in both tables. If a record in one table has no corresponding record in the other table, it will not appear in the result.
Example:
Letās say we have two tables:
- employeesĀ (withĀ employee_id,Ā name,Ā department_id)
- departmentsĀ (withĀ department_id,Ā department_name)
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
- This query will return all employees who have a matching department (based onĀ department_id).
- If an employee has no department assigned (i.e.,Ā department_idĀ doesn’t match), they will not appear in the result.
2.Ā LEFT JOIN (or LEFT OUTER JOIN)
A LEFT JOIN returns all records from the left table (the table before the JOIN keyword) and the matched records from the right table. If thereās no match, NULL values are returned for columns from the right table.
Example:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
- This query returnsĀ allĀ employees, including those without a department. For employees without a department, theĀ department_nameĀ will beĀ NULL.
3.Ā RIGHT JOIN (or RIGHT OUTER JOIN)
A RIGHT JOIN is the opposite of a LEFT JOIN. It returns all records from the right table and the matching records from the left table. If no match is found, NULL values will be returned for columns from the left table.
Example:
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
- This query returnsĀ allĀ departments, including those with no employees. For departments without any employees, theĀ nameĀ column will beĀ NULL.
4.Ā FULL JOIN (or FULL OUTER JOIN)
A FULL JOIN returns all records when there is a match in either the left or right table. If thereās no match, it returns NULL for the non-matching side.
Example:
SELECT employees.name, departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;
- This query returnsĀ allĀ employees and all departments, even if some employees donāt belong to a department or some departments have no employees.
5.Ā CROSS JOIN
A CROSS JOIN returns the Cartesian product of both tables, meaning it returns all possible combinations of rows from the two tables. This join does not require a condition. Itās rarely used in practice unless you specifically need this kind of result.
Example:
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;
- If there are 3 employees and 5 departments, this query will return 3 Ć 5 = 15 rows (every employee with every department).
6.Ā SELF JOIN
A SELF JOIN is a join where a table is joined with itself. Itās useful when you need to compare rows within the same table.
Example:
Consider an employees table where each employee has a manager_id which is a reference to another employee_id (the manager).
SELECT e1.name AS Employee, e2.name AS Manager
FROM employees e1
LEFT JOIN employees e2
ON e1.manager_id = e2.employee_id;
- This query returns a list of employees with their respective managers. If an employee doesnāt have a manager, theĀ ManagerĀ column will beĀ NULL.
Summary of SQL JOIN Types:
Join Type | Description |
---|---|
INNER JOIN | Returns rows with matching values in both tables. |
LEFT JOIN | Returns all rows from the left table, and matching rows from the right table (or NULL if no match). |
RIGHT JOIN | Returns all rows from the right table, and matching rows from the left table (or NULL if no match). |
FULL JOIN | Returns all rows when there is a match in one of the tables. |
CROSS JOIN | Returns the Cartesian product of both tables (all possible combinations of rows). |
SELF JOIN | Joins a table with itself, useful for hierarchical data like employees and managers. |
Performance Considerations:
- INNER JOINĀ is often faster than outer joins because it doesnāt need to include unmatched rows.
- LEFT/RIGHT JOINsĀ can be slower because they need to handle NULL values for unmatched rows.
- CROSS JOINĀ can generate large results and may severely impact performance when dealing with large datasets.