SQL Server - Find Records From One Table Which Don't Exist in Another
Last Updated :
24 May, 2024
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 exist in another are defined in the article.
SQL Server - Find Records From One Table that Don't Exist in Another
The below method helps us to Find records from one table that don't exist in another SQL server defined below:
Let's set up an environment:
-- Table: employees
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
department_id INT,
hire_date DATE,
salary DECIMAL(10, 2)
);
-- Table: employees_details
CREATE TABLE employees_details (
detail_id INT PRIMARY KEY,
employee_id INT,
address VARCHAR(255),
phone_number VARCHAR(15),
date_of_birth DATE,
CONSTRAINT fk_employee FOREIGN KEY (employee_id) REFERENCES employees(employee_id)
);
-- Sample data for employees table
INSERT INTO employees (employee_id, first_name, last_name, email, department_id, hire_date, salary)
VALUES
(1, 'John', 'Doe', '[email protected]', 101, '2020-01-15', 60000.00),
(2, 'Jane', 'Smith', '[email protected]', 102, '2019-07-20', 65000.00),
(3, 'Michael', 'Johnson', '[email protected]', 101, '2021-03-10', 58000.00),
(4, 'Emily', 'Brown', '[email protected]', 103, '2022-05-05', 62000.00);
-- Sample data for employees_details table
INSERT INTO employees_details (detail_id, employee_id, address, phone_number, date_of_birth)
VALUES
(1, 1, '123 Main St, Cityville', '+1234567890', '1985-08-10'),
(2, 2, '456 Elm St, Townsville', '+1987654321', '1990-03-25'),
(3, 3, '789 Oak St, Villageton', '+1122334455', '1993-11-15'),
(4, 4, '101 Pine St, Hamletown', '+1554433221', '1988-06-20');
employees Table:

employees_details Table:

1. Using NOT EXISTS
- The NOT EXISTS clause is a powerful tool for filtering records based on the absence of corresponding entries in another table.
- This below method involves using a subquery to check if there are no matching records in the second table.
SELECT *
FROM employees e
WHERE NOT EXISTS (
SELECT 1
FROM employees_details ed
WHERE e.employee_id = ed.employee_id
);
Output:
employee_id | first_name | last_name | email | department_id | hire_date | salary
------------|------------|-----------|-------|---------------|------------|--------
4 | Emily | Brown | [email protected] | 103 | 2022-05-05 | 62000.00
Explanation: The query begins with a select statement from the first table in the case of multiple tables it may be employees. It works by using the NOT EXISTS clause with the sub-query that checks if there are no records appearing on the employees_details table based on the employee_id.
If no matching records are found in an inner query, a row from an employees table is returned.
2. Using LEFT JOIN
- The second method combines the records of the first table with those of the second table by applying the LEFT JOIN operation and then removes the records where there is no matching occurrence in the second table.
- This below method is useful for joining tables and identifying unmatched records.
SELECT e.*
FROM employees e
LEFT JOIN employees_details ed ON e.employee_id = ed.employee_id
WHERE ed.employee_id IS NULL;
Output:
employee_id | first_name | last_name | email | department_id | hire_date | salary
------------|------------|-----------|-------|---------------|------------|--------
4 | Emily | Brown | [email protected] | 103 | 2022-05-05 | 62000.00
Explanation: The query first retrieves all records from the first table (employees) and then executes a LEFT JOIN to the employees_details table using the employee_id. The WHERE clause removes any rows that do not have an entry in the employees_details table (indicated by a NULL value in the joined column).
Conclusion
Overall, comparing data between tables in SQL Server to find records that don't exist in another table is a common and important task in database management. By using the NOT EXISTS clause or a LEFT JOIN operation, you can efficiently identify and manage such records, ensuring data integrity and consistency in your database.
Similar Reads
How to Find Records From One Table Which Don't Exist in Another SQLite? 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
4 min read
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
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 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
5 min read
How to Exclude Records With Certain Values in SQL Select? In this article, we will understand how to exclude some records having certain values from a table. For the purpose of demonstration, we will be creating a Participant table in a database called âGeeksForGeeksDatabaseâ. Step 1: Creating the DatabaseUse the below SQL statement to create a database ca
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
8 min read