Home »
MySQL
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;
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';
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';
As you can see from the result, we have got all the names of "male" students from all the departments other than "CSE".