0
0
PostgreSQLquery~5 mins

Range types (int4range, daterange) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a range type in PostgreSQL?
A range type in PostgreSQL represents a continuous set of values between a lower and upper bound, such as numbers or dates. It allows easy querying of intervals without storing each value separately.
Click to reveal answer
beginner
What does int4range represent?
int4range is a built-in PostgreSQL range type that stores a range of 4-byte integers (normal integers). It can represent intervals like [1,10) meaning from 1 up to but not including 10.
Click to reveal answer
beginner
How do you create a daterange value for dates from 2023-01-01 to 2023-01-10 inclusive?
You can write: '[2023-01-01,2023-01-10]'::daterange. The square brackets mean the range includes both start and end dates.
Click to reveal answer
intermediate
How can you check if a value is inside a range in PostgreSQL?
Use the @> operator. For example, int4range(1,10) @> 5 returns true because 5 is inside the range 1 to 10.
Click to reveal answer
intermediate
What is the difference between [1,10) and (1,10] in range types?
[1,10) includes 1 but excludes 10. (1,10] excludes 1 but includes 10. Square brackets mean inclusive, parentheses mean exclusive bounds.
Click to reveal answer
Which PostgreSQL range type would you use to store a range of dates?
Adaterange
Bint4range
Ctextrange
Dnumrange
What does the range notation [5,15) mean?
AIncludes 5 and excludes 15
BExcludes 5 and includes 15
CIncludes both 5 and 15
DExcludes both 5 and 15
Which operator checks if a range contains a value in PostgreSQL?
A<@
B@>
C&&
D<<
How do you cast a string to a range type in PostgreSQL?
AYou cannot cast strings to range types
BUse <code>CAST()</code> only
CUse <code>::</code> operator, e.g. <code>'[1,5]'::int4range</code>
DUse <code>TO_RANGE()</code> function
What will daterange('2023-01-01','2023-01-10', '[]') represent?
ADates from Jan 2 to Jan 9 inclusive
BDates from Jan 1 to Jan 10 excluding Jan 10
CInvalid syntax
DDates from Jan 1 to Jan 10 inclusive
Explain what range types are in PostgreSQL and give examples of when you might use int4range and daterange.
Think about intervals like date bookings or number spans.
You got /4 concepts.
    Describe how to check if a value is inside a range and how to create a range with inclusive or exclusive bounds.
    Remember the symbols for inclusive and exclusive bounds.
    You got /3 concepts.