Return the overlapping portion of two ranges, or an empty range if they do not overlap.
RANGE_INTERSECT(range1, range2)
## Overview Computes the intersection of two ranges. The result is the portion contained by both inputs; if the inputs do not overlap, the result is an empty range. Use this function to find overlapping time windows, numeric ranges, or event intervals. ## Behavior - Returns NULL if either argument is NULL. - Both arguments must have the same element type. - Returns a range value with inclusive or exclusive bounds that match the more restrictive input bounds. - Empty ranges are the identity: intersecting with an empty range returns empty. - Deterministic and side effect free. ## Compatibility - PG-compat alias for the `*` operator on range types.
| Name | Type | Description |
|---|---|---|
range1 | Specifies the first range. Must be of the same type as range2. | |
range2 | Specifies the second range. Must be of the same type as range1. |
-- Overlapping integer ranges
SELECT RANGE_INTERSECT('[1,10]', '[5,15]'); -- '[5,10]'
-- Non-overlapping ranges return an empty range
SELECT RANGE_INTERSECT('[1,5]', '[10,20]'); -- 'empty'
-- Identical ranges
SELECT RANGE_INTERSECT('[1,10]', '[1,10]'); -- '[1,10]'
-- Adjacent but non-overlapping ranges
SELECT RANGE_INTERSECT('[1,5]', '[5,10]'); -- '[5,5]' (single-point overlap)
-- Find overlapping reservation windows
SELECT reservation_id, RANGE_INTERSECT(window, requested) AS overlap
FROM booking.catalog.reservations;