MySQL NOT Operator

MySQL | NOT Operator: Learn about the MySQL NOT Operator, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 12, 2022

NOT Operator

When we specifically don't want any condition then in such case we use NOT operator. This operator can be really helpful sometimes when you strictly avoid some particular data.

NOT Operator Syntax

SELECT column_name1, column_name2, ...
FROM table_name
WHERE NOT condition;

Let us see some examples;

Suppose we have a table named "student_details" and inside that table, we have the following columns;

NOT Operator Example 1

Case 1: Suppose I want to know the names of all departments other than the "CSE", in such case we can simply use the NOT operator as we only don't want that department,

SELECT * FROM student_details 
WHERE NOT student_department='CSE';
NOT Operator Example 2

According to the condition applied we have got all the students' names from all the departments other than the "CSE" department.

Case 2: Suppose we want to know the names of all the "male" students from all the branches other than "CSE". In this case, we will use two NOT operators with one AND operator. AND operator is used here because we want both conditions to be followed.

SELECT * FROM student_details 
WHERE NOT student_department='CSE' AND NOT gender='Female';
NOT Operator Example 3

As you can see from the result, we have got all the names of "male" students from all the departments other than "CSE".


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.