WHERE with BETWEEN range in SQL - Time & Space Complexity
We want to understand how the time to run a SQL query changes when we use a WHERE clause with a BETWEEN range.
Specifically, how does the size of the data affect the time it takes to find rows within a range?
Analyze the time complexity of the following code snippet.
SELECT *
FROM sales
WHERE sale_date BETWEEN '2023-01-01' AND '2023-01-31';
This query selects all sales records where the sale date is in January 2023.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning rows to check if sale_date is within the given range.
- How many times: Once for each row in the sales table.
As the number of rows in the sales table grows, the database must check more rows to find those in the date range.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Using BETWEEN always makes the query very fast regardless of table size."
[OK] Correct: Without an index, the database still checks every row, so bigger tables take longer.
Understanding how filtering with ranges affects query time helps you explain how databases handle data efficiently.
"What if we added an index on sale_date? How would the time complexity change?"
Practice
WHERE column BETWEEN 10 AND 20 do?Solution
Step 1: Understand BETWEEN clause behavior
The BETWEEN clause includes both the start and end values in the range.Step 2: Apply to the given range 10 to 20
This means values equal to 10, 20, or any number between them are selected.Final Answer:
Selects rows where the column value is between 10 and 20, including 10 and 20 -> Option BQuick Check:
BETWEEN includes boundaries = B [OK]
- Thinking BETWEEN excludes boundary values
- Confusing BETWEEN with > and < operators
- Assuming BETWEEN works only with numbers
age is between 18 and 30?Solution
Step 1: Recall correct BETWEEN syntax
The correct syntax uses BETWEEN start AND end without commas or parentheses.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.Final Answer:
SELECT * FROM users WHERE age BETWEEN 18 AND 30; -> Option DQuick Check:
BETWEEN ... AND ... syntax = D [OK]
- Using TO instead of AND
- Adding commas between values
- Using parentheses around BETWEEN clause
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.
Solution
Step 1: Identify the range in BETWEEN clause
The query filters prices between 50 and 100, including both 50 and 100.Step 2: Check which prices fall in this range
Prices 50, 75, and 100 are within the range; 45 and 120 are outside.Final Answer:
Rows with prices 50, 75, and 100 -> Option AQuick Check:
BETWEEN includes boundaries, so 50 and 100 included = A [OK]
- Excluding boundary values 50 and 100
- Including values outside the range like 45 or 120
- Confusing BETWEEN with > and < operators
SELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' TO '2023-01-31';
Solution
Step 1: Check BETWEEN syntax
BETWEEN requires AND between the two values, not TO.Step 2: Validate other parts
Dates in quotes are correct, BETWEEN works with dates, and semicolon is optional in many systems.Final Answer:
Using TO instead of AND in BETWEEN clause -> Option AQuick Check:
BETWEEN uses AND, not TO = A [OK]
- Replacing AND with TO in BETWEEN
- Thinking dates can't be quoted
- Assuming semicolon is mandatory
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?
Solution
Step 1: Understand BETWEEN usage
The query selects employees hired from 2020-01-01 to 2022-12-31 inclusive.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.Step 3: Combine conditions
Only employees hired on weekdays within the date range are selected.Final Answer:
Selects employees hired between the dates excluding weekends -> Option CQuick Check:
BETWEEN + weekday filter = C [OK]
- Assuming BETWEEN excludes boundaries
- Confusing day numbers for weekend days
- Thinking EXTRACT causes syntax error
Ready to prove it?
Pass the test and this pattern moves unknown
