Structured Query Language (SQL) is a strong instrument that can be used to handle and modify relational databases. The join is one of the chief operations in SQL that helps you combine rows from two or more tables based on a related column. In this article, we will examine various kinds of joins involving inner, left, right and full joins by using the specified keywords.
Table of Contents
Different types of sql joins
In SQL there are different types of joins having a unique purpose:
- INNER JOIN
- LEFT JOIN( Left outer join)
- RIGHT JOIN( Right outer join)
- FULL JOIN( Full outer join)
Inner join
Inner join returns the records that have matching values in both the tables. It is the most common join in SQL and is used when you need to retrieve the rows that have corresponding values in both tables.
SYNTAX:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
EXAMPLE:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
EXPLANATION: In this example, we are retrieving the names of employees and the names of their departments. The INNER JOIN ensures that only employees who are assigned to a department are included in the results. The join key is the department_id, which must match in both the employees and departments tables for a row to be included in the result set.
Left join
Left join returns all the records from the left table or table1, and the matched records from the right table table2. If there is no match, the result is null from the right side.
SYNTAX:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
EXAMPLE:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
EXPLANATION: This query retrieves the names of all employees and their corresponding department names. If an employee is not assigned to any department, the department_name will be NULL in the result set. The LEFT JOIN is useful when you need to include all records from the left table, regardless of whether there is a match in the right table.
Right join
Right join is similar to left join , it returns all the records from the right table or table2 and only matched record from the left table or table1. If there is no match, the result is null from the left side.
SYNTAX:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
EXAMPLE:
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
EXPLANATION: In this query, we retrieve the names of employees and their departments. If a department does not have any employees assigned to it, the name column will be NULL in the result set. The RIGHT JOIN is useful when you need to include all records from the right table, even if there are no corresponding records in the left table.
Full join
Full join returns all the records even when there are no matches between the left table or table1 and the right table or the table2. If there is no matches the result is null from the side where there is no match.
SYNTAX:
SELECT column_name(s)
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
EXAMPLE:
SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments
ON employees.department_id = departments.department_id;
EXPLAINATION: This query retrieves all employee names and department names, including those records that do not have a match in the other table. If an employee is not assigned to a department, or if a department has no employees, the respective columns will contain NULL values. The FULL JOIN is useful for combining all records from both tables, ensuring no data is omitted.
Output examples
TABLE 1
id | name |
1 | alice |
2 | bob |
3 | carol |
TABLE 2
id | department |
2 | hr |
3 | it |
4 | sales |
Inner join
ID | name | department |
2 | bob | hr |
3 | carol | it |
Left join
ID | name | department |
1 | alice | null |
2 | bob | hr |
3 | carol | it |
Right join
ID | name | department |
2 | bob | hr |
3 | carol | it |
4 | null | sales |
Full join
ID | name | department |
1 | alice | null |
2 | bob | hr |
3 | carol | it |
4 | null | sales |
Difference between joins
Join type | inner join | left join | right join | full join |
description | returns only the rows with the matching values in both tables. | returns all the rows from left table and matched rows from the right table. | returns all rows from the right table and matched rows from the left table. | returns all rows when there is a match in either left or right table. |
includes rows from | only rows with matching values in both tables. | all rows from the left table and matched rows from the right table. | all rows from the right table and matched rows from the left table. | all rows from both tables |
when match is found | rows with matching values from both tables are included. | matched rows from both table are included. | matched rows from both tables are included. | matched rows from both tables are included. |
when no match is found | rows with no matching values are excluded. | unmatched rows from from the left table are included with nulls for columns from the right table. | unmatched rows from the right table are included with null for columns from the left table. | unmatched rows from both tables are included with null for columns from the tables without a match. |
Key points and usage
- SQL join key: The column(s) used to join tables. For example, department_id in the examples above.
- ON in SQL: Clause used to specify the condition to join tables. It defines the relationship between columns in different tables.
- What is full join: A join that returns all records from both tables, with NULLs in places where the join condition is not met.
- As of join: Not a standard SQL term; possibly refers to specific temporal joins in some databases, used to join tables based on time or versioning.
- Join type: Refers to the type of join operation (inner, left, right, full, natural). Each type serves different data retrieval needs.
- What is right join: A join that returns all records from the right table and matched records from the left table.