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.
Different types of sql joinsIn SQL there are different types of joins having a unique purpose:
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 joinLeft 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 joinRight 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 joinFull 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 examplesTABLE 1
TABLE 2
Inner join
Left join
Right join
Full join
Difference between joins Key points and usageAll Rights Reserved. Copyright , Central Coast Communications, Inc.