SQL AND & OR



AND OR – SQL Operators :

  1. AND & OR operators are used to select records based based on more than one condition.
  2. SQL AND operator displays a record if both the first condition AND the second condition are true.
  3. SQL OR operator displays a record if either the first condition OR the second condition is true.
OperatorCondition 1Condition 2Record Selected
ANDtruetrueRecord is Selected
ANDfalsetrueRecord is not Selected
ANDtruefalseRecord is not Selected
ANDfalsefalseRecord is not Selected
ORtruetrueRecord is Selected
ORfalsetrueRecord is Selected
ORtruefalseRecord is Selected
ORfalsefalseRecord is not Selected

Demo Example

Consider following “CUSTOMER” table –

IDNAMEAGEADDRESSSALARY
1Raj20Pune1000.00
2Saurabh20Pune6000.00
3Omkar24Mumbai4000.00
4Anand23Pune3000.00
5Anmol29Pune1000.00
6Poonam25Delhi9000.00

AND Operator :

Suppose we need to select all the customers that are living in Pune and having customer id greater than or equal to 2 then we can write below query –

SQL> Select * from CUSTOMER where ID >=2 AND CITY = 'PUNE'

Above query will provide you following records

IDNAMEAGEADDRESSSALARY
2Saurabh20Pune6000.00
4Anand23Pune3000.00
5Anmol29Pune1000.00

OR Operator :

We need to select all the customers that are living in Pune or having their age greater than 23 then Consider the below query –

SQL> Select * from CUSTOMER where CITY = 'PUNE' OR AGE > 23

Query will provide you following result –

IDNAMEAGEADDRESSSALARY
1Raj20Pune1000.00
2Saurabh20Pune6000.00
3Omkar24Mumbai4000.00
4Anand23Pune3000.00
5Anmol29Pune1000.00
6Poonam25Delhi9000.00