Working with Range Types in PostgreSQL
📖 Scenario: You are managing a library system that tracks book loans. Each loan has a start date and an end date. You want to store these dates efficiently and check if a book is currently loaned out during a specific date range.
🎯 Goal: Create a table using PostgreSQL range types to store loan periods, insert sample data, and query loans that overlap with a given date range.
📋 What You'll Learn
Create a table called
book_loans with columns loan_id (integer) and loan_period of type daterangeInsert three rows with specific loan periods using
daterange literalsCreate a variable
check_period as a daterange to represent the date range to checkWrite a query to select
loan_id and loan_period from book_loans where loan_period overlaps with check_period💡 Why This Matters
🌍 Real World
Range types are useful for storing intervals like dates, times, or numbers efficiently and querying overlaps, gaps, or containment.
💼 Career
Many database roles require understanding range types to optimize queries for scheduling, booking, or inventory systems.
Progress0 / 4 steps