0
0
PostgreSQLquery~20 mins

BETWEEN for range filtering in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BETWEEN Range Filtering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
ARows with price less than 20
BRows with price 10, 15, and 20
CRows with price 10 and 20 only
DRows with price 11 to 19 only
Attempts:
2 left
💡 Hint
BETWEEN includes both the start and end values.
📝 Syntax
intermediate
2: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)?
ASELECT * FROM orders WHERE order_date BETWEEN '01/01/2023' AND '01/31/2023';
BSELECT * FROM orders WHERE order_date BETWEEN 2023-01-01 AND 2023-01-31;
CSELECT * FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-01-31';
DSELECT * FROM orders WHERE order_date BETWEEN '2023/01/01' AND '2023/01/31';
Attempts:
2 left
💡 Hint
Date literals in PostgreSQL should be in ISO format and enclosed in single quotes.
optimization
advanced
2: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'?
ASELECT * FROM sales WHERE sale_date BETWEEN '2023-01-01' AND '2023-01-31';
BSELECT * FROM sales WHERE sale_date >= '2023-01-01' AND sale_date <= '2023-01-31';
CSELECT * FROM sales WHERE sale_date > '2023-01-01' AND sale_date < '2023-01-31';
DSELECT * FROM sales WHERE sale_date IN ('2023-01-01', '2023-01-31');
Attempts:
2 left
💡 Hint
BETWEEN is often optimized by the database engine for range queries on indexed columns.
🔧 Debug
advanced
2:00remaining
Why does this BETWEEN query return no rows?
Given the table 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'.
ABecause the query syntax is invalid for TIMESTAMP columns
BBecause BETWEEN does not work with TIMESTAMP columns
CBecause the dates are in the wrong format
DBecause '2023-03-11' is treated as '2023-03-11 00:00:00', excluding later times that day
Attempts:
2 left
💡 Hint
Consider how date strings without time default in TIMESTAMP comparisons.
🧠 Conceptual
expert
2:00remaining
BETWEEN behavior with NULL values
Consider a table 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?
ARows with NULL age are excluded because NULL comparisons return UNKNOWN
BRows with NULL age are included because BETWEEN treats NULL as a wildcard
CThe query returns an error due to NULL values in BETWEEN
DRows with NULL age are included only if 30 or 40 is NULL
Attempts:
2 left
💡 Hint
Remember how SQL treats NULL in comparisons.