A join is an SQL operation that combines records from two or more tables based on related columns. The primary purpose of a join is to assemble data from multiple tables to provide a comprehensive view of the information. Joins are performed using the JOIN clause, which specifies the columns on which the tables should be joined.
2. Types of Joins
There are several types of joins in SQL, each serving a specific purpose. The most common types are:
An inner join returns only the rows that have matching values in both tables. It is the most commonly used type of join.
SELECT e.employee_id, e.first_name, e.last_name, d.department_nameFROM employees eINNER JOIN departments d ON e.department_id = d.department_id;
Result:
employee_id | first_name | last_name | department_name------------------------------------------------------1 | John | Doe | Human Resources2 | Jane | Smith | Finance4 | Mike | Davis | Human Resources5 | Sara | Wilson | Engineering
NOTE: No return for employee_id = 3
2.2. Left Join (or Left Outer Join)
A left join returns all rows from the left table and the matched rows from the right table. If no match is found, NULL values are returned for columns from the right table.
SELECT e.employee_id, e.first_name, e.last_name, d.department_nameFROM employees eLEFT JOIN departments d ON e.department_id = d.department_id;
Result:
employee_id | first_name | last_name | department_name------------------------------------------------------1 | John | Doe | Human Resources2 | Jane | Smith | Finance3 | Sam | Brown | NULL4 | Mike | Davis | Human Resources5 | Sara | Wilson | Engineering
2.3. Right Join (or Right Outer Join)
A right join returns all rows from the right table and the matched rows from the left table. If no match is found, NULL values are returned for columns from the left table.
SELECT e.employee_id, e.first_name, e.last_name, d.department_nameFROM employees eRIGHT JOIN departments d ON e.department_id = d.department_id;
Result:
employee_id | first_name | last_name | department_name------------------------------------------------------1 | John | Doe | Human Resources2 | Jane | Smith | Finance4 | Mike | Davis | Human Resources5 | Sara | Wilson | EngineeringNULL | NULL | NULL | Marketing
2.4. Full Join (or Full Outer Join)
A full join returns all rows when there is a match in either table. If there is no match, NULL values are returned for columns from the table without a match.
SELECT e.employee_id, e.first_name, e.last_name, d.department_nameFROM employees eFULL JOIN departments d ON e.department_id = d.department_id;
Result:
employee_id | first_name | last_name | department_name------------------------------------------------------1 | John | Doe | Human Resources2 | Jane | Smith | Finance3 | Sam | Brown | NULL4 | Mike | Davis | Human Resources5 | Sara | Wilson | EngineeringNULL | NULL | NULL | Marketing
2.5. Cross Join
A cross join returns the Cartesian product of the two tables, i.e., all possible combinations of rows from the two tables. In our example, employees table has 5 records and departments table has 4 records. Therefore, cross join should result in 5x4= 20 records/combinations.
employee_id | first_name | last_name | department_name------------------------------------------------------1 | John | Doe | Human Resources1 | John | Doe | Finance1 | John | Doe | Engineering1 | John | Doe | Marketing2 | Jane | Smith | Human Resources2 | Jane | Smith | Finance2 | Jane | Smith | Engineering2 | Jane | Smith | Marketing3 | Sam | Brown | Human Resources3 | Sam | Brown | Finance3 | Sam | Brown | Engineering3 | Sam | Brown | Marketing4 | Mike | Davis | Human Resources4 | Mike | Davis | Finance4 | Mike | Davis | Engineering4 | Mike | Davis | Marketing5 | Sara | Wilson | Human Resources5 | Sara | Wilson | Finance5 | Sara | Wilson | Engineering5 | Sara | Wilson | Marketing
2.6. Self Join
A self join is a regular join where a table is joined with itself. This is useful for scenarios where you need to compare rows within the same table. For example, let's say we want to find employees and their managers from the employees table.
SELECT e1.employee_id AS employee_id, e1.first_name AS employee_first_name, e1.last_name AS employee_last_name, e2.first_name AS manager_first_name, e2.last_name AS manager_last_nameFROM employees e1LEFT JOIN employees e2 ON e1.manager_id = e2.employee_id;
Result:
employee_id | employee_first_name | employee_last_name | manager_first_name | manager_last_name---------------------------------------------------------------------------------------------1 | John | Doe | NULL | NULL2 | Jane | Smith | John | Doe3 | Sam | Brown | John | Doe4 | Mike | Davis | John | Doe5 | Sara | Wilson | Jane | Smith
Below is one of the most popular diagram showing all joins, excluding cross and self joins:
3. Conclusion
Joins are a powerful feature of SQL that allow you to combine data from multiple tables in various ways to gain deeper insights and produce meaningful reports. Understanding the different types of joins and how to use them effectively is crucial for anyone working with relational databases. By mastering joins, you can write more efficient queries and build more complex and informative datasets.