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)
SQL AND & OR Operator – Interview Questions
Q 1: Why use AND and OR together?
Ans: To build complex logical conditions.
Q 2: How is precedence handled?
Ans: AND has higher precedence than OR.
Q 3: Why are parentheses important?
Ans: They control the order of evaluation.
Q 4: Can multiple AND and OR be used together?
Ans: Yes, in a single WHERE clause.
Q 5: Does incorrect grouping cause errors?
Ans: It may cause logical errors in results.
SQL AND & OR Operator – Objective Questions (MCQs)
Q1. What is the purpose of combining AND and OR?
Q2. Which operator has higher precedence?
Q3. Parentheses are used to:
Q4. Which query is correct?
Q5. Combined AND & OR use which logic?