View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Demystifying SQL Joins: A Beginner's Guide to Inner, Outer, Left, and Right Joins

Updated on 31/01/2025485 Views

Introduction

When combining data from many tables, the SQL JOIN statement works well. Effective database queries need a solid understanding of JOINs. This comprehensive guide examines SQL joins, including their types, syntax, practical uses, performance concerns, and advanced techniques. We will also look at many Join SQL examples.

Overview

SQL JOINs allow you to retrieve data from two or more tables that are based on a related column between them. The primary keyword, ‘join in SQL’, refers to this fundamental concept. Let's dive into the different Joins in SQL and their applications.

When working with relational databases, you will frequently come across situations where data is dispersed over several tables. As an example, take a database of an e-commerce site, which might have distinct tables for orders, products, and customers. You must compile data from these tables to track sales and analyze customer behavior. SQL JOINs are useful in this situation.

SQL JOINs help you merge data from multiple tables based on a common column, typically known as the ‘key’ or ‘foreign key’. This allows you to create comprehensive datasets that contain information from related tables.

We'll explore a variety of SQL JOIN types as we move further, including inner joins, outer joins (left, right, and complete), and sophisticated join methods like self-joins and cross joins. Each type of join serves a unique purpose and has its own syntax and use cases.

The SQL JOIN Types

To combine data from several tables, SQL JOINs are essential to database querying. There are multiple types of SQL joins, and each has its special features and uses. There are Inner and Outer Join in SQL and let's explore them in more detail:

1. The Inner JOIN

An inner join is a fundamental type of SQL JOIN that combines data from two tables based on a specified condition, returning only the rows that have matching values. This join type is particularly useful when you want to retrieve data that exists in both tables, filtering out any unmatched rows.

The Inner Join in SQL syntax:

SELECT columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column;

In the syntax:

  • SELECT columns specify the columns you want to retrieve from the combined result.
  • table1 and table2 are the names of the tables you are joining.
  • ON table1.column = table2.column defines the condition for the join, specifying which columns to match.

Here’s an Example of Join SQL:

Suppose we have two tables, Employees and Departments, with a common column DepartmentID. Let's explore how an inner join between these tables would work:

SELECT Employees.EmployeeID, Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

In this example:

  • We're selecting the columns for EmployeeID and Name from the table named Employees, along with the DepartmentName column from the Departments table.
  • The INNER JOIN keyword combines the two tables based on the common column DepartmentID.
  • The ON Employees.DepartmentID = Departments.DepartmentID condition specifies that only rows with matching DepartmentID values in both tables will be included in the result.

An inner join effectively creates a new result set that includes the rows from Employees and Departments where the DepartmentID values match. This means that employees who belong to existing departments will be included in the result, while employees with department IDs that do not match any department in the Departments table will be excluded.

2. The Outer Join

An outer join is a type of SQL join that includes unmatched rows from one or both tables, ensuring that data is preserved even if there is no matching entry in the other table. This is particularly useful when you want to retrieve data from one table, regardless of corresponding entries in another table. Let’s look at different outer join in SQL with examples:

  • Left Outer Join (Left Join)

This returns all rows from the left table and matching rows from the right table. If there is no match in the right table, NULL values are filled for the columns from the right table.

Left Outer Join in SQL syntax:

SELECT columns
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;

Here’s the Left Outer Join in SQL example:

Consider the scenario where we have an Employees table and a Departments table. Using a left join, we can retrieve all employees along with their corresponding departments, even if some employees are not assigned to any department.

SELECT Employees.EmployeeID, Employees.EmployeeName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

This Left outer join in SQL server query retrieves the EmployeeID and EmployeeName from the Employees table, along with the DepartmentName from the Departments table. It uses a left join to ensure that all employees are included in the result set, even if they are not assigned to any department. If an employee is not associated with any department, the DepartmentName column will contain a NULL value for that employee.

  • Right Outer Join (Right Join)

This returns all rows from the right table and matching rows from the left table. If there's no match in the left table, NULL values are filled for the columns from the left table.

Right Outer Join in SQL Syntax:

SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;

Example:

Suppose we want to retrieve all departments along with their assigned employees, even if some departments have no employees. A right join would accomplish this, ensuring that all departments are included in the result set.

SELECT Departments.DepartmentID, Departments.DepartmentName, Employees.EmployeeName
FROM Departments
RIGHT JOIN Employees ON Departments.DepartmentID = Employees.DepartmentID;

This SQL query retrieves the DepartmentID and DepartmentName from the Departments table, along with the EmployeeName from the Employees table. It uses a right join to ensure that all departments are included in the result set, even if they have no assigned employees. If a department has no associated employees, the EmployeeName column will contain a NULL value for that department.

  • Full Outer Join

This returns all rows when there is a match in either of the tables, with NULL values for unmatched rows in the opposite table.

SELECT columns
FROM table1
FULL OUTER JOIN table2 ON table1.column = table2.column;

Example:

In scenarios where we need to combine data and include all records from both tables, regardless of whether they have matches, a full outer join is appropriate. For instance, when merging customer data from a CRM system with order data from a sales system, a full outer join ensures that all customers and orders are included in the result set.

SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

In this SQL query, we are selecting the CustomerID and CustomerName from the Customers table, along with the OrderID from the Orders table. By using a full outer join (FULL OUTER JOIN), we ensure that all records from both the Customers and Orders tables are included in the result set, regardless of whether they have matching entries in the other table.

This is particularly useful when merging data from disparate systems, such as customer data from a CRM system and order data from a sales system, ensuring that all customers and orders are accounted for in the result set.

  1. Cross Join

A cross join in SQL returns the Cartesian product of the two tables involved, generating all possible combinations of rows between them.

Cross Join in SQL server syntax:

SELECT columns
FROM table1
CROSS JOIN table2;

Cross Join in SQL server example:

Suppose we have two tables, Colors and Shapes, each containing different attributes. To generate all possible combinations of colors and shapes, this is how we can use a cross join:

SELECT Colors.Color, Shapes.Shape
FROM Colors
CROSS JOIN Shapes;

This query will produce a result set containing every possible combination of colors and shapes from the respective tables.

Output:

Color

Shape

Red

Circle

Red

Square

Red

Triangle

Blue

Circle

Blue

Square

Blue

Triangle

Green

Circle

Green

Square

Green

Triangle


4. Self Join

A self join in SQL joins a table to itself, treating it as if it were two separate tables. It's typically used to compare rows within the same table.

Self Join in SQL server Syntax:

SELECT columns
FROM table1 t1
INNER JOIN table1 t2
ON t1.column = t2.column;

Example:

Suppose we have an Employees table containing information about employees, including their names and managers. We want to find pairs of employees who share the same manager. We can achieve this using a self join:

SELECT e1.EmployeeName AS Employee1, e2.EmployeeName AS Employee2, e1.Manager
FROM Employees e1
INNER JOIN Employees e2 ON e1.Manager = e2.Manager
WHERE e1.EmployeeID < e2.EmployeeID;

In this query, we alias the Employees table as e1 and e2, to differentiate between the two instances of the same table. We then join the table to itself based on the Manager column, ensuring that we only retrieve pairs of employees who share the same manager.

Output:

Employee1

Employee2

Manager

Alice

Bob

Jane

Alice

Charlie

Jane

Bob

Charlie

Jane

5. Natural Join

This is a natural join in SQL that automatically joins tables based on columns with the same name, eliminating the need to specify the join condition explicitly.

Syntax:

SELECT columns
FROM table1
NATURAL JOIN table2;

Example:

Consider the scenario where we have an Employees table and a Departments table, both containing a column named DepartmentID. We want to retrieve information about employees along with their corresponding departments. We can use a natural join to achieve this:

SELECT Employees.EmployeeID, Employees.EmployeeName, Departments.DepartmentName
FROM Employees
NATURAL JOIN Departments;

Output:

EmployeeID

EmployeeName

DepartmentName

1

Alice

Sales

2

Bob

Sales

3

Charlie

Marketing

4

Dave

Marketing

5

Emily

HR

6

Frank

HR

In this table, we have combined data from the tables given for Employees and Departments, using a natural join. The final table provides information about each employee (EmployeeID and EmployeeName) along with their respective department (DepartmentName).

The join is performed automatically based on the common column DepartmentID present in both tables.

Other Types of JOIN

  1. Equi Join in SQL

An equi join in SQL is a type of join that retrieves rows from two or more tables based on a matching condition specified in the join clause. Specifically, an equi join uses the equality operator (=) to compare values in the columns from the joined tables.

  1. Non-Equi Join in SQL

A non-equi join in SQL is a join condition that does not use the equality operator (=). Instead, it uses other comparison operators such as >, <, >=, <=, or <> (not equal to) to retrieve rows from multiple tables based on specified conditions.

  1. Default Join in SQL

There is no specific ‘default join’ in SQL. However, if you simply use the JOIN keyword without specifying a specific type of join (e.g., INNER JOIN, LEFT JOIN, etc.), the default behavior is an inner join. In other words, if you write SELECT * FROM table1 JOIN table2, it is interpreted as an inner join.

  1. Multiple Inner Join in SQL

A multiple inner join in SQL is when you join three or more tables together, using inner join operations. This allows you to combine data from multiple tables based on matching values in specified columns, creating a more comprehensive result set that includes information from all joined tables.

Conclusion

SQL joins are powerful tools for combining data from multiple tables. By understanding the different types of joins, their syntax, practical use cases, performance considerations, and advanced techniques, you can enhance your database querying skills. Experiment with various join scenarios and explore how they can streamline data analysis and decision-making processes in your projects. Happy querying!

FAQs

  1. What are the 4 types of JOINs in SQL?

    The four types of JOINs in SQL are:
  • INNER JOIN
  • LEFT JOIN (or LEFT OUTER JOIN)
  • RIGHT JOIN (or RIGHT OUTER JOIN)
  • FULL JOIN (or FULL OUTER JOIN)
  1. Can we join 3 tables in SQL?

    Yes, you can join three or more tables in SQL by specifying multiple join conditions in the query.
  1. What is the difference between WHERE and ON clauses in a JOIN?

    The WHERE clause is used to filter rows after tables have been joined, while the ON clause specifies the join conditions during the joining process.
  1. How do I decide which type of JOIN to use?

    Choose the appropriate type of JOIN based on the relationship between the tables and the desired result set. INNER JOIN returns only matching rows, LEFT JOIN returns all rows from the left table, RIGHT JOIN returns all rows from the right table, and FULL JOIN returns all rows from both tables.
  1. Can we join 2 same tables in SQL?

    Yes, you can join the same tables in SQL by aliasing the table with different names to distinguish between them.
  1. How to join 10 tables in SQL?

    To join 10 tables in SQL, specify the join conditions for each pair of tables and ensure that the join conditions logically connect all tables in the query.
  1. Why is self join used?

    A self join is used to compare rows within the same table, typically when you want to retrieve related information.
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.

2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.