Open In App

FULL OUTER JOIN in SQLite

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In the area of data querying and manipulation, the ability to combine information from different sources is important. SQLite, a popular embedded database engine, offers a range of join operations to fast process. One such operation FULL OUTER JOIN is particularly powerful as it allows us to merge data from two tables, even if there are no direct matches between them. In this article, we will explore the concept of FULL OUTER JOIN in SQLite, its syntax, and how it can be used to extract valuable insights from your data.

What is SQLite FULL OUTER JOIN?

The main concept of SQLite FULL OUTER JOIN is to combine rows from two tables (based on a specified condition), including unmatched rows from both tables. The set of results includes matched rows where the condition is satisfied rows that are unmatched and rows filled with NULL values where applicable.

The main advantage of a FULL OUTER JOIN in SQLite (or any SQL database) that allows us to combine rows from two tables even if there is no match between the columns being joined. This means we can retrieve all records from both tables, matching them where possible and including NULL values where there is no match.

Syntax:

SELECT *
FROM table1
LEFT OUTER JOIN table2 ON table1.key = table2.key
UNION
SELECT *
FROM table1
RIGHT OUTER JOIN table2 ON table1.key = table2.key
WHERE table1.key IS NULL;

Explanation of Syntax: This above syntax performs a full outer join between table1 and table2, combining all rows from both tables based on the key column. The LEFT OUTER JOIN retrieves all records from table1 and matching records from table2, while the RIGHT OUTER JOIN retrieves all records from table2 and matching records from table1. The UNION operator merges the results and the WHERE clause filters out rows where table1.key is NULL, ensuring the full outer join behavior.

Example of FULL OUTER JOIN

-- Creating employees table
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
name TEXT,
department_id INTEGER,
salary INTEGER
);

-- Creating departments table
CREATE TABLE departments (
department_id INTEGER PRIMARY KEY,
department_name TEXT
);

As we can saw in the image that the we have created two employees and departments table.

DB-Schema

DB Schema with Both Employees and Departments Tables

After Inserting Some records into the employees table, the table looks:

Employees-Table

Employees Table

After Inserting Some records into the departments table, the table looks:

Departments-Table

Departments Table

Example 1: Basic FULL OUTER JOIN

SELECT * FROM employees
FULL OUTER JOIN departments ON employees.department_id = departments.department_id;

Output:

Basic-FULL-OUTER-JOIN

Basic FULL OUTER JOIN

Output Explanation: In this example, the querry we have written here fethes all records from both tables and matches them based on the “department_id“. The result includes all rows from the ‘employees‘ table and the ‘departments‘ table, with unmatched records filled with NULL values.

Example 2: Filtering with WHERE Clause

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

Output:

Filtering-with-WHERE-Clause

Filtering with WHERE Clause

Output Explanation: In this example, the query is applying a filter to the result set using the WHERE clause. It selects only those records where the employee’s salary is greater than $50,000.

Here, the FULL OUTER JOIN ensures that even if an employee does not belong to any department or if a department has no employees that meets the criteria, their information is still included in the result.

Example 3: Handling NULL Values with COALESCE

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

Output:

Handling-NULL-Values-with-COALESCE

Handling NULL Values with COALESCE

Output Explanation: In this example, we have used ‘COALESCE‘ function to handle NULL values. If we can see in the previous two examples, we are getting the output with the Null values. But in this example, we did not get any null values and this happened becasue of the ‘COALESCE‘ function only.

It selcts the employee ID, name, and department name. If an employee does not belong to any department, the ‘COALESCE‘ function will simply replace the NULL department name with ‘No Department,’ and it also ensures a more user-friendly presentation of the data.

Conclusion

Overall, FULL OUTER JOIN is a powerful tool, when you need an extensive view of data from two tables, as well as both matched and unmatched rows. It ensures that no information is escape from the final result set and provides a complete picture of the data relationships. Make sure that your SQLite version supports the FULL OUTER JOIN syntax. Becasue some older versions might require alternative approaches using UNION clauses to achieve a similar result. It can through errors for not supporting the syntax. The concepts of FULL OUTER JOIN will help you doing better data analysis and reporting as well.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads