0
0
PostgreSQLquery~20 mins

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

Choose your learning style9 modes available
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.