Open In App

How to Select All Records from One Table That Do Not Exist in Another Table in SQL?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Laravel's query builder. The article will also include examples, outputs, and optimized queries for improved database performance.

Why Find Records That Don’t Exist in Another Table?

  • Finding records that don’t exist in another table is a critical operation in data analysis and reporting. It helps identify gaps and discrepancies in datasets, ensuring accurate insights and maintaining data integrity.
  • Common use cases include detecting unprocessed orders by comparing an orders table with a processed_orders table, identifying inactive users by analyzing differences between users and active_users tables, or validating relationships between related tables to ensure consistent and reliable data.
  • This operation is essential for maintaining efficient workflows and ensuring robust database management.

SQL Query to Find Records in One Table But Not in Another

When managing relational databases, it is often necessary to identify records that exist in one table but not in another. This guide demonstrates how to achieve this with practical examples, including table creation, data insertion, and queries to fetch unmatched records.

1. Creating Tables

Consider two tables, employee_details and resigned_employees:

CREATE TABLE employee_details(
emp_id VARCHAR(8),
emp_name VARCHAR(20),
emp_designation VARCHAR(20),
emp_age INT);
CREATE TABLE employee_resigned(
emp_id VARCHAR(8),
emp_name VARCHAR(20),
emp_designation VARCHAR(20),
emp_age INT);

2. Inserting data into the Table 

Next, insert records into the employee_details and employee_resigned tables using the following queries.

INSERT INTO employee_details VALUES
('E40001','PRADEEP','H.R',36),
('E40002','ASHOK','MANAGER',28),
('E40003','PAVAN KUMAR','ASST MANAGER',28),
('E40004','SANTHOSH','STORE MANAGER',25),
('E40005','THAMAN','GENERAL MANAGER',26),
('E40006','HARSH',' ANALYST',25),
('E40007','SAMHITH','GENERAL MANAGER',26),
('E40008','SAMEER','SENIOR ANALYST',25),
('E40009','RISABH','BUSINESS ANALYST',26);
INSERT INTO employee_resigned VALUES('E40001','PRADEEP','H.R',36),
('E40004','SANTHOSH','STORE MANAGER',25),
('E40005','THAMAN','GENERAL MANAGER',26);

3. Verifying the inserted data :

To confirm that the data has been inserted correctly, use the following queries:

View Data in employee_details:

SELECT* FROM employee_details;

Output

employee_details
employee_details

View Data in employee_resigned:

SELECT* FROM employee_resigned;

Output

employee_resigned
employee_resigned

Method 1: Using LEFT JOIN with WHERE NULL

The LEFT JOIN technique is one of the most effective ways to find unmatched records between two tables. It retrieves all records from the left table and only matching records from the right table. The WHERE NULL condition then filters out the rows where no match exists in the right table, leaving only unmatched records.

Query:

SELECT e.* 
FROM employee_details e
LEFT JOIN employee_resigned r
ON e.emp_id = r.emp_id
WHERE r.emp_id IS NULL;

Output

emp_idemp_nameemp_designationemp_age
E40002ASHOKMANAGER28
E40003PAVAN KUMARASST MANAGER28
E40006HARSHANALYST25
E40007SAMHITHGENERAL MANAGER26
E40008SAMEERSENIOR ANALYST25
E40009RISABHBUSINESS ANALYST26

Explanation:

The output contains details of employees in employee_details who are not present in employee_resigned. These are the employees who have not resigned.

Method 2: Using NOT EXISTS

The NOT EXISTS clause is a highly efficient method for finding unmatched records, especially when working with large datasets. It checks for the absence of a matching row in the subquery and returns records from the main query that do not satisfy the condition in the subquery.

Query:

SELECT emp_id,emp_name  
FROM employee_details
WHERE NOT EXISTS
(SELECT *
FROM employee_resigned
WHERE employee_details.emp_id = employee_resigned.emp_id);

Output

emp_idemp_name
E40002ASHOK
E40003PAVAN KUMAR
E40006HARSH
E40007SAMHITH
E40008SAMEER
E40009RISABH

Explanation:

The output lists the employee IDs and names of employees who have not resigned. This method efficiently identifies active employees by excluding those found in the employee_resigned table.

Method 3: Using NOT IN

The NOT IN clause is a straightforward way to find unmatched records by filtering rows from one table based on the absence of corresponding values in another table. It is particularly simple to use when dealing with small or moderately sized datasets.

Query:

SELECT *
FROM employee_details
WHERE emp_id NOT IN
(SELECT emp_id
FROM employee_resigned)

Output

emp_idemp_nameemp_designationemp_age
E40002ASHOKMANAGER28
E40003PAVAN KUMARASST MANAGER28
E40006HARSHANALYST25
E40007SAMHITHGENERAL MANAGER26
E40008SAMEERSENIOR ANALYST25
E40009RISABHBUSINESS ANALYST26

Explanation:

The output includes the complete details of employees who have not resigned, such as their ID, name, designation, and age. By using NOT IN, we can quickly identify records that are exclusive to one table, making it a practical choice for straightforward queries.

Conclusion

Finding records in one table that do not exist in another table is a fundamental operation in SQL and Laravel. Use LEFT JOIN, NOT IN, or NOT EXISTS, depending on our specific use case and dataset size. Laravel provides convenient methods to achieve the same using Eloquent ORM or query builder. By applying the techniques and performance tips shared in this guide, we can efficiently handle unmatched records in our database.


Similar Reads