In this tutorial, we will learn how to use AND
, OR
and NOT
Operator with WHERE
clause to select record based on one or more conditions.
AND
, OR
and NOT
are the boolean operators which are used to define the multiple conditions in the WHERE
clause.
AND
and OR
Operators are used to filtering the records based on one or more conditions.
It returns a record if all the condition which is separated by AND
operator is satisfied or true.
SELECT column1,column2.....columnN
FROM tableName
WHERE [CONDITION1] AND [CONDITION2] AND...[CONDITION-N]
It returns a record if atleast one of the condition which is separated by OR
operator is satisfied or true.
SELECT column1,column2.....columnN
FROM tableName
WHERE [CONDITION1] OR [CONDITION2] OR...[CONDITION-N]
It returns a record if the condition is false or not true.
SELECT column1,column2.....columnN
FROM tableName
WHERE NOT [CONDITION]
Let us consider this table "Employee" for records.
Table Name: Employee
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
8 | Sarah | New York | US | female | 76000 |
9 | Rocky | Noida | India | male | 28000 |
For finding an employee whose City is "Delhi" and Country is "India", a query will be:-
SELECT * FROM Employee
WHERE City='Delhi' AND Country='India';
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
For finding an employee whose City is "Delhi" OR Country is "India", a query will be:-
SELECT * FROM Employee
WHERE City='Delhi' OR Country='India'
ID | EmpName | City | Country | Gender | Salary |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
9 | Rocky | Noida | India | male | 28000 |
For finding employee whose City is NOT "Delhi", query will be:-
SELECT * FROM Employee
WHERE NOT City='Delhi'
ID | EmpName | City | Country | Gender | Salary |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
8 | Sarah | New York | US | female | 76000 |
9 | Rocky | Noida | India | male | 28000 |
For finding employee whose Salary greater then 15000 and Salary less than 28000 or Gender not equals to "female", query will be:-
SELECT * FROM Employee
WHERE (Salary>15000 AND Salary<28000)
OR NOT Gender='female'
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
4 | Kapil | Noida | India | male | 25000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |