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

Range & List Filtering: BETWEEN, IN, NOT IN & NULL Handling

Overview & Learning Objectives

Filtering continuous numerical ranges or discrete categorical lists is a daily requirement for data analysts. SQL provides `BETWEEN ... AND ...` for clean range checks and `IN (...)` for matching against discrete value sets. Crucially, analysts must understand the dangerous "NOT IN with NULLs" pitfall, where a single NULL inside a subquery causes `NOT IN` to return an empty result set.

  • Filter numeric, date, and text ranges using inclusive `BETWEEN ... AND ...`.
  • Query discrete categorical sets using `IN (...)` and `NOT IN (...)`.
  • Diagnose and avoid the infamous "NOT IN with NULL" trap.

Industry Standard

Standard: Analytics teams at Amazon require using `NOT EXISTS` instead of `NOT IN` when subqueries might contain NULL values to prevent silent query failure.

Real-World Example: ScoopCraft Dark-Store Delivery Batch Filtering

ScoopCraft extracts deliveries completed between 15 and 30 minutes across selected metro dark stores.

order_iddark_storedelivery_minBETWEEN 15 AND 30IN ("BLR-01", "BLR-02")
ORD-401BLR-0118.5 minMATCH (Inside range)MATCH
ORD-402BLR-0324.0 minMATCHNO MATCH (Store BLR-03)
ORD-403BLR-0214.2 minNO MATCH (< 15 min)MATCH

Syntax & Core SQL Implementation

-- Range filtering (Inclusive boundaries)
SELECT order_id, order_total, delivery_min
FROM orders
WHERE delivery_min BETWEEN 15.0 AND 30.0;

-- Discrete set filtering
SELECT order_id, dark_store, order_status
FROM orders
WHERE dark_store IN ('BLR-01', 'BLR-02', 'BLR-04')
  AND order_status NOT IN ('Cancelled', 'Returned');

Parameters & Arguments Breakdown

  • BETWEEN a AND b: Matches values greater than or equal to a and less than or equal to b (inclusive).
  • IN (val1, val2...): Shorthand for a chain of OR equality conditions.
  • NOT IN (val1, val2...): Matches values that are not equal to every single value in the list.

Sample Walkthrough (Q&A)

Question: Why does "WHERE customer_id NOT IN (SELECT customer_id FROM vip_churn)" return zero rows if the subquery contains a single NULL?

SQL Pattern: NOT IN (1, 2, NULL)

Result: Returns Empty Result Set (0 rows)

Explanation: In SQL, x != NULL evaluates to UNKNOWN. Because NOT IN requires (x != 1 AND x != 2 AND x != NULL), the entire condition evaluates to UNKNOWN, returning nothing. Use NOT EXISTS or filter out NULLs.

Common Mistakes & Limitations

  • Assuming `BETWEEN` is exclusive; in SQL, `BETWEEN 10 AND 20` includes both 10 and 20.
  • Using `BETWEEN` on datetime columns with dates only (`BETWEEN "2026-01-01" AND "2026-01-31"`), which excludes records timestamped on Jan 31st after 00:00:00.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Comment Selected LinesCtrl + /Cmd + /

Pro Tips & Modern Best Practices

  • For datetime filtering, use `>= "2026-01-01" AND < "2026-02-01"` instead of `BETWEEN` to safely cover every millisecond of the target month.
  • Always write `WHERE col NOT IN (SELECT col FROM t WHERE col IS NOT NULL)` to guard against the NULL trap.