Bird
Raised Fist0
MySQLquery~10 mins

BETWEEN range filtering in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - BETWEEN range filtering
Start Query
↓
Check BETWEEN condition
↓
Value >= Lower Bound?
No→Exclude Row
↓
Value <= Upper Bound?
No→Exclude Row
↓
Include Row
↓
Return Filtered Rows
The query checks if a value is between two bounds inclusive, including rows that satisfy both conditions.
Execution Sample
MySQL
SELECT * FROM products WHERE price BETWEEN 10 AND 20;
This query selects products with prices from 10 to 20 inclusive.
Execution Table
StepRow IDpriceCheck price >= 10Check price <= 20Include Row?
115FalseN/ANo
2210TrueTrueYes
3315TrueTrueYes
4420TrueTrueYes
5525TrueFalseNo
6EndN/AN/AN/AQuery complete
💡 All rows checked; only rows with price between 10 and 20 inclusive are included.
Variable Tracker
VariableStartAfter Row 1After Row 2After Row 3After Row 4After Row 5Final
priceN/A510152025N/A
Include Row?N/ANoYesYesYesNoN/A
Key Moments - 2 Insights
Why is the row with price 10 included even though it equals the lower bound?
BETWEEN includes the boundary values, so price >= 10 is True for 10, making the row included as shown in execution_table row 2.
Why is the row with price 25 excluded even though it is greater than 10?
Because price <= 20 is False for 25, the row is excluded as shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of the check price >= 10 for Row ID 1?
ATrue
BN/A
CFalse
DUnknown
💡 Hint
Refer to execution_table row 1, column 'Check price >= 10'
At which row does the condition price <= 20 become False?
ARow 5
BRow 4
CRow 2
DRow 3
💡 Hint
Check execution_table column 'Check price <= 20' for the first False value
If the lower bound changed from 10 to 15, which row would now be excluded?
ARow with price 15
BRow with price 10
CRow with price 20
DRow with price 25
💡 Hint
Look at variable_tracker for price and inclusion when lower bound is 10; changing to 15 excludes prices below 15
Concept Snapshot
BETWEEN syntax: value BETWEEN low AND high
Includes rows where value >= low AND value <= high
Bounds are inclusive
Useful for filtering ranges easily
Equivalent to: value >= low AND value <= high
Full Transcript
The BETWEEN operator filters rows where a column's value falls within a specified range, including the boundaries. The query checks each row's value against the lower and upper bounds. If the value is greater than or equal to the lower bound and less than or equal to the upper bound, the row is included in the result. For example, selecting products with prices BETWEEN 10 AND 20 includes prices 10, 15, and 20 but excludes 5 and 25. This behavior is shown step-by-step in the execution table and variable tracker. Remember, BETWEEN is inclusive of the boundary values.

Practice

(1/5)
1. What does the BETWEEN operator do in a MySQL query?
easy
A. Filters rows where a column's value is NULL.
B. Filters rows where a column's value is within a specified range, inclusive.
C. Filters rows where a column's value is outside a specified range.
D. Filters rows where a column's value is exactly equal to one value.

Solution

  1. Step 1: Understand the purpose of BETWEEN

    The BETWEEN operator is used to filter rows where a column's value falls between two values, including both ends.
  2. Step 2: Compare options with definition

    Filters rows where a column's value is within a specified range, inclusive. correctly describes this inclusive range filtering. Other options describe different filters.
  3. Final Answer:

    Filters rows where a column's value is within a specified range, inclusive. -> Option B
  4. Quick Check:

    BETWEEN means inclusive range filter = D [OK]
Hint: BETWEEN includes both start and end values in range [OK]
Common Mistakes:
  • Thinking BETWEEN excludes the end values
  • Confusing BETWEEN with equality (=)
  • Assuming BETWEEN works only with numbers
2. Which of the following is the correct syntax to select rows where the column age is between 18 and 30?
easy
A. SELECT * FROM users WHERE age BETWEEN 18 & 30;
B. SELECT * FROM users WHERE age BETWEEN (18, 30);
C. SELECT * FROM users WHERE age BETWEEN 18 TO 30;
D. SELECT * FROM users WHERE age BETWEEN 18 AND 30;

Solution

  1. Step 1: Recall correct BETWEEN syntax

    The correct syntax uses BETWEEN value1 AND value2 without parentheses or other connectors.
  2. Step 2: Check each option

    SELECT * FROM users WHERE age BETWEEN 18 AND 30; uses correct syntax. Options B, C, and D use invalid syntax with commas, TO, or &.
  3. Final Answer:

    SELECT * FROM users WHERE age BETWEEN 18 AND 30; -> Option D
  4. Quick Check:

    BETWEEN ... AND ... syntax = A [OK]
Hint: BETWEEN syntax always uses AND, no commas or TO [OK]
Common Mistakes:
  • Using commas inside BETWEEN
  • Replacing AND with TO or &
  • Adding parentheses around range values
3. Given the table products with a column price, what will this query return?

SELECT * FROM products WHERE price BETWEEN 50 AND 100;
medium
A. All products with price greater than 50 and less than 100, excluding 50 and 100.
B. All products with price exactly 50 or 100 only.
C. All products with price from 50 up to 100, including both 50 and 100.
D. All products with price less than 50 or greater than 100.

Solution

  1. Step 1: Understand BETWEEN inclusiveness

    BETWEEN includes both boundary values, so prices 50 and 100 are included.
  2. Step 2: Analyze query result

    The query returns products priced from 50 to 100 inclusive, matching All products with price from 50 up to 100, including both 50 and 100..
  3. Final Answer:

    All products with price from 50 up to 100, including both 50 and 100. -> Option C
  4. Quick Check:

    BETWEEN includes boundaries = C [OK]
Hint: BETWEEN includes start and end values in results [OK]
Common Mistakes:
  • Thinking BETWEEN excludes boundary values
  • Confusing BETWEEN with > and < operators
  • Assuming BETWEEN works only for numbers
4. Identify the error in this query:

SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31'
medium
A. No error; the query is correct.
B. BETWEEN cannot be used with date values.
C. The date format is incorrect for BETWEEN.
D. Missing semicolon at the end of the query.

Solution

  1. Step 1: Check BETWEEN usage with dates

    BETWEEN works perfectly with date values in MySQL when dates are in 'YYYY-MM-DD' format.
  2. Step 2: Verify query syntax

    The query syntax is correct; semicolon is optional in many clients and not an error here.
  3. Final Answer:

    No error; the query is correct. -> Option A
  4. Quick Check:

    BETWEEN works with dates correctly = A [OK]
Hint: BETWEEN works with numbers, dates, and text [OK]
Common Mistakes:
  • Thinking BETWEEN can't filter dates
  • Expecting semicolon is mandatory always
  • Using wrong date formats like MM-DD-YYYY
5. You want to find all employees hired between January 1, 2020, and December 31, 2022, but your query returns no rows:

SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2022-12-31 23:59:59';

What is the most likely reason and how to fix it?
hard
A. hire_date column stores only dates without time; remove time part from end date.
B. BETWEEN does not work with datetime values; use >= and <= instead.
C. The date format is wrong; use MM-DD-YYYY format.
D. The query is correct; the table is empty.

Solution

  1. Step 1: Understand data type of hire_date

    If hire_date stores only DATE (no time), comparing with '2022-12-31 23:59:59' fails because time part is ignored or mismatched.
  2. Step 2: Fix the query

    Use '2022-12-31' without time to match date-only values correctly.
  3. Final Answer:

    hire_date column stores only dates without time; remove time part from end date. -> Option A
  4. Quick Check:

    Match data type format in BETWEEN range = B [OK]
Hint: Match data types exactly when using BETWEEN with dates [OK]
Common Mistakes:
  • Adding time to date-only columns in BETWEEN
  • Assuming BETWEEN fails with datetime
  • Using wrong date formats
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard