Beginner LevelPostgreSQL
What are Range Types (int4range, daterange, tstzrange) and overlap operators (&&)?
❓ Interview Prompt: Explain Range Types (`int4range`, `numrange`, `tsrange`, `tstzrange`, `daterange`), bounds notation (`[`, `)`), overlap (`&&`), and containment (`@>`).
Range Types represent contiguous ranges of scalar values within a single database column:
Range Operators:
•
'&&': Overlap operator (returns true if two ranges share common points).
•
'@>': Containment operator (checks if range contains value or sub-range).
•
Bounds Notation: '[1, 10)' includes 1 but excludes 10.
Code Implementation Example
sql
CREATE TABLE hotel_reservations (
room_id INT,
stay_period DATERANGE
);
-- Query reservations overlapping target date range
SELECT * FROM hotel_reservations
WHERE stay_period && daterange('2026-06-01', '2026-06-15');💡Querying date range overlaps using the && operator.
🎯 Interview Key Takeaway
Use native Range Types (daterange, tstzrange) and GiST indexes to model reservation schedules and time windows.
#Range Types#daterange#&& Operator#Beginner
Back to all PostgreSQL Questions→