Discover how a simple data type can save you hours of confusing date or number checks!
Why Range types (int4range, daterange) in PostgreSQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a list of hotel bookings with check-in and check-out dates written on paper. To find if a new booking overlaps with existing ones, you have to check each date range manually, which is confusing and slow.
Manually comparing date or number ranges means writing many conditions and checking each case. It is easy to make mistakes, miss overlaps, or forget edge cases. This slows down work and causes errors in important data like bookings or schedules.
Range types like int4range and daterange let you store ranges as single values. PostgreSQL provides simple operators to check overlaps, containment, and adjacency, making queries clear, fast, and reliable.
WHERE start_date <= new_end AND end_date >= new_start
WHERE daterange(start_date, end_date, '[]') && daterange(new_start, new_end, '[]')
With range types, you can easily and accurately find overlapping periods or number intervals with simple, readable queries.
A hotel booking system can quickly check if a new reservation conflicts with existing ones by testing if date ranges overlap, preventing double bookings.
Manual range checks are complex and error-prone.
Range types store intervals as single values for easier handling.
Built-in operators simplify overlap and containment queries.
Practice
int4range data type store?Solution
Step 1: Understand the name
The prefixint4rangeint4means 4-byte integer in PostgreSQL.Step 2: Identify the stored data type
int4rangestores a range (interval) of 4-byte integers.Final Answer:
A range of 4-byte integers -> Option BQuick Check:
int4range= integer range [OK]
- Confusing int4range with floating point ranges
- Thinking it stores dates instead of integers
- Assuming it stores text ranges
period of type daterange?Solution
Step 1: Recall correct type declaration
In PostgreSQL, range types likedaterangeare used directly as column types.Step 2: Check each option
CREATE TABLE events (period daterange); usesperiod daterangewhich is correct syntax. CREATE TABLE events (period range(daterange)); wrongly usesrange(daterange). CREATE TABLE events (period date range); splits the type incorrectly. CREATE TABLE events (period daterange[]); declares an array, not a single range.Final Answer:
CREATE TABLE events (period daterange); -> Option CQuick Check:
Use type name directly for range columns [OK]
- Adding extra parentheses around range type
- Splitting type name into two words
- Using array syntax when not needed
bookings with a daterange column stay, what does this query return?SELECT * FROM bookings WHERE stay && daterange('2024-06-01', '2024-06-10');Solution
Step 1: Understand the operator
The&&for ranges&&operator checks if two ranges overlap at all.Step 2: Analyze the query condition
The query selects rows where thestayrange overlaps with the range from June 1 to June 10, 2024.Final Answer:
Rows where stay overlaps with June 1 to June 10, 2024 -> Option DQuick Check:
Range overlap operator && means any overlap [OK]
- Thinking && means fully inside
- Confusing overlap with exact start or end match
- Assuming it filters only before or after dates
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?
Solution
Step 1: Understand
<@operator meaning<@means the left range is fully contained inside the right range.Step 2: Check daterange bounds behavior
By default,daterangeupper bound is exclusive, so '2024-06-30' is not included in the range. This excludes bookings on June 30.Final Answer:
The daterange upper bound is exclusive by default, so '2024-06-30' is not included -> Option AQuick Check:
Upper bound exclusive means end date not included [OK]
- Confusing <@ with overlap operator
- Assuming inclusive upper bound by default
- Ignoring data type mismatch
intervals with column int_range (type int4range) that do NOT overlap with [10, 20). Which query correctly finds these non-overlapping ranges?Solution
Step 1: Understand the overlap operator
&&&&returns true if ranges overlap.Step 2: Find non-overlapping ranges
To find ranges that do NOT overlap, negate the overlap condition withNOT (int_range && int4range(10, 20)).Final Answer:
SELECT * FROM intervals WHERE NOT (int_range && int4range(10, 20)); -> Option AQuick Check:
Negate overlap to find non-overlapping ranges [OK]
- Using && without NOT to find non-overlapping
- Confusing <@ and @> operators
- Using containment instead of overlap
