How to Find Records From One Table Which Don't Exist in Another SQLite?
Last Updated :
19 Mar, 2024
In database management, one of the most common tasks is to compare records either to identify differences or missing records in certain tables. This phase is crucial for data validation, reconciliation, and complete data integrity.
On SQLite, a lightweight relational database management system, this is probably the most straightforward task to do through SQL queries.
This article is to help you know how to find records that are not present in another table in the SQLite database.
Discovering Missing Records in SQLite Tables
In SQLite, to find records from one table that don't exist in another, two methods are commonly used: NOT EXISTS and LEFT JOIN. NOT EXISTS employs a subquery to filter non-existent records, while LEFT JOIN compares tables directly.
We can find records from one table that don't exist in another using two methods:
- Using NOT EXISTS
- Using LEFT JOIN
Let us start by making some easy tables and filling them with some data. We shall create two tables, the EMPLOYEE and ATTENDANCE table whereby the EMPLOYEE table will have more records than the ATTENDANCE table.
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT
);
INSERT INTO employees (employee_id, employee_name) VALUES
(1, 'Minal Pandey'),
(2, 'Vivek Sharma'),
(3, 'Mivi Sharma');
CREATE TABLE attendances (
attendance_id INTEGER PRIMARY KEY,
employee_id INTEGER,
date TEXT
);
INSERT INTO attendances (attendance_id, employee_id, date) VALUES
(1, 1, '2024-03-18'),
(2, 1, '2024-03-19'),
(3, 2, '2024-03-20');
The following is the initial data in both the EMPLOYEE and ATTENDANCE tables:
EMPLOYEE Table data
Employee TableATTENDANCE Table data
Attendance TableNow that we have our tables and data set up, let's perform the operations to find employees who haven't recorded any attendance using both methods.
1. Using NOT EXISTS
NOT EXISTS clause is used to check for the presence of rows in the subquery. It is often used in conjunction with a related subquery to check for the non-existence of the specific records that satisfy the particular conditions.
Example: Find Employees Who Haven't Recorded any Attendance Using NOT EXISTS
SELECT * FROM employees
WHERE NOT EXISTS (
SELECT 1
FROM attendances
WHERE employees.employee_id = attendances.employee_id
);
Explanation:
SELECT * FROM employees: This selects all columns (*) from the employees table.
SELECT 1 FROM attendances WHERE employees.employee_id = attendances.employee_id: This subquery selects the value 1 from the attendances table where there exists a match between employees.employee_id and attendances.employee_id.
WHERE NOT EXISTS (subquery): The NOT EXISTS condition is being used to find if the subquery returns any rows. If the subquery which runs on the employees table do not get any row for a particular employee in the table that means the employee does not have any attendance record in the attendances table.
Output:
Using NOT EXISTS2. Using LEFT JOIN
In the left join option, only the records from the left table will be returned whether there is a match or not. By running this, we can remove the records where the fields from the left table are empty.
Example: Find Employees Who Haven't Recorded any Attendance Using LEFT JOIN
SELECT employees.* FROM employees
LEFT JOIN attendances ON employees.employee_id = attendances.employee_id WHERE attendances.employee_id IS NULL;
Explanation
SELECT employees.* FROM employees: This selects all columns (*) from the employees table.
LEFT JOIN attendances ON employees.employee_id = attendances.employee_id: This executes a left join between the employees and attendances tables according to the column employee_id. This implies that it will consist of all of the records from the employees tab no matter the presence of the matching record in the attendances table.
WHERE attendances.employee_id IS NULL: The join condition here filters the result set so that it selects only the rows that are missing the matching record in the attendances table for each employee.
Output:
Using LEFT JOINConclusion
The SQL queries with the NOT EXISTS clause will enable you to find records in one table that don't exist in another table based on the specified conditions and are more efficient in doing so. This is an essential element for data analysis, validation, and database consistency assurance. With SQLite's simplicity and superiority, such tasks can be done effectively even in lightweight environments.
In conclusion, mastering SQL querying techniques empowers you to manipulate and analyze data effectively, facilitating informed decision-making and ensuring the integrity of your database systems.
Similar Reads
How to Find Records From One Table Which Don't Exist in Another MySQL
MySQL is a free and open-source relational database management system written in C and C++ that is extremely popular among developers. Like other relational database management systems, MySQL provides a variety of rich features to create databases and tables, insert data in them, and further manipul
5 min read
SQL Server - Find Records From One Table Which Don't Exist in Another
When working with databases, it is often necessary to compare data between tables to find records that exist in one table but not in another. In SQL Server, this can be achieved using various methods. In this article, we will explore two common approaches to finding records from one table that don't
3 min read
How to Select All Records from One Table That Do Not Exist in Another Table in SQL?
When working with SQL databases, a common requirement is to find records from one table that do not exist in another table. This can be achieved using various SQL techniques like LEFT JOIN, NOT IN, or NOT EXISTS. In this detailed guide, we will explain how to accomplish this using SQL queries and La
4 min read
How to Update a Table Data From Another Table in SQLite
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. The SQLite offers some features which are that it is a serverless architecture, quick, self-contained, reliable, full-featured SQL database engine. SQLite does not require any server to perform queri
3 min read
How to Select Rows with no Matching Entry in Another Table in SQLite?
In database management, selecting rows from one table that does not have matching entries in another table means returning the rows that are present in one table but do not have the same entry in any other table. This scenario often arises in various data validation and analysis processes. In this a
4 min read
How to Select Rows from a Table that are Not in Another Table?
In MySQL, the ability to select rows from one table that do not exist in another is crucial for comparing and managing data across multiple tables. This article explores the methods to perform such a selection, providing insights into the main concepts, syntax, and practical examples. Understanding
3 min read
SQL Query to Exclude Records if it Matches an Entry in Another Table
In this article, we will see, how to write the SQL Query to exclude records if it matches an entry in another table. We can perform the above function using the NOT IN operator in SQL. For obtaining the list of values we can write the subquery. NOT IN operators acts as a negation of In operator and
3 min read
How to Fetch Data From Two Tables Based on Date in SQL?
In SQL, fetching data from multiple tables based on a specific date range is a common task. By using certain SQL operations, we can combine data from two different tables and filter the results based on a date condition. This method is particularly useful when working with multiple datasets and we n
3 min read
SQL Server Update From One Table to Another Based on an ID Match
In the world of database management, we need to perform various OLTP operations like insert, update, and delete. The ability to efficiently update data between tables is crucial for maintaining data integrity and ensuring accurate information. SQL Server provides powerful tools to accomplish this ta
7 min read
How to Delete a Specific Row from SQLite Table using Python ?
In this article, we will discuss how to delete of a specific row from the SQLite table using Python. In order to delete a particular row from a table in SQL, we use the DELETE query, The DELETE Statement in SQL is used to delete existing records from a table. We can delete a single record or multipl
3 min read