SQL AND & OR Operator

Sometimes we need to combine AND & OR condition in one query. We can apply SELECT, INSERT, UPDATE, and DELETE Statement.

In SELECT Statement

Syntax:-


SELECT column(s) 
FROM table_name 
WHERE condition1
AND condition2
-------------
OR conditionN

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     |

Now, we have to get the employees which last_name is Tailor and country is not Australia OR id is 6.
SELECT * FROM employees 
where (last_name='Tailor' AND country!='Australia') 
OR id=6;

Output:-

+----+------------+-----------+---------------+---------+
| id | first_name | last_name | email         | country |
+----+------------+-----------+---------------+---------+
|  1 | John       | Tailor    | john@abc.com  | USA     |
|  2 | Rom        | Tailor    | rom@abc.com   | USA     |
|  6 | Virat      | Kohli     | virat@abc.com | India   |
+----+------------+-----------+---------------+---------+
3 rows in set (0.00 sec)