Learn SQL LEFT OUTER JOIN with Examples


SQL

Reviewer: Deepak Prasad

 

A Left Outer Join (often referred to simply as a Left Join) is a type of join in SQL that returns all records from the left table (the first table listed in the query) and the matched records from the right table (the second table listed). If there is no match, the result is NULL on the side of the right table. This join is useful when you want to include all records from the left table, regardless of whether there is a corresponding match in the right table.

The basic syntax for a Left Outer Join is as follows:

SELECT columns
FROM table1
LEFT OUTER JOIN table2
ON table1.common_column = table2.common_column;

Explanation:

  • SELECT columns: This part of the query specifies the columns that you want to retrieve.
  • FROM table1: This part specifies the left table from which to retrieve the data.
  • LEFT OUTER JOIN table2: This part specifies the right table to be joined with the left table.
  • ON table1.common_column = table2.common_column: This part specifies the condition for joining the two tables, usually based on a common column.

 

SQL LEFT OUTER JOIN vs INNER JOIN vs RIGHT OUTER JOIN vs FULL OUTER JOIN

Criteria LEFT OUTER JOIN INNER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
Left Table Displayed Displayed Optional Displayed
Right Table Optional Displayed Displayed Displayed
Match Required No Yes No No
Unmatched Rows in Left Table Displayed with NULLs Ignored Ignored Displayed with NULLs
Unmatched Rows in Right Table Ignored Ignored Displayed with NULLs Displayed with NULLs
  • Displayed: The records will appear in the result set.
  • Optional: The records may or may not appear in the result set, based on whether a matching record is found.
  • Ignored: The records will not appear in the result set.
  • Displayed with NULLs: The records will appear in the result set, but with NULL values where no match is found.

Here is an image showcasing different types of SQL JOINS:

SQL LEFT OUTER JOIN Explained with Examples

 

Lab Environment (Sample Tables for Examples)

Let us create some sample tables which we will use throughout our examples:

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(50),
    department_id INT
);

INSERT INTO employees (employee_id, name, department_id) VALUES
(1, 'Aarav', 101),
(2, 'Vivaan', 102),
(3, 'Aditya', 103),
(4, 'Sai', NULL),
(5, 'Diya', 101),
(6, 'Ishaan', 104),
(7, 'Mira', NULL);

CREATE TABLE departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(50)
);

INSERT INTO departments (department_id, department_name) VALUES
(101, 'Human Resources'),
(102, 'Finance'),
(103, 'IT'),
(105, 'Marketing');

 

Simple Example - Using Left Outer Join

Here is how a Left Outer Join can be used to retrieve all employees and their corresponding departments, including employees who may not be assigned to a department:

SELECT employees.name, departments.department_name
FROM employees
LEFT OUTER JOIN departments
ON employees.department_id = departments.department_id;

Result:

Learn SQL LEFT OUTER JOIN with Examples
  • All employees are listed regardless of whether they have a corresponding department.
  • Employees Sai and Mira have NULL as their department because they do not have a department_id.
  • Employee Ishaan has a department_id (104) that does not exist in the departments table, so department_name is NULL.

 

Handling NULLs in SQL LEFT OUTER JOIN

Example 1: Using COALESCE to Replace NULL Values

Assuming we want to replace NULL values in the department_name column with the text "No Department".

SELECT 
    employees.name, 
    COALESCE(departments.department_name, 'No Department') AS department_name
FROM 
    employees
LEFT OUTER JOIN 
    departments
ON 
    employees.department_id = departments.department_id;

COALESCE function checks if departments.department_name is NULL. If it is, it returns 'No Department'. Otherwise, it returns the actual department_name.

Learn SQL LEFT OUTER JOIN with Examples

 

Example 2: Using IS NULL to Filter Records with NULL Values

Assuming we want to find employees who do not belong to any department:

SELECT 
    employees.name
FROM 
    employees
LEFT OUTER JOIN 
    departments
ON 
    employees.department_id = departments.department_id
WHERE 
    departments.department_id IS NULL;

The IS NULL condition filters out rows where departments.department_id is NULL, indicating that there is no matching department for these employees.

Learn SQL LEFT OUTER JOIN with Examples

 

Example 3: Using IS NOT NULL to Filter Records with Non-NULL Values

Now let's say we want to find employees who are assigned to a department.

SELECT 
    employees.name, 
    departments.department_name
FROM 
    employees
LEFT OUTER JOIN 
    departments
ON 
    employees.department_id = departments.department_id
WHERE 
    departments.department_id IS NOT NULL;

The IS NOT NULL condition filters out rows where departments.department_id is NULL, showing only employees with a department.

Learn SQL LEFT OUTER JOIN with Examples

 

Example 4: Using IFNULL (MySQL) or NVL (Oracle) to Handle NULL Values

What if we want to use database-specific functions to handle NULL values:

MySQL Query Using IFNULL:

SELECT 
    employees.name, 
    IFNULL(departments.department_name, 'No Department') AS department_name
FROM 
    employees
LEFT OUTER JOIN 
    departments
ON 
    employees.department_id = departments.department_id;

Oracle Query Using NVL:

SELECT 
    employees.name, 
    NVL(departments.department_name, 'No Department') AS department_name
FROM 
    employees
LEFT OUTER JOIN 
    departments
ON 
    employees.department_id = departments.department_id;

IFNULL and NVL are similar to COALESCE but are specific to MySQL and Oracle, respectively. They replace NULL values with a specified value.

 

Advanced Concepts in SQL LEFT OUTER JOIN

Diving deeper into SQL LEFT OUTER JOIN allows us to explore advanced concepts, such as its implementation in subqueries and executing multiple LEFT OUTER JOINs in a single query. These advanced techniques further enhance the flexibility and power of SQL queries. We’ll demonstrate these concepts using the Employees and Departments tables.

 

Using LEFT OUTER JOIN in Subqueries

We want to find employees who have worked on the most recent project in their department. Here, the projects table contains information about projects and their completion dates.

CREATE TABLE projects (
    project_id INT PRIMARY KEY,
    department_id INT,
    project_name VARCHAR(50),
    completion_date DATE,
    FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

INSERT INTO projects (project_id, department_id, project_name, completion_date) VALUES
(1, 101, 'Project A', '2024-01-10'),
(2, 102, 'Project B', '2024-03-15'),
(3, 103, 'Project C', '2024-02-20'),
(4, 101, 'Project D', '2024-05-22'),
(5, 104, 'Project E', '2024-04-30');

Additionally we need to ensure all department IDs in the projects table are present in the departments table. So for the data we are using in this lab, the department_id 104 must be added to the departments table.

INSERT INTO departments (department_id, department_name) VALUES
(104, 'Operations');

SQL Query:

SELECT 
    e.name, 
    p.project_name, 
    p.completion_date
FROM 
    employees e
LEFT OUTER JOIN (
    SELECT 
        p1.department_id, 
        p1.project_name, 
        p1.completion_date
    FROM 
        projects p1
    INNER JOIN (
        SELECT 
            department_id, 
            MAX(completion_date) AS max_date
        FROM 
            projects
        GROUP BY 
            department_id
    ) p2 
    ON 
        p1.department_id = p2.department_id 
        AND p1.completion_date = p2.max_date
) p
ON 
    e.department_id = p.department_id;
  • Subquery p2: Selects the maximum completion_date for each department_id.
  • Subquery p1: Joins with p2 to get the project details for the latest projects per department.
  • Main Query: Performs a LEFT OUTER JOIN between employees and the subquery p to include all employees and their latest project details, if available.

Result:

Learn SQL LEFT OUTER JOIN with Examples

 

Multiple LEFT OUTER JOINs in a Query

To demonstrate this we want to list all employees, their department names, and the names of their managers. The managers table contains information about department managers.

CREATE TABLE managers (
    manager_id INT PRIMARY KEY,
    department_id INT,
    manager_name VARCHAR(50),
    FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

INSERT INTO managers (manager_id, department_id, manager_name) VALUES
(1, 101, 'Dr. Sharma'),
(2, 102, 'Ms. Nair'),
(3, 103, 'Mr. Singh'),
(4, 105, 'Ms. Gupta');

SQL Query:

SELECT 
    e.name AS employee_name, 
    d.department_name, 
    m.manager_name
FROM 
    employees e
LEFT OUTER JOIN 
    departments d
ON 
    e.department_id = d.department_id
LEFT OUTER JOIN 
    managers m
ON 
    e.department_id = m.department_id;
  • The first LEFT OUTER JOIN retrieves department names for employees.
  • The second LEFT OUTER JOIN retrieves manager names for employees.

Result:

Learn SQL LEFT OUTER JOIN with Examples

 

Frequently Asked Questions and Answers on SQL Left Outer Join

What is a LEFT OUTER JOIN in SQL?

A LEFT OUTER JOIN in SQL is a type of join that returns all records from the left table and the matched records from the right table. If there are no matching records in the right table, the result set will contain NULL values. It allows us to retrieve data from two or more tables while preserving the unmatched rows from the left table.

How does the LEFT OUTER JOIN handle NULL values?

LEFT OUTER JOIN handles NULL values by filling in NULL in the columns of the result set where there is no matching record in the right table. For example, if we have a LEFT OUTER JOIN between Employees and Departments tables on the DepartmentID, and if some employees do not have a corresponding department, the DepartmentName in the output will show as NULL, indicating the absence of a matching department for those employees.

What is the difference between an INNER JOIN and a LEFT OUTER JOIN?

An INNER JOIN returns records that have matching values in both tables involved in the join, excluding records that don’t have corresponding matches in either table. On the other hand, a LEFT OUTER JOIN returns all records from the left table, and the matched records from the right table, filling in with NULLs where there’s no match. In essence, an INNER JOIN shows only the common records between two tables, while a LEFT OUTER JOIN retains all records from the left table, regardless of whether there’s a matching record in the right table or not.

How do we manage or replace NULL values in the result of a LEFT OUTER JOIN?

We can manage or replace NULL values in the result of a LEFT OUTER JOIN using functions like ISNULL() (in SQL Server) or COALESCE() (in most SQL databases). These functions help in replacing the NULL values with a specified value. For instance, when joining Employees and Departments, if some employees don’t belong to any department, you could use ISNULL(Departments.DepartmentName, 'Not Assigned') to replace the NULL values in the DepartmentName column with the text 'Not Assigned'.

Can we perform multiple LEFT OUTER JOINS in a single query, and what are the considerations?

Yes, we can perform multiple LEFT OUTER JOINS in a single query. When doing so, it’s essential to carefully manage the order of the joins and the conditions to ensure that the logic of the query remains correct. We should also consider the performance implications, as multiple joins can make the query more complex and potentially slow down the execution, depending on the database and the amount of data being processed.

 

Summarizing Key Takeaways

  • Understanding of LEFT OUTER JOIN: LEFT OUTER JOIN is instrumental in combining rows from both participating tables even if there are no matching rows in the right table, keeping all records from the left table.
  • Handling NULLs: LEFT OUTER JOIN manages the absence of matching records by introducing NULL values where matches are not found, ensuring that the dataset remains comprehensive.
  • Flexibility in Queries: Incorporating LEFT OUTER JOIN into queries offers flexibility and robustness, allowing for the retrieval of inclusive datasets and accommodating for disparities in table relationships.
  • Advanced Usage: Using LEFT OUTER JOIN in subqueries and multiple LEFT OUTER JOINs in a single query enables complex data retrieval and manipulation, providing a powerful tool for more advanced SQL operations.
  • Comparison with Other Joins: Understanding the differences and suitable use-cases for LEFT OUTER JOIN in comparison to INNER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN is crucial for effective database querying and design.

Here are some resources which can be used to learn more:

 

Falguni Thakker

Falguni Thakker

She is a dedicated professional with expertise in SQL, Python, C++, and Linux. Currently serving as a professor at a prestigious university. With a passion for teaching and a strong technical background, she inspires the next generation of computer scientists. You can connect with her on her LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

3 thoughts on “Learn SQL LEFT OUTER JOIN with Examples”

  1. joints or joins?

    What are JOINS in SQL?
    “The joints are predominantly used when a user is trying to fetch or get data from tables which have one to many or many to many relationships between them. “

    Reply

Leave a Comment