SQL NOT Operator

NOT operator is used to get the records if the condition is not true.
In SELECT Statement

Syntax:-


SELECT column(s) 
FROM table_name 
WHERE NOT column=value

Example:- Suppose we have employees table which has 7 records


+----+------------+-----------+----------------+-----------+
| id | first_name | last_name | email          | country   |
+----+------------+-----------+----------------+-----------+
|  1 | John       | Tailor    | john@abc.com   | USA       |
|  2 | Rom        | Tailor    | rom@abc.com    | USA       |
|  3 | Andrew     | Symonds   | andrew@abc.com | Australia |
|  4 | Miacle     | Tailor    | miacle@abc.com | Australia |
|  5 | Sachin     | Tendulkar | sachin@abc.com | India     |
|  6 | Virat      | Kohli     | virat@abc.com  | India     |
|  7 | rohit      | NULL      | rohit@abc.com  | India     |

Find the employees that country has not the “USA”


SELECT * FROM employees WHERE NOT country='USA';

Output:-


+----+------------+-----------+----------------+-----------+
| id | first_name | last_name | email          | country   |
+----+------------+-----------+----------------+-----------+
|  3 | Andrew     | Symonds   | andrew@abc.com | Australia |
|  4 | Miacle     | Tailor    | miacle@abc.com | Australia |
|  5 | Sachin     | Tendulkar | sachin@abc.com | India     |
|  6 | Virat      | Kohli     | virat@abc.com  | India     |
|  7 | rohit      | NULL      | rohit@abc.com  | India     |
+----+------------+-----------+----------------+-----------+
5 rows in set (0.00 sec)

SQL NOT Operator – Interview Questions

Q 1: What does NOT do in SQL?
Ans: NOT negates a condition.
Q 2: Can NOT be used with IN and LIKE?
Ans: Yes.
Q 3: Is NOT commonly used in WHERE?
Ans: Yes.
Q 4: Does NOT affect performance?
Ans: It may impact indexing.
Q 5: Can NOT be combined with AND/OR?
Ans: Yes.

SQL NOT Operator – Objective Questions (MCQs)

Q1. What does the NOT operator do?






Q2. NOT operator is used with:






Q3. Which query finds names not starting with 'A'?






Q4. NOT IN returns rows that:






Q5. NOT operator uses which logic?






Related SQL NOT Operator Topics