Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Real-world queries rarely filter by a single attribute. You will often need to retrieve records meeting multiple simultaneous criteria (e.g., "Orders placed in Maharashtra AND total > ₹5,000 AND NOT refunded"). Combining multiple conditions is accomplished using the SQL Logical Operators: AND, OR, and NOT.
AND OperatorThe AND operator returns TRUE only if both conditions on either side evaluate to TRUE:
OR OperatorThe OR operator returns TRUE if at least one of the conditions evaluates to TRUE:
NOT OperatorThe NOT operator inverts the boolean truth value of a condition (turning TRUE into FALSE and FALSE into TRUE):
In SQL, logical operators do not evaluate simply from left to right. They adhere to strict Operator Precedence:
Suppose your manager asks: "List all customers from Delhi or Mumbai who have a balance over ₹50,000."
Because AND has higher precedence than OR, MySQL interprets this as: "Give me anyone from Mumbai with balance > ₹50,000 ... OR ANYONE AT ALL FROM DELHI REGARDLESS OF THEIR BALANCE!"
| Condition A | Condition B | A AND B | A OR B | NOT A |
|---|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE | FALSE |
| TRUE | FALSE | FALSE | TRUE | FALSE |
| TRUE | UNKNOWN | UNKNOWN | TRUE | FALSE |
| FALSE | UNKNOWN | FALSE | UNKNOWN | TRUE |
| UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN | UNKNOWN |
AND and OR in the same query. Enclosing sub-conditions in parentheses clarifies your intent to other developers and prevents catastrophic filtering bugs.OR: Queries using OR across multiple unindexed columns often force MySQL to perform a full table scan because standard B-Trees cannot easily satisfy split OR conditions in a single index pass. Prefer using UNION or IN where applicable.WHERE city = 'Pune' OR city = 'Goa' AND status = 'VIP', how does the database engine evaluate the logic?TRUE OR UNKNOWN according to SQL three-valued logic?AND and OR clauses?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Comparison Operators in SQL | Range Filtering with BETWEEN |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.