SQL Query to Exclude Records if it Matches an Entry in Another Table
Last Updated :
17 May, 2022
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 return the results excluding the items present in the specified list.
Syntax: NOT IN
SELECT * FROM table_name WHERE column_name NOT IN (list);
Now, for the demonstration follow the below steps:
Step 1: Create a database
we can use the following command to create a database called geeks.
Query:
CREATE DATABASE geeks;
Step 2: Use the database
Use the below SQL statement to switch the database context to geeks:
Query:
USE geeks;
Step 3: Table definition
We have two tables named 'demo_table1' and 'demo_table2' in our geek’s database.
Query(demo_table1):
CREATE TABLE demo_table1(
NAME VARCHAR(20),
AGE INT,
CITY VARCHAR(20) );
Query(demo_table2):
CREATE TABLE demo_table2(
NAME VARCHAR(20),
AGE int);
Step 4: Insert data into a table
Query(demo_table1):
INSERT INTO demo_table1 VALUES
('Romy',23,'Delhi'),
('Rahul',23,'Delhi'),
('Nikhil',24,'Punjab'),
('Ranvir',23,'Punjab'),
('Samiksha',23,'Banglore'),
('Ashtha',24,'Banglore'),
('Tannu',30,'Patna'),
('Girish',30,'Patna'),
('Ram', 20 , 'Patna'),
('Raj', 12, 'Delhi');
Query(demo_table2):
INSERT INTO demo_table2 VALUES
('Fanny',25 ),
('Prem', 30),
('Preeti',21),
('Samita',32),
('Rahul',23),
('Ranvir',23);
Step 5: View the content
Execute the below query to see the content of the table
Query:
SELECT * FROM demo_table1;
Output:

Query:
SELECT * FROM demo_table2;
Output:

Step 6: Exclude data from demo_table1 based on matches found in demo_table2
For the demonstration, exclude the data from demo_table1 whose values in the NAME column match the entries in the Name column of demo_table2.
Query:
SELECT * FROM demo_table1 WHERE NAME NOT IN (SELECT NAME FROM demo_table2);
Output:

We can see in the image that two entries are excluded as the values match from entry in demo_table2.
Alternate Method:
Alternatively, to exclude rows from demo_table1 whose values in the NAME column match the rows in the NAME column in demo_table2, we can perform it with left join.
As we know, the left join returns all the rows in the left table and only matches rows in the right table, so the right table column's values will be NULL if they don't have matching rows. Hence, we will this concept of the left join with a condition that the right table's column value is null.
The query for the above content using Left Join and Condition:
select t1.* from demo_table1 t1 left join demo_table2 t2 on t1.name = t2.name where t2.name is null
This returns, the required data set.
Similar Reads
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
SQL Query to Display First 50% Records from Employee Table
Here, we are going to see how to display the first 50% of records from an Employee Table in MS SQL server's databases. For the purpose of the demonstration, we will be creating an Employee table in a database called "geeks". Creating a Database : Use the below SQL statement to create a database call
2 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
SQL Query to Display Last 50% Records from Employee Table
Here, we are going to see how to display the last 50% of records from an Employee Table in MySQL and MS SQL server's databases. For the purpose of demonstration, we will be creating an Employee table in a database called "geeks". Creating a Database : Use the below SQL statement to create a database
2 min read
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
SQL Query to Find Duplicate Names in a Table
Duplicate records in a database can create confusion, generate incorrect results, and waste storage space. Itâs essential to identify and remove duplicates to maintain data accuracy and database performance. In this article, weâll discuss the reasons for duplicates, how to find duplicate records in
3 min read
How to Update From One Table to Another Based on an ID Match in SQL
In SQL, updating data between tables is a common operation used to maintain data consistency and accuracy across related datasets. Whether we need to synchronize records, update fields, or correct discrepancies, SQL provides efficient methods to achieve this. In this article, we will explain how to
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
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