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
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PostgreSQL
CREATE TABLE table_name ( column_name int4range, another_column daterange );
int4range stores ranges of integers.
daterange stores ranges of dates.
Examples
PostgreSQL
SELECT int4range(1, 5);
PostgreSQL
SELECT daterange('2024-01-01', '2024-01-10');
PostgreSQL
SELECT * FROM bookings WHERE booking_dates @> '2024-01-05'::date;
Sample Program
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.
PostgreSQL
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;
Important Notes
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.
Summary
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
1. What does the PostgreSQL
int4range data type store?easy
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]
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
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]
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
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]
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:
But it returns no rows even though some bookings are inside June. What is the likely problem?
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
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]
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
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]
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
