0
0
PostgreSQLquery~5 mins

Date arithmetic with intervals in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interval in PostgreSQL?
An interval in PostgreSQL represents a span of time, such as days, hours, or minutes, that can be added to or subtracted from date or timestamp values.
Click to reveal answer
beginner
How do you add 3 days to the current date in PostgreSQL?
You can add 3 days using: SELECT CURRENT_DATE + INTERVAL '3 days';
Click to reveal answer
beginner
What happens if you subtract an interval from a timestamp?
Subtracting an interval from a timestamp moves the timestamp backward by that time span. For example, timestamp - INTERVAL '1 hour' gives the time one hour earlier.
Click to reveal answer
intermediate
Can intervals include multiple units like days and hours together?
Yes, intervals can combine units, e.g., INTERVAL '2 days 4 hours' adds 2 days and 4 hours together.
Click to reveal answer
intermediate
How do you find the difference between two dates as an interval?
Subtract one date from another, e.g., SELECT '2024-06-10'::date - '2024-06-01'::date; returns the number of days as an integer. For timestamps, subtraction returns an interval.
Click to reveal answer
Which SQL expression adds 5 hours to the current timestamp?
ASELECT CURRENT_TIMESTAMP + INTERVAL '5 hours';
BSELECT CURRENT_TIMESTAMP + 5;
CSELECT CURRENT_TIMESTAMP + '5 hours';
DSELECT CURRENT_TIMESTAMP + INTERVAL 5;
What is the result type when subtracting two timestamps in PostgreSQL?
AInteger
BInterval
CDate
DText
How do you represent an interval of 1 month and 10 days?
AINTERVAL '1 month 10 days'
BINTERVAL '10 days 1 month'
CINTERVAL '1 day 10 months'
DINTERVAL '10 months 1 day'
Which of these adds 30 minutes to a timestamp?
Atimestamp + INTERVAL '30 minutes'
Btimestamp + INTERVAL 30
Ctimestamp + '30 minutes'
Dtimestamp + INTERVAL '30'
What does this query return? SELECT CURRENT_DATE - INTERVAL '1 day';
AYesterday's date
BTomorrow's date
CCurrent date plus one day
DError
Explain how to add and subtract intervals from dates or timestamps in PostgreSQL.
Think about how you can move forward or backward in time using intervals.
You got /4 concepts.
    Describe how to find the difference between two timestamps and what type of result you get.
    Consider what happens when you subtract one time from another.
    You got /4 concepts.