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 Row With Max Value on a Column in SQL?
SQL is a powerful language for managing and handling relational databases. A common requirement in database management is to retrieve rows where a specific column has the maximum value. Here, we will look at different ways to do this, depending on different situations and database environments. This
5 min read
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
5 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 Exclude Weekend Days in a SQL Server Query?
With this article, we will learn how to exclude weekend days in a SQL server query. For this task, we use the DATEADD ( ) MS.SQL server function. This function in SQL Server is used, to sum up, a time or a date interval to a specified date then returns the modified date. Syntax : DATEADD(interval, n
2 min read
How to Select the Nth Row in a SQLite Database Table?
In SQLite, selecting a specific row from a table can be a common requirement, especially when dealing with large datasets. In this article, we will explore different methods to select the nth row from a SQLite database table. Whether we're a beginner or an experienced developer understanding these m
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
4 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 Parameterize an SQL Server IN clause
SQL Server IN Clause is used to filter data based on a set of values provided. The IN clause can be used instead of using multiple OR conditions to filter data from SELECT, UPDATE, or DELETE query. The IN clause with parameterized data mainly inside Stored Procedures helps filter dynamic data using
5 min read
How to Find Duplicate Records in SQL?
To find duplicate records in SQL, we can use the GROUP BY and HAVING clauses. The GROUP BY clause allows us to group values in a column, and the COUNT function in the HAVING clause shows the count of the values in a group. Using the HAVING clause with a condition of COUNT(*) > 1, we can identify
3 min read