Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL Query to Delete Duplicate Columns
Next article icon

SQL Query to Exclude Multiple Values

Last Updated : 13 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Using the Database

To use the GeeksforGeeks database use the below command:

USE GeeksforGeeks

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)  );

Verifying the Table:

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

EXEC sp_columns student_details;

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');

Verifying the Inserted Data

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

SELECT* FROM employee_details;

Example Queries With the Syntax:

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

Using NOT IN:

Syntax:

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

Query:

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

Using AND:

Syntax:

SELECT * FROM table_name  WHERE condition1 AND condition2 AND condition3;

Query:

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

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

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

Next Article
SQL Query to Delete Duplicate Columns

L

lokeshpotta20
Improve
Article Tags :
  • SQL
  • Blogathon
  • Blogathon-2021
  • SQL-Query

Similar Reads

  • 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
  • SQL Query for Matching Multiple Values in the Same Column
    Querying multiple values within a single column is a vital skill in SQL, enabling users to filter and retrieve data based on specific criteria. Whether we're working with large datasets or simple tables, mastering techniques like the IN clause, LIKE operator, and comparison operators (e.g., >=) e
    4 min read
  • How to Get Multiple Counts With One SQL Query?
    Efficiency is important in database management, and performing operations like data retrieval should be optimized. Obtaining multiple counts in a single query is a useful technique to enhance performance and streamline queries. Instead of executing separate queries for different conditions, we can u
    6 min read
  • SQL Query to Delete Duplicate Columns
    Through this article, we will learn how to delete duplicate columns from a database table. As we know that duplicity in our database tends to be a waste of memory space. It records inaccurate data and is also unable to fetch the correct data from the database. To remove the duplicate columns we use
    2 min read
  • SQL Query to Find Unique Column Values From Table
    Finding unique column values is a common task in SQL when analysing data or ensuring data integrity. By using the DISTINCT clause, we can efficiently retrieve unique values from a specified column, avoiding duplicate results. In this article, we will explain the use of the DISTINCT clause with detai
    3 min read
  • How to Ignore 0(zero) Values in SQL Query?
    In SQL, many times we are required to display all the rows of a table in which the entries having 0(zero) as an entry/value are ignored. This is achieved through REPLACE command in SQL. In this article, we will discuss how to ignore 0(zero) and replace them with an empty space in SQL. For this artic
    2 min read
  • SQL Query to Delete Duplicate Rows
    Duplicate rows in a database can cause inaccurate results, waste storage space, and slow down queries. Cleaning duplicate records from our database is an essential maintenance task for ensuring data accuracy and performance. Duplicate rows in a SQL table can lead to data inconsistencies and performa
    6 min read
  • SQL Query to Find the Sum of all Values in a Column
    In SQL, calculating the sum of values in a column is a crucial task for performing data analysis and generating reports. The SUM() function helps to calculate the total sum of numeric values from a column, which is especially useful in scenarios like finding total sales, total employees, or total re
    5 min read
  • SQL - Multiple Column Ordering
    SQL is the standard language used for managing and manipulating databases. One of its powerful features is the ability to sort data using the ORDER BY clause, allowing us to arrange the query results in a meaningful way. By default, SQL sorts data in ascending order, but we can customize it to sort
    4 min read
  • How to Get Multiple Counts With Single Query in SQLite?
    In data analysis, obtaining multiple counts for different categories is a common requirement. SQLite, a lightweight and versatile database management system, offers a powerful feature that allows us to achieve this efficiently. In this article, we'll explore how to use SQLite to retrieve multiple co
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences