Challenge - 5 Problems
BETWEEN Range Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of BETWEEN with inclusive range
Given a table products with a column
price, what rows will this query return?SELECT * FROM products WHERE price BETWEEN 10 AND 20;PostgreSQL
CREATE TABLE products (id INT, name TEXT, price INT); INSERT INTO products VALUES (1, 'Pen', 5), (2, 'Notebook', 10), (3, 'Backpack', 15), (4, 'Calculator', 20), (5, 'Chair', 25);
Attempts:
2 left
💡 Hint
BETWEEN includes both the start and end values.
✗ Incorrect
The BETWEEN operator in SQL is inclusive, so it includes rows where price is exactly 10 and exactly 20, as well as any values in between.
📝 Syntax
intermediate2:00remaining
Correct syntax for BETWEEN with dates
Which of the following queries correctly filters rows with
order_date between January 1, 2023 and January 31, 2023 (inclusive)?Attempts:
2 left
💡 Hint
Date literals in PostgreSQL should be in ISO format and enclosed in single quotes.
✗ Incorrect
Option C uses the correct ISO date format 'YYYY-MM-DD' with quotes, which PostgreSQL recognizes as dates. Other options either lack quotes or use non-standard formats.
❓ optimization
advanced2:00remaining
Optimizing BETWEEN for indexed columns
You have a large table
sales with an index on sale_date. Which query is most efficient to find sales between '2023-01-01' and '2023-01-31'?Attempts:
2 left
💡 Hint
BETWEEN is often optimized by the database engine for range queries on indexed columns.
✗ Incorrect
BETWEEN is equivalent to >= and <= but is clearer and often optimized by PostgreSQL to use the index efficiently. Option A excludes boundary dates, and D only matches exact dates.
🔧 Debug
advanced2:00remaining
Why does this BETWEEN query return no rows?
Given the table
Assume there are rows with start_time on '2023-03-11 15:00:00'.
events with a start_time column of type TIMESTAMP, why does this query return no rows?SELECT * FROM events WHERE start_time BETWEEN '2023-03-10' AND '2023-03-11';Assume there are rows with start_time on '2023-03-11 15:00:00'.
Attempts:
2 left
💡 Hint
Consider how date strings without time default in TIMESTAMP comparisons.
✗ Incorrect
The string '2023-03-11' is interpreted as midnight at the start of that day, so rows with times later on '2023-03-11' are excluded. To include the whole day, the end boundary should be '2023-03-11 23:59:59' or use < '2023-03-12'.
🧠 Conceptual
expert2:00remaining
BETWEEN behavior with NULL values
Consider a table
Specifically, what happens to rows where
employees with a column age that can be NULL. What will this query return?SELECT * FROM employees WHERE age BETWEEN 30 AND 40;Specifically, what happens to rows where
age is NULL?Attempts:
2 left
💡 Hint
Remember how SQL treats NULL in comparisons.
✗ Incorrect
In SQL, any comparison with NULL returns UNKNOWN, which is treated as false in WHERE clauses. Therefore, rows with NULL in the age column are excluded from the result.