Lesson 10 of 42
Compound Logic: AND, OR, NOT & Operator Precedence in MySQL
Overview & Learning Objectives
Real-world business rules combine multiple criteria simultaneously—such as targeting orders placed in Koramangala OR Indiranagar that also exceeded ₹500. Combining `AND`, `OR`, and `NOT` requires understanding operator precedence: in SQL, `AND` has higher precedence than `OR`. Omitting parentheses around `OR` conditions is the #1 cause of silent data filtering bugs.
- Construct compound multi-criteria filters using `AND`, `OR`, and `NOT`.
- Understand SQL operator precedence (`NOT` > `AND` > `OR`).
- Use explicit parentheses to group logical conditions deterministically.
Industry Standard
Standard: Analytics engineering code reviews at Stripe mandate wrapping all `OR` conditions in explicit parentheses to eliminate precedence ambiguity.
Real-World Example: ScoopCraft VIP Promo Eligibility Filter
ScoopCraft targets customers located in Whitefield OR Indiranagar who spent over ₹1,000.
| Customer Name | Dark Store Hub | Total Spend | Without Parens (Bugged) | With Parens (Correct) |
|---|---|---|---|---|
| Ananya Sen | Whitefield Hub | ₹250.00 | INCLUDED (Bug: OR bypassed spend check) | EXCLUDED (Correct) |
| Rahul Sharma | Indiranagar Hub | ₹1,200.00 | INCLUDED | INCLUDED |
| Priya Nair | Whitefield Hub | ₹1,450.00 | INCLUDED | INCLUDED |
| Vikram Patel | Koramangala Hub | ₹1,800.00 | EXCLUDED | EXCLUDED |
Syntax & Core SQL Implementation
-- BUGGY: AND binds tighter than OR!
-- Evaluates as: (dark_store = 'Whitefield') OR (dark_store = 'Indiranagar' AND total > 1000)
SELECT * FROM orders
WHERE dark_store = 'Whitefield Hub' OR dark_store = 'Indiranagar Hub' AND order_total > 1000;
-- CORRECT: Explicit parentheses enforce correct grouping
SELECT * FROM orders
WHERE (dark_store = 'Whitefield Hub' OR dark_store = 'Indiranagar Hub')
AND order_total > 1000;
Parameters & Arguments Breakdown
AND: Requires both left and right predicates to evaluate to TRUE.OR: Requires at least one predicate to evaluate to TRUE.NOT: Inverts the truth value of the succeeding boolean expression.( ... ) Parentheses: Overrides default operator precedence, guaranteeing contained conditions evaluate first.
Sample Walkthrough (Q&A)
Question: What is the precedence order among NOT, AND, and OR in SQL?
SQL Pattern: NOT > AND > OR
Result: NOT evaluates first, then AND, and finally OR
Explanation: Without parentheses, AND expressions group together first, potentially bypassing outer OR conditions.
Common Mistakes & Limitations
- Writing `WHERE col = "A" OR col = "B" AND col2 > 10` without parentheses, accidentally including all "A" records regardless of col2.
- Applying `NOT` carelessly with `AND`/`OR` without applying De Morgan's Laws (`NOT (A AND B)` equals `NOT A OR NOT B`).
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Auto-Indent Query | Tab / Shift + Tab | Tab / Shift + Tab |
Pro Tips & Modern Best Practices
- Substitute repetitive `col = "A" OR col = "B" OR col = "C"` chains with the cleaner and faster `col IN ("A", "B", "C")` syntax.
- Always use defensive parentheses around complex boolean logic even if you know the operator precedence rules by heart.