Skip to content

SQL Ranges

Multiranges

Multiranges in PostgreSQL 14

test=# SELECT int4multirange(
int4range(10, 20),
int4range(12, 25),
int4range(38, 42)
);
int4multirange
-------------------
{[10,25),[38,42)}
(1 row)

overlapping ranges?

  • it’ll resolve into a non-overlapping set 🤯

Range Operators

<@ - range/element is contained by

SELECT int4range(3,5) <@ int4range(2,8);  -- t

@> - contains range, element

SELECT int4range(3,5) @> int4range(3,4);  -- t
SELECT '[2014-01-01,2014-04-01)'::tsrange @> '2014-03-10'::timestamp;  -- t

Last update: 2022-11-04