Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
 
 
 
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 

SQL Joins (Inner, Left, Right and Full)

DATE POSTED:November 21, 2024

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 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

TABLE 2

Inner join

Left join

Right join

Full join

Difference between joins 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.