Mastering MySQL for Data AnalystsLesson 9 of 42
Lesson 9 of 42

Filtering Data: WHERE Clause, Comparison Operators & Null-Safe Equal (<=>)

Overview & Learning Objectives

Filtering allows data analysts to isolate records meeting exact business requirements. The `WHERE` clause applies boolean predicate logic against each candidate row. In addition to standard ANSI operators (`=`, `!=`, `<`, `>`), MySQL provides the unique Null-Safe Equal operator (`<=>`), which allows comparing values where either side may be `NULL` without resulting in `UNKNOWN`.

  • Filter records using standard comparison operators (`=`, `!=` / `<>`, `>`, `<`, `>=`, `<=`).
  • Understand three-valued boolean logic in SQL (TRUE, FALSE, UNKNOWN).
  • Master MySQL's unique Null-Safe Equal operator (`<=>`).

Industry Standard

Standard: Production database guidelines at Goldman Sachs enforce indexed column filtering in the `WHERE` clause to enable index seek operations rather than full table scans.

Real-World Example: ScoopCraft High-Value Delivery Orders

ScoopCraft isolates all orders with basket values exceeding ₹500 delivered from the Koramangala dark store.

order_iddark_storeorder_amountdelivery_minCriteria Met?
ORD-301Koramangala Hub620.0018.5YES (amount > 500 AND store = Koramangala)
ORD-302Indiranagar Hub750.0022.0NO (Wrong store)
ORD-303Koramangala Hub340.0014.0NO (Amount < 500)

Syntax & Core SQL Implementation

-- Standard comparison filtering
SELECT order_id, dark_store, order_amount
FROM orders
WHERE dark_store = 'Koramangala Hub'
  AND order_amount > 500.00;

-- MySQL Null-Safe Equal (<=>)
SELECT order_id, discount_code
FROM orders
WHERE discount_code <=> NULL;

Parameters & Arguments Breakdown

  • =: Tests equality between two expressions (returns UNKNOWN if either operand is NULL).
  • != or <>: Tests inequality between two expressions.
  • <=> (Null-Safe Equal): MySQL-exclusive operator: returns 1 (TRUE) if both operands are NULL or equal, and 0 (FALSE) otherwise.

Sample Walkthrough (Q&A)

Question: Why does "WHERE discount_code = NULL" return zero rows even if discount_code has NULL values?

SQL Pattern: WHERE discount_code = NULL

Result: Returns 0 rows (Empty Set)

Explanation: In SQL three-valued logic, any comparison with NULL using standard = evaluates to UNKNOWN, which the WHERE clause rejects. You must use "IS NULL" or MySQL's "<=> NULL".

Common Mistakes & Limitations

  • Using `= NULL` or `!= NULL` instead of `IS NULL` / `IS NOT NULL`.
  • Filtering on expressions wrapped around indexed columns (e.g. `WHERE YEAR(order_date) = 2026`), which invalidates B-Tree index lookups.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute Current StatementCtrl + EnterCmd + Enter

Pro Tips & Modern Best Practices

  • Use `col >= "2026-01-01" AND col < "2027-01-01"` instead of `YEAR(col) = 2026` to allow MySQL to utilize range index scans.
  • The `<=>` operator is particularly useful in dynamic stored procedures or joins where matching NULL to NULL is intentional.