Bird
Raised Fist0
PostgreSQLquery~20 mins

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

Choose your learning style10 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.

Practice

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

Solution

  1. Step 1: Understand the purpose of BETWEEN

    The BETWEEN operator checks if a value lies between two given values, including both ends.
  2. Step 2: Compare with other filtering options

    Unlike equality or NULL checks, BETWEEN covers a range inclusively.
  3. Final Answer:

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

    BETWEEN means inclusive range filter [OK]
Hint: BETWEEN includes both range ends, like a closed interval [OK]
Common Mistakes:
  • Thinking BETWEEN excludes the range ends
  • 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 (inclusive)?
easy
A. SELECT * FROM users WHERE age BETWEEN 18 TO 30;
B. SELECT * FROM users WHERE age BETWEEN 18 AND 30;
C. SELECT * FROM users WHERE age >= 18 AND <= 30;
D. SELECT * FROM users WHERE age IN BETWEEN (18, 30);

Solution

  1. Step 1: Recall correct BETWEEN syntax

    The correct syntax uses BETWEEN followed by the lower and upper bounds separated by AND.
  2. Step 2: Check each option

    SELECT * FROM users WHERE age BETWEEN 18 AND 30; uses BETWEEN 18 AND 30 correctly; others have syntax errors or wrong keywords.
  3. Final Answer:

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

    BETWEEN ... AND ... is correct syntax [OK]
Hint: BETWEEN always uses AND, not TO or commas [OK]
Common Mistakes:
  • Using TO instead of AND
  • Using incomplete comparison operators
  • Misplacing parentheses or commas
3. Given the table orders with a column order_date of type DATE, what will this query return?

SELECT order_id FROM orders WHERE order_date BETWEEN '2023-01-01' AND '2023-01-31';
medium
A. Orders placed exactly on January 1, 2023, only.
B. All orders placed before January 1, 2023.
C. All orders placed after January 31, 2023.
D. All orders placed from January 1, 2023, to January 31, 2023, inclusive.

Solution

  1. Step 1: Understand the BETWEEN date range

    The query filters orders where order_date is between '2023-01-01' and '2023-01-31', including both dates.
  2. Step 2: Interpret the result set

    It returns all orders placed on any date from January 1 to January 31, 2023, inclusive.
  3. Final Answer:

    All orders placed from January 1, 2023, to January 31, 2023, inclusive. -> Option D
  4. Quick Check:

    BETWEEN includes both start and end dates [OK]
Hint: BETWEEN dates includes both start and end days [OK]
Common Mistakes:
  • Assuming BETWEEN excludes the end date
  • Confusing BETWEEN with less than or greater than only
  • Thinking it returns only one date
4. Identify the error in this query:

SELECT * FROM products WHERE price BETWEEN 100, 500;
medium
A. The BETWEEN clause uses a comma instead of AND between values.
B. The price column cannot be used with BETWEEN.
C. BETWEEN requires parentheses around the values.
D. The query is correct and will run without errors.

Solution

  1. Step 1: Check BETWEEN syntax

    BETWEEN requires two values separated by AND, not a comma.
  2. Step 2: Identify the syntax error

    The query uses a comma between 100 and 500, which is invalid syntax.
  3. Final Answer:

    The BETWEEN clause uses a comma instead of AND between values. -> Option A
  4. Quick Check:

    BETWEEN needs AND, not commas [OK]
Hint: BETWEEN always uses AND, never commas [OK]
Common Mistakes:
  • Using commas instead of AND
  • Adding unnecessary parentheses
  • Assuming BETWEEN works with any separator
5. You want to find all employees hired between January 1, 2020, and December 31, 2022, but exclude those hired exactly on January 1, 2020. Which query achieves this?
hard
A. All of the above queries will work correctly.
B. SELECT * FROM employees WHERE hire_date > '2020-01-01' AND hire_date <= '2022-12-31';
C. SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-02' AND '2022-12-31';
D. SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2022-12-31' AND hire_date <> '2020-01-01';

Solution

  1. Step 1: Understand the requirement

    We want employees hired after January 1, 2020, up to December 31, 2022, excluding January 1, 2020 itself.
  2. Step 2: Analyze each query

    SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2022-12-31' AND hire_date <> '2020-01-01'; uses BETWEEN including '2020-01-01' but excludes it with <> condition.
    SELECT * FROM employees WHERE hire_date > '2020-01-01' AND hire_date <= '2022-12-31'; uses > and <= to exclude '2020-01-01' naturally.
    SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-02' AND '2022-12-31'; shifts the BETWEEN range to start from '2020-01-02', excluding '2020-01-01'.
  3. Step 3: Confirm all queries meet the condition

    All three queries exclude '2020-01-01' and include dates up to '2022-12-31'.
  4. Final Answer:

    All of the above queries will work correctly. -> Option A
  5. Quick Check:

    Multiple ways to exclude start date in range [OK]
Hint: Exclude start date by > or adjust BETWEEN start or add <> [OK]
Common Mistakes:
  • Assuming BETWEEN excludes start date by default
  • Using BETWEEN without excluding the start date
  • Confusing BETWEEN with exclusive range
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