Bird
Raised Fist0
SQLquery~10 mins

WHERE with BETWEEN range in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - WHERE with BETWEEN range
Start Query
↓
Read WHERE clause
↓
Check if value BETWEEN low AND high
↓
Include row
↓
Return filtered rows
↓
End
The query checks each row's value to see if it falls within the specified range using BETWEEN. Rows inside the range are included; others are excluded.
Execution Sample
SQL
SELECT * FROM products
WHERE price BETWEEN 10 AND 20;
This query selects all products with prices from 10 to 20 inclusive.
Execution Table
StepRow IDpriceCondition: price BETWEEN 10 AND 20Include Row?
1155 BETWEEN 10 AND 20 is FalseNo
221010 BETWEEN 10 AND 20 is TrueYes
331515 BETWEEN 10 AND 20 is TrueYes
442020 BETWEEN 10 AND 20 is TrueYes
552525 BETWEEN 10 AND 20 is FalseNo
6EndQuery ends, all rows checked
💡 All rows checked; 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 - 3 Insights
Does BETWEEN include the boundary values (low and high)?
Yes, BETWEEN includes both the low and high values as shown in rows 2 and 4 where price equals 10 and 20 and rows are included.
What happens if the value is less than the low boundary?
The row is excluded, as shown in row 1 where price is 5, which is less than 10, so it is not included.
Is BETWEEN equivalent to using >= low AND <= high?
Yes, BETWEEN low AND high is the same as checking if value >= low AND value <= high, filtering rows inclusively.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, is the row with price 20 included or excluded?
ADepends on other conditions
BExcluded
CIncluded
DNot checked
💡 Hint
Check row 4 in the execution_table where price is 20 and see the Include Row? column.
At which step does the condition first become true?
AStep 1
BStep 2
CStep 5
DStep 3
💡 Hint
Look at the Condition column in execution_table and find the first 'True' result.
If the BETWEEN range changed to BETWEEN 15 AND 25, which row would now be excluded?
ARow with price 10
BRow with price 15
CRow with price 20
DRow with price 25
💡 Hint
Compare the original range 10-20 with the new range 15-25 and see which price falls outside.
Concept Snapshot
WHERE with BETWEEN range:
- Syntax: WHERE column BETWEEN low AND high
- Includes rows where column >= low AND column <= high
- Boundaries are inclusive
- Filters rows within the specified range
- Simplifies range conditions in queries
Full Transcript
This visual execution shows how the SQL WHERE clause with BETWEEN works. Each row's price is checked to see if it falls between 10 and 20 inclusive. Rows with prices 10, 15, and 20 pass the condition and are included in the result. Rows with prices outside this range are excluded. BETWEEN includes the boundary values. This is equivalent to using >= and <= conditions combined.

Practice

(1/5)
1. What does the SQL clause WHERE column BETWEEN 10 AND 20 do?
easy
A. Selects rows where the column value is greater than 10 but less than 20, excluding 10 and 20
B. Selects rows where the column value is between 10 and 20, including 10 and 20
C. Selects rows where the column value is exactly 10 or exactly 20 only
D. Selects rows where the column value is less than 10 or greater than 20

Solution

  1. Step 1: Understand BETWEEN clause behavior

    The BETWEEN clause includes both the start and end values in the range.
  2. Step 2: Apply to the given range 10 to 20

    This means values equal to 10, 20, or any number between them are selected.
  3. Final Answer:

    Selects rows where the column value is between 10 and 20, including 10 and 20 -> Option B
  4. Quick Check:

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

Solution

  1. Step 1: Recall correct BETWEEN syntax

    The correct syntax uses BETWEEN start AND end without commas or parentheses.
  2. Step 2: Check each option

    SELECT * FROM users WHERE age BETWEEN 18 AND 30; uses correct syntax: BETWEEN 18 AND 30. Others have syntax errors.
  3. Final Answer:

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

    BETWEEN ... AND ... syntax = D [OK]
Hint: BETWEEN always uses AND, never TO or commas [OK]
Common Mistakes:
  • Using TO instead of AND
  • Adding commas between values
  • Using parentheses around BETWEEN clause
3. Given the table products with a column price, what rows will this query return?
SELECT * FROM products WHERE price BETWEEN 50 AND 100;

Assume the prices in the table are: 45, 50, 75, 100, 120.
medium
A. Rows with prices 50, 75, and 100
B. Rows with prices 45, 50, 75, 100, and 120
C. Rows with prices 75 and 100 only
D. Rows with prices 50 and 100 only

Solution

  1. Step 1: Identify the range in BETWEEN clause

    The query filters prices between 50 and 100, including both 50 and 100.
  2. Step 2: Check which prices fall in this range

    Prices 50, 75, and 100 are within the range; 45 and 120 are outside.
  3. Final Answer:

    Rows with prices 50, 75, and 100 -> Option A
  4. Quick Check:

    BETWEEN includes boundaries, so 50 and 100 included = A [OK]
Hint: BETWEEN includes start and end values in results [OK]
Common Mistakes:
  • Excluding boundary values 50 and 100
  • Including values outside the range like 45 or 120
  • Confusing BETWEEN with > and < operators
4. Identify the error in this SQL query:
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' TO '2023-01-31';
medium
A. Using TO instead of AND in BETWEEN clause
B. Dates should not be in quotes
C. BETWEEN cannot be used with dates
D. Missing semicolon at the end

Solution

  1. Step 1: Check BETWEEN syntax

    BETWEEN requires AND between the two values, not TO.
  2. Step 2: Validate other parts

    Dates in quotes are correct, BETWEEN works with dates, and semicolon is optional in many systems.
  3. Final Answer:

    Using TO instead of AND in BETWEEN clause -> Option A
  4. Quick Check:

    BETWEEN uses AND, not TO = A [OK]
Hint: BETWEEN always uses AND, never TO [OK]
Common Mistakes:
  • Replacing AND with TO in BETWEEN
  • Thinking dates can't be quoted
  • Assuming semicolon is mandatory
5. You want to select employees hired between January 1, 2020, and December 31, 2022, inclusive. Which query correctly uses BETWEEN and also excludes employees hired on weekends?
SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2022-12-31' AND EXTRACT(DOW FROM hire_date) NOT IN (0,6);

What does this query do?
hard
A. Selects employees hired between the dates including weekends
B. Selects employees hired only on weekends between the dates
C. Selects employees hired between the dates excluding weekends
D. Syntax error due to EXTRACT usage

Solution

  1. Step 1: Understand BETWEEN usage

    The query selects employees hired from 2020-01-01 to 2022-12-31 inclusive.
  2. Step 2: Analyze EXTRACT(DOW FROM hire_date) condition

    EXTRACT(DOW) returns day of week (0=Sunday, 6=Saturday). NOT IN (0,6) excludes weekends.
  3. Step 3: Combine conditions

    Only employees hired on weekdays within the date range are selected.
  4. Final Answer:

    Selects employees hired between the dates excluding weekends -> Option C
  5. Quick Check:

    BETWEEN + weekday filter = C [OK]
Hint: Use BETWEEN for range and filter weekdays with EXTRACT [OK]
Common Mistakes:
  • Assuming BETWEEN excludes boundaries
  • Confusing day numbers for weekend days
  • Thinking EXTRACT causes syntax error
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