Mastering MySQL for Data AnalystsLesson 10 of 42
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 NameDark Store HubTotal SpendWithout Parens (Bugged)With Parens (Correct)
Ananya SenWhitefield Hub₹250.00INCLUDED (Bug: OR bypassed spend check)EXCLUDED (Correct)
Rahul SharmaIndiranagar Hub₹1,200.00INCLUDEDINCLUDED
Priya NairWhitefield Hub₹1,450.00INCLUDEDINCLUDED
Vikram PatelKoramangala Hub₹1,800.00EXCLUDEDEXCLUDED

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)

ActionWindowsMac
Auto-Indent QueryTab / Shift + TabTab / 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.