0
0
PostgreSQLquery~5 mins

Interval type for durations in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the interval type used for in PostgreSQL?
The interval type stores a duration of time, like days, hours, minutes, or seconds, representing a span between two points in time.
Click to reveal answer
beginner
How do you create an interval of 3 days in PostgreSQL?
You can write INTERVAL '3 days' to represent a duration of three days.
Click to reveal answer
beginner
Can you add an interval to a timestamp in PostgreSQL? Give an example.
Yes. For example, SELECT timestamp '2024-06-01 10:00' + INTERVAL '2 hours'; adds 2 hours to the timestamp.
Click to reveal answer
intermediate
What units can you use inside an interval literal in PostgreSQL?
You can use units like years, months, days, hours, minutes, seconds, and combinations like '1 year 2 months 3 days'.
Click to reveal answer
advanced
How does PostgreSQL store intervals internally?
PostgreSQL stores intervals as three parts: months, days, and microseconds, allowing flexible representation of durations.
Click to reveal answer
Which of these is a valid interval literal in PostgreSQL?
AINTERVAL '5 hours 30 minutes'
BINTERVAL 5 hours 30 minutes
C'5 hours 30 minutes' INTERVAL
DINTERVAL(5 hours 30 minutes)
What does SELECT INTERVAL '1 day' + INTERVAL '2 hours'; return?
AAn interval of 3 days
BAn error because you cannot add intervals
CAn interval of 1 day and 2 hours
DA timestamp value
How do you subtract 30 minutes from a timestamp in PostgreSQL?
Atimestamp - INTERVAL '30 minutes'
Btimestamp + INTERVAL '30 minutes'
Ctimestamp - '30 minutes'
Dtimestamp - 30
Which part is NOT stored separately inside a PostgreSQL interval?
AMonths
BYears
CMicroseconds
DDays
What happens if you add an interval to a date in PostgreSQL?
AYou get a timestamp with time zone
BYou get an error
CThe interval is ignored
DYou get a new date shifted by the interval
Explain what the PostgreSQL interval type is and how you can use it with timestamps.
Think about how you tell time differences or add time in real life.
You got /4 concepts.
    Describe the internal storage structure of PostgreSQL intervals and why it matters.
    Consider how different units of time are handled inside the database.
    You got /4 concepts.