Bird
Raised Fist0
PostgreSQLquery~20 mins

Range types (int4range, daterange) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Range Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
Output of int4range containment check
What is the output of this query?

SELECT int4range(1, 5) @> 3 AS contains_three;
PostgreSQL
SELECT int4range(1, 5) @> 3 AS contains_three;
Atrue
BSyntaxError
Cfalse
Dnull
Attempts:
2 left
💡 Hint
The operator @> checks if the range contains the value.
query_result
intermediate
2:00remaining
Result of daterange union operation
What is the result of this query?

SELECT daterange('2023-01-01', '2023-01-10') + daterange('2023-01-05', '2023-01-15') AS union_range;
PostgreSQL
SELECT daterange('2023-01-01', '2023-01-10') + daterange('2023-01-05', '2023-01-15') AS union_range;
ASyntaxError
B'[2023-01-01,2023-01-10)'
C'[2023-01-05,2023-01-15)'
D'[2023-01-01,2023-01-15)'
Attempts:
2 left
💡 Hint
The + operator merges overlapping or adjacent ranges.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in int4range creation
Which option contains a syntax error when creating an int4range?

Query example: SELECT int4range(...);
Aint4range(1, 5, '[]')
Bint4range(1, 5, '[)')
Cint4range(5, 1)
D)')[' ,5 ,1(egnar4tni
Attempts:
2 left
💡 Hint
The lower bound must be less than or equal to the upper bound.
🔧 Debug
advanced
2:00remaining
Why does this daterange overlap query return no rows?
Given a table events(date_range daterange), why does this query return no rows?

SELECT * FROM events WHERE daterange('2023-01-01', '2023-01-10') && daterange('2023-01-10', '2023-01-20');
AThe ranges do not overlap because the first ends exactly at the start of the second, and ranges are half-open by default.
BThe query syntax is invalid and causes no rows to be returned.
CThe && operator checks for containment, not overlap.
DThe daterange values are stored as text, so the operator fails silently.
Attempts:
2 left
💡 Hint
Remember how half-open ranges treat their boundaries.
🧠 Conceptual
expert
2:30remaining
Choosing the correct range type for a date interval with inclusive end
You want to store a date interval that includes both the start and end dates (inclusive). Which range type and bounds option should you use in PostgreSQL?
AUse int4range with bounds '[)' and convert dates to integers.
BUse daterange with bounds '[]' to include both start and end dates.
CUse daterange with bounds '(]' to include only the end date.
DUse tsrange with default bounds '[)' for inclusive start and exclusive end.
Attempts:
2 left
💡 Hint
Check how bounds affect inclusion of start and end in ranges.

Practice

(1/5)
1. What does the PostgreSQL int4range data type store?
easy
A. A range of 4-byte floating point numbers
B. A range of 4-byte integers
C. A range of dates
D. A range of text strings

Solution

  1. Step 1: Understand the name int4range

    The prefix int4 means 4-byte integer in PostgreSQL.
  2. Step 2: Identify the stored data type

    int4range stores a range (interval) of 4-byte integers.
  3. Final Answer:

    A range of 4-byte integers -> Option B
  4. Quick Check:

    int4range = integer range [OK]
Hint: Remember int4 means 4-byte integer range [OK]
Common Mistakes:
  • Confusing int4range with floating point ranges
  • Thinking it stores dates instead of integers
  • Assuming it stores text ranges
2. Which of the following is the correct syntax to create a table with a column named period of type daterange?
easy
A. CREATE TABLE events (period daterange[]);
B. CREATE TABLE events (period range(daterange));
C. CREATE TABLE events (period daterange);
D. CREATE TABLE events (period date range);

Solution

  1. Step 1: Recall correct type declaration

    In PostgreSQL, range types like daterange are used directly as column types.
  2. Step 2: Check each option

    CREATE TABLE events (period daterange); uses period daterange which is correct syntax. CREATE TABLE events (period range(daterange)); wrongly uses range(daterange). CREATE TABLE events (period date range); splits the type incorrectly. CREATE TABLE events (period daterange[]); declares an array, not a single range.
  3. Final Answer:

    CREATE TABLE events (period daterange); -> Option C
  4. Quick Check:

    Use type name directly for range columns [OK]
Hint: Use range type name directly as column type [OK]
Common Mistakes:
  • Adding extra parentheses around range type
  • Splitting type name into two words
  • Using array syntax when not needed
3. Given the table bookings with a daterange column stay, what does this query return?
SELECT * FROM bookings WHERE stay && daterange('2024-06-01', '2024-06-10');
medium
A. Rows where stay starts exactly on June 1, 2024
B. Rows where stay is completely inside June 1 to June 10, 2024
C. Rows where stay ends before June 10, 2024
D. Rows where stay overlaps with June 1 to June 10, 2024

Solution

  1. Step 1: Understand the operator && for ranges

    The && operator checks if two ranges overlap at all.
  2. Step 2: Analyze the query condition

    The query selects rows where the stay range overlaps with the range from June 1 to June 10, 2024.
  3. Final Answer:

    Rows where stay overlaps with June 1 to June 10, 2024 -> Option D
  4. Quick Check:

    Range overlap operator && means any overlap [OK]
Hint: && means any overlap between ranges [OK]
Common Mistakes:
  • Thinking && means fully inside
  • Confusing overlap with exact start or end match
  • Assuming it filters only before or after dates
4. You wrote this query to find bookings fully inside June 2024:
SELECT * FROM bookings WHERE stay <@ daterange('2024-06-01', '2024-06-30');

But it returns no rows even though some bookings are inside June. What is the likely problem?
medium
A. The daterange upper bound is exclusive by default, so '2024-06-30' is not included
B. The <@ operator checks for overlap, not containment
C. The stay column is not of type daterange
D. The query syntax is invalid and causes an error

Solution

  1. Step 1: Understand <@ operator meaning

    <@ means the left range is fully contained inside the right range.
  2. Step 2: Check daterange bounds behavior

    By default, daterange upper bound is exclusive, so '2024-06-30' is not included in the range. This excludes bookings on June 30.
  3. Final Answer:

    The daterange upper bound is exclusive by default, so '2024-06-30' is not included -> Option A
  4. Quick Check:

    Upper bound exclusive means end date not included [OK]
Hint: Remember daterange upper bound is exclusive by default [OK]
Common Mistakes:
  • Confusing <@ with overlap operator
  • Assuming inclusive upper bound by default
  • Ignoring data type mismatch
5. You want to find all integer ranges in a table intervals with column int_range (type int4range) that do NOT overlap with [10, 20). Which query correctly finds these non-overlapping ranges?
hard
A. SELECT * FROM intervals WHERE NOT (int_range && int4range(10, 20));
B. SELECT * FROM intervals WHERE int_range && int4range(10, 20);
C. SELECT * FROM intervals WHERE int_range <@ int4range(10, 20);
D. SELECT * FROM intervals WHERE int_range @> int4range(10, 20);

Solution

  1. Step 1: Understand the overlap operator &&

    && returns true if ranges overlap.
  2. Step 2: Find non-overlapping ranges

    To find ranges that do NOT overlap, negate the overlap condition with NOT (int_range && int4range(10, 20)).
  3. Final Answer:

    SELECT * FROM intervals WHERE NOT (int_range && int4range(10, 20)); -> Option A
  4. Quick Check:

    Negate overlap to find non-overlapping ranges [OK]
Hint: Use NOT with && to find non-overlapping ranges [OK]
Common Mistakes:
  • Using && without NOT to find non-overlapping
  • Confusing <@ and @> operators
  • Using containment instead of overlap