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_id | dark_store | order_amount | delivery_min | Criteria Met? |
|---|---|---|---|---|
| ORD-301 | Koramangala Hub | 620.00 | 18.5 | YES (amount > 500 AND store = Koramangala) |
| ORD-302 | Indiranagar Hub | 750.00 | 22.0 | NO (Wrong store) |
| ORD-303 | Koramangala Hub | 340.00 | 14.0 | NO (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)
| Action | Windows | Mac |
|---|---|---|
| Execute Current Statement | Ctrl + Enter | Cmd + 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.