0
0
PostgreSQLquery~10 mins

Range types (int4range, daterange) in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Range types (int4range, daterange)
Create Range Value
Store Range in Column
Query Range Column
Check Range Operators
Return Rows Matching Range Condition
END
This flow shows how a range value is created, stored in a column, queried with range operators, and returns matching rows.
Execution Sample
PostgreSQL
CREATE TABLE events (
  id SERIAL PRIMARY KEY,
  event_period DATERANGE
);

INSERT INTO events (event_period) VALUES
  ('[2024-01-01,2024-01-10)'),
  ('[2024-02-01,2024-02-05)');

SELECT * FROM events WHERE event_period && '[2024-01-05,2024-01-15)';
Create a table with a daterange column, insert two date ranges, then select rows where the event_period overlaps a given date range.
Execution Table
StepActionInput/ConditionResult/Output
1Create table 'events' with daterange columnN/ATable created with column event_period of type daterange
2Insert first event_period'[2024-01-01,2024-01-10)'Row inserted with event_period from Jan 1 to Jan 9 (end exclusive)
3Insert second event_period'[2024-02-01,2024-02-05)'Row inserted with event_period from Feb 1 to Feb 4 (end exclusive)
4Query events where event_period overlaps '[2024-01-05,2024-01-15)'event_period && '[2024-01-05,2024-01-15)'Returns first row only, because its range overlaps the query range
5Query endsNo more rowsQuery complete with 1 row returned
💡 Query ends after checking all rows; only the first row's range overlaps the query range.
Variable Tracker
VariableStartAfter Insert 1After Insert 2After Query
events tableempty[{id:1, event_period:'[2024-01-01,2024-01-10)'}][{id:1, event_period:'[2024-01-01,2024-01-10)'}, {id:2, event_period:'[2024-02-01,2024-02-05)'}][{id:1, event_period:'[2024-01-01,2024-01-10)'}]
Key Moments - 2 Insights
Why does the query only return the first row and not the second?
Because the query uses the '&&' operator which checks for overlapping ranges. The first event_period '[2024-01-01,2024-01-10)' overlaps with the query range '[2024-01-05,2024-01-15)', but the second event_period '[2024-02-01,2024-02-05)' does not. See execution_table row 4.
What does the parenthesis ')' mean in the range '[2024-01-01,2024-01-10)'?
It means the range includes the start date '2024-01-01' but excludes the end date '2024-01-10'. So the range covers dates from Jan 1 up to Jan 9. This is why the overlap works as expected. See execution_table rows 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the second event_period inserted?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Check the 'Action' column in execution_table for the insertion steps.
According to variable_tracker, how many rows are in the events table after both inserts?
A2
B1
C0
D3
💡 Hint
Look at the 'After Insert 2' column in variable_tracker for the events table state.
If the query range was '[2024-02-03,2024-02-06)', which rows would the query return?
AOnly the first row
BOnly the second row
CBoth rows
DNo rows
💡 Hint
Consider which event_period overlaps with the new query range based on execution_table logic.
Concept Snapshot
Range types store continuous intervals like dates or integers.
int4range is for integer ranges, daterange for date ranges.
Ranges can be inclusive or exclusive at ends, e.g. '[1,5)' includes 1 but excludes 5.
Use operators like '&&' to check if ranges overlap.
Queries can filter rows based on range conditions easily.
Full Transcript
This visual execution trace shows how PostgreSQL range types like int4range and daterange work. First, a table is created with a range column. Then, rows are inserted with specific ranges. When querying, the range operators such as '&&' check if stored ranges overlap with a given range. The execution table walks through each step: creating the table, inserting rows, and querying with a range condition. The variable tracker shows how the table's data changes after each insert and query. Key moments clarify why only certain rows match the query and what the range boundaries mean. The quiz tests understanding of insertion steps, table state, and range overlap logic. The snapshot summarizes the key points about range types and their usage in queries.