Range types let you store a range of values in one column, like a span of numbers or dates. This helps keep data organized and easy to check if something falls inside that range.
Range types (int4range, daterange) in PostgreSQL
CREATE TABLE table_name ( column_name int4range, another_column daterange );
int4range stores ranges of integers.
daterange stores ranges of dates.
SELECT int4range(1, 5);
SELECT daterange('2024-01-01', '2024-01-10');
SELECT * FROM bookings WHERE booking_dates @> '2024-01-05'::date;
This example creates an events table with a date range column. It inserts three events with their date ranges. Then it finds events happening on June 4, 2024.
CREATE TABLE events ( id SERIAL PRIMARY KEY, event_name TEXT, event_period daterange ); INSERT INTO events (event_name, event_period) VALUES ('Conference', daterange('2024-06-01', '2024-06-05')), ('Workshop', daterange('2024-06-04', '2024-06-06')), ('Holiday', daterange('2024-06-10', '2024-06-15')); -- Find events happening on June 4, 2024 SELECT event_name FROM events WHERE event_period @> '2024-06-04'::date;
Ranges are half-open by default: they include the start value but exclude the end value.
You can use operators like @> to check if a range contains a value.
Range types help simplify queries involving intervals and make data easier to manage.
Range types store intervals of values in one column.
Use int4range for integer ranges and daterange for date ranges.
They make it easy to check if a value is inside a range and to find overlapping ranges.
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
Ready to prove it?
Pass the test and this pattern moves unknown
