How to Exclude Records With Certain Values in SQL Select?
Last Updated :
08 Oct, 2021
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 Database
Use the below SQL statement to create a database called GeeksForGeeksDatabase.
Query:
CREATE DATABASE GeeksForGeeksDatabase;

Step 2: Using the Database
Use the below SQL statement to switch the database context to GeeksForGeeksDatabase.
Query:
USE GeeksForGeeksDatabase;

Step 3: Table Definition
Query:
CREATE TABLE Geeks(
GeekID INTEGER PRIMARY KEY,
GeekName VARCHAR(255) NOT NULL,
GeekRank INTEGER NOT NULL,
GeekSchool VARCHAR(255) NOT NULL
);

Step 4: Insert some data into the table
Query:
INSERT INTO Geeks VALUES (101, 'Nix',2 ,'Code Valley School');
INSERT INTO Geeks VALUES (102, 'Rutz',4 ,'Blue Chip School');
INSERT INTO Geeks VALUES (103, 'Shrey',1 ,'GCOEA School');
INSERT INTO Geeks VALUES (104, 'Ankx',3 ,'Round Robin Play School');
INSERT INTO Geeks VALUES (105, 'Ridz',7 ,'Dream School');
INSERT INTO Geeks VALUES (106, 'Mayo',6 ,'Silver Shining School');
INSERT INTO Geeks VALUES (107, 'Bugs',5 ,'Twinkle Star Convent');

You can use the below statement to see the contents of the created table:
Query:
SELECT * FROM Geeks;

Now let’s see how to exclude some records from the table according to certain conditions.
There are many ways to do so, lets see the examples one by one:
Query:
Query to exclude a student from a particular school i.e. Blue Chip School. NOT shows those records where the condition is NOT TRUE.
Note: If we haven't use NOT here then the result would be the opposite.
SELECT * FROM Geeks WHERE NOT GeekSchool = 'Blue Chip School';
This query will output all students except the students with a given school:

We can also exclude some more records by providing where conditions are separated by AND OR operator.
Note: We can also do the same using != operator
Query:
SELECT * FROM Geeks WHERE NOT GeekID > 104;
Now see the difference in how NOT is working. Here in the example, we provided the condition, which when true follows the NOT means the query will select all the rows for which the provided condition is not true.

In the above output, the condition GeekID > 104 is satisfied and due to NOT all the rows are selected which are less than 104.
Note: We can also do the same using != operator
Query:
SELECT * FROM Geeks WHERE GeekID NOT IN (104,101,102,107);
In this query, we are excluding those records (rows) where GeekID does not lie in the provided list (i.e. GeekID should not be 104,101,102,107)
So the resultant data will contain the records excluding the provided Geek ids.

Thus we can apply any condition to any column of the table and exclude those using NOT operator.
Query:
We can also provide subquery in IN operator and can also include one or many conditions using WHERE clause :
SELECT * FROM Geeks WHERE GeekRank NOT IN (SELECT GeekRank FROM Geeks WHERE GeekRank >= 4);

The resultant table selects all the rows which not satisfies the condition GeekRank >=4, So all the geeks with ranks above 4 are selected. We can also combine many conditions together and get different results accordingly.
Similar Reads
How to Select Words With Certain Values at the End of Word in SQL? Selecting words from a string with specific values at the end can be crucial in SQL for tasks like filtering data, pattern matching, and extracting relevant information. In SQL, we often need to work with strings where we need to find words ending with certain characters or patterns. This guide will
4 min read
How to Use Count With Condition in PostgreSQL? In PostgreSQL, the COUNT() function serves as a tool for tallying the number of records within a table. This article aims to address this query, delving into the nuances and implications of integrating conditions into the COUNT() function in PostgreSQL. The COUNT() function in PostgreSQL is traditio
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
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 Exclude Null Values In relational databases, managing NULL values is a critical aspect of data integrity and accuracy. A NULL value signifies the absence of data in a column, distinguishing it from a zero, which is an integer, or a blank space, which is a character. Queries involving NULL values require careful handlin
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