;

SQL AND, OR and NOT


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.

SQL AND, OR and NOT Operators

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.

AND Operator:

It returns a record if all the condition which is separated by AND operator is satisfied or true.

Syntax - AND Operator
SELECT column1,column2.....columnN
FROM tableName
WHERE [CONDITION1] AND [CONDITION2] AND...[CONDITION-N]

OR Operator:

It returns a record if atleast one of the condition which is separated by OR operator is satisfied or true.

Syntax - OR Operator
SELECT column1,column2.....columnN
FROM tableName
WHERE [CONDITION1] OR [CONDITION2] OR...[CONDITION-N]

NOT Operator:

It returns a record if the condition is false or not true.

Syntax - NOT Operator
SELECT column1,column2.....columnN
FROM tableName
WHERE NOT [CONDITION]

Example:

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

Example of AND Operator

For finding an employee whose City is "Delhi" and Country is "India", a query will be:-

Example - AND Operator
SELECT * FROM Employee
WHERE City='Delhi' AND Country='India';
Output - AND Operator
ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000

Example of OR Operator

For finding an employee whose City is "Delhi" OR Country is "India", a query will be:-

Example - OR Operator
SELECT * FROM Employee
WHERE City='Delhi' OR Country='India'
Output - OR Operator
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

Example of NOT Operator

For finding employee whose City is NOT "Delhi", query will be:-

Example - NOT Operator
SELECT * FROM Employee
WHERE NOT City='Delhi'
Output - NOT Operator
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

Nested Condition AND, OR and NOT Operator

For finding employee whose Salary greater then 15000 and Salary less than 28000 or Gender not equals to "female", query will be:-

Example  
SELECT * FROM Employee
WHERE (Salary>15000 AND Salary<28000)
OR NOT Gender='female'
Output
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