SQL Query to Exclude Multiple Values - GeeksforGeeks (2024)

To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE statement.

In this article let us see the SQL query to exclude multiple values using both AND and NOT IN clauses.

Creating a Database

Use the below command to create a database named GeeksforGeeks:

CREATE DATABASE GeeksforGeeks

SQL Query to Exclude Multiple Values - GeeksforGeeks (1)

Using the Database

To use the GeeksforGeeks database use the below command:

USE GeeksforGeeks

SQL Query to Exclude Multiple Values - GeeksforGeeks (2)

Creating a Table

Create a table student_details with 4 columns using the following SQL query:

CREATE TABLE student_details(stu_id VARCHAR(8),branch VARCHAR(20),course_code VARCHAR(10),backlogs VARCHAR(10));

SQL Query to Exclude Multiple Values - GeeksforGeeks (3)

Verifying the Table:

To view the description of the tables in the database using the following SQL query:

EXEC sp_columns student_details;

SQL Query to Exclude Multiple Values - GeeksforGeeks (4)

Inserting Data into the Table:

Inserting rows into student_details tables using the following SQL query:

INSERT INTO student_details VALUES ('191401','E.C.E','ECPC-251', 'NO'), ('191302','I.C.E','ICPC-221','YES'), ('191305','I.C.E','ICPC-225','YES'), ('191410','E.C.E','ECPC-251', 'YES'), ('191210','M.E','MEPC-103', 'YES'), ('191215','M.E','MEPC-101', 'NO'), ('191505','C.E','CEPC-501', 'NO'), ('191525','C.E','CEPC-502', 'NO');

SQL Query to Exclude Multiple Values - GeeksforGeeks (5)

Verifying the Inserted Data

Viewing the table student_details after inserting rows by using the following SQL query:

SELECT* FROM employee_details;

SQL Query to Exclude Multiple Values - GeeksforGeeks (6)

Example Queries With the Syntax:

1. Query to find the students other than ‘ECE’, ‘ICE’, ‘ME’

Using NOT IN:

Syntax:

SELECT * FROM table_nameWHERE req_column NOT IN(data1,data2,data3,....)

Query:

SELECT* FROM student_detailsWHERE branch NOT IN ('E.C.E','I.C.E','M.E');

SQL Query to Exclude Multiple Values - GeeksforGeeks (7)

Using AND:

Syntax:

SELECT * FROM table_nameWHERE condition1 AND condition2 AND condition3;

Query:

SELECT* FROM student_detailsWHERE branch<>'E.C.E' AND branch <> 'I.C.E' AND branch<>'M.E';

SQL Query to Exclude Multiple Values - GeeksforGeeks (8)

2. Query to update the backlogs to NO other than students of C.E and M.E.

UPDATE student_detailsSET backlogs='NO' WHERE branch NOT IN ('C.E','M.E');SELECT* FROM student_details;

SQL Query to Exclude Multiple Values - GeeksforGeeks (9)

Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.


Last Updated : 13 Sep, 2021

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

SQL Query to Exclude Multiple Values - GeeksforGeeks (2024)

FAQs

How do you exclude multiple values in SQL? ›

In SQL, you can exclude multiple records from a query result using the “Not in” or “not exists” clauses. Expample Not in SELECT * FROM table_name; WHERE column_name; NOT IN (value1, value2, ...);

How to exclude certain values in SQL query? ›

If you want to write exclude queries using the EXCEPT operator, the syntax is as follows : SELECT column_name1, column_name2, ... FROM table_name1 EXCEPT SELECT column_name1, column_name2, ...

How do you exclude row multiple conditions in SQL? ›

If you want to exclude, simply reverse your conditions. For example, rather than "IN" use "NOT IN" or instead of "<" use ">=". Or, alternately, toss some brackets around your WHERE conditions (all of them in 1 set of brackets) and toss a NOT in front.

How to extract multiple values in SQL? ›

Extracting multiple (comma-separated) values from data columns in SQL Server. In some cases, you may have a table with a column containing comma-separated values. To extract distinct values from this column and use them in a pivot widget, you can use the 'STRING_SPLIT' function in SQL Server.

How to SELECT multiple values with like in SQL? ›

Syntax: SELECT col1, col2, col3... colN WHERE (column_name LIKE 'pattern' OR column name LIKE 'pattern' OR column name LIKE 'pattern') FROM TABLE_NAME; In a SQL query, many like statements are possible.

Can we SELECT multiple distinct values in SQL? ›

In SQL multiple fields may also be added with DISTINCT clause. DISTINCT will eliminate those rows where all the selected fields are identical.

How do I SELECT only different values in SQL? ›

The SELECT DISTINCT statement is used to return only distinct (different) values.

What is the SELECT except value in SQL? ›

The SQL EXCEPT statement returns those records from the left SELECT query, that are not present in the results returned by the SELECT query on the right side of the EXCEPT statement. A SQL EXCEPT statement works very similarly to the way that the minus operator does in mathematics.

How do I restrict values in SQL? ›

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

How does except work in SQL? ›

The EXCEPT clause in SQL helps users combine two SELECT statements and returns distinct rows from the first SELECT statement that are not available in the second SELECT statement. Its rules are similar to the UNION operator and can be compared to subtract operator in relational algebra.

How do you delete multiple rows in SQL with different values? ›

Another way to delete multiple rows is to use the IN operator. DELETE FROM table_name WHERE column_name IN (value 1, value 2, value 3, etc...); If you want to delete all records from the table then you can use this syntax.

How to use multiple and conditions in SQL? ›

To specify an AND condition for two different columns

In the Filter column for the first data column to search, specify the first condition. In the Filter column for the second data column, specify the second condition.

What is coalesce() in SQL? ›

Definition and Usage

The COALESCE() function returns the first non-null value in a list.

Can we pass multiple values in like operator in SQL? ›

7. SQL LIKE with Multiple Values Using OR/AND. You can combine multiple conditions using the LIKE syntax too. For example, use the OR condition to find results that satisfy at least one out of multiple LIKE patterns.

How do I show only different values in SQL? ›

The SQL DISTINCT statement returns only distinct (different) values from a table. It is used when a column contains many duplicate values, and you only want to list the different (distinct) values. 1 SELECT DISTINCT column1, column2,.....

How do I remove multiple characters from a string in SQL? ›

To remove multiple special characters from a string in SQL, use the REPLACE and TRANSLATE functions together. The query will replace a specific string with another string and remove all specified special characters. Sometimes you are better off doing this cleanup in a different language.

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5780

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.