Sql Operator

AND & OR operator

AND & OR operators are used with Where clause to make more precise conditions for fetching data from database by combining more than one condition together.

AND operator

AND operator is used to set multiple conditions with Where clause.

Example of AND

Consider the following emp table

eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * from emp WHERE salary < 10000 AND age > 25; 

The above query will return records where salary is less than 10000 and age greater than 25.

eid name age salary
402 Shane 29 8000
405 Tiger 35 9000

OR operator

OR operator is also used to combine multiple conditions with Where clause. The only difference between AND and OR is their behaviour. When we use AND to combine two or more than two conditions, records satisfying all the condition will be in the result. But in case of OR, atleast one condition from the conditions specified must be satisfied by any record to be in the result.

Example of OR

Consider the following emp table

eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * from emp WHERE salary > 10000 OR age > 25; 

The above query will return records where either salary is greater than 10000 or age greater than 25.

402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000

Admin has written 171 articles

Leave a Reply