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;
✗ Incorrect
Only option A correctly uses INTERVAL with a quoted string specifying '5 hours'.
What is the result type when subtracting two timestamps in PostgreSQL?
AInteger
BInterval
CDate
DText
✗ Incorrect
Subtracting two timestamps returns an interval representing the time difference.
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'
✗ Incorrect
The correct syntax is to list units in order, e.g., '1 month 10 days'.
Which of these adds 30 minutes to a timestamp?
Atimestamp + INTERVAL '30 minutes'
Btimestamp + INTERVAL 30
Ctimestamp + '30 minutes'
Dtimestamp + INTERVAL '30'
✗ Incorrect
Only option A correctly uses INTERVAL with a quoted string specifying the unit.
What does this query return? SELECT CURRENT_DATE - INTERVAL '1 day';
AYesterday's date
BTomorrow's date
CCurrent date plus one day
DError
✗ Incorrect
Subtracting 1 day interval from current date returns yesterday's date.
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.
Practice
(1/5)
1. What does the following PostgreSQL expression do? SELECT CURRENT_DATE + INTERVAL '5 days';
easy
A. Returns the current date without change
B. Subtracts 5 days from the current date
C. Causes a syntax error
D. Adds 5 days to the current date
Solution
Step 1: Understand the CURRENT_DATE keyword
CURRENT_DATE returns today's date without time.
Step 2: Add interval of 5 days
Adding INTERVAL '5 days' increases the date by 5 days.
Final Answer:
Adds 5 days to the current date -> Option D
Quick Check:
Adding interval '5 days' = date + 5 days [OK]
Hint: Adding INTERVAL 'X days' shifts date forward by X days [OK]
Common Mistakes:
Confusing addition with subtraction
Thinking CURRENT_DATE includes time
Using wrong interval syntax
2. Which of the following is the correct syntax to subtract 3 months from a date column named order_date?
easy
A. order_date - '3 months'
B. order_date - INTERVAL '3 months'
C. order_date - INTERVAL 3 months
D. order_date - INTERVAL '3 month'
Solution
Step 1: Identify correct interval syntax
INTERVAL must be followed by a quoted string with number and unit, e.g., '3 months'.
Step 2: Check subtraction from date column
Subtracting INTERVAL '3 months' from order_date is valid syntax.
Final Answer:
order_date - INTERVAL '3 months' -> Option B
Quick Check:
Subtract INTERVAL '3 months' with quotes [OK]
Hint: Always quote interval value like '3 months' [OK]
Common Mistakes:
Omitting quotes around interval string
Using singular 'month' instead of plural 'months'
Trying to subtract string without INTERVAL keyword
3. What is the result of this query if today is 2024-06-15?
Hint: Add then subtract intervals stepwise for correct date [OK]
Common Mistakes:
Subtracting before adding
Confusing day and month units
Not casting to date and including time
4. Identify the error in this query:
SELECT CURRENT_DATE + INTERVAL 5 days;
medium
A. Missing quotes around interval value
B. CURRENT_DATE cannot be used with intervals
C. INTERVAL keyword is misspelled
D. Adding intervals to dates is not allowed
Solution
Step 1: Check INTERVAL syntax
INTERVAL requires the value to be a quoted string, e.g., '5 days'.
Step 2: Identify missing quotes error
The query lacks quotes around 5 days, causing syntax error.
Final Answer:
Missing quotes around interval value -> Option A
Quick Check:
INTERVAL '5 days' needs quotes [OK]
Hint: Always put interval value in single quotes [OK]
Common Mistakes:
Writing INTERVAL 5 days without quotes
Assuming CURRENT_DATE can't be used with intervals
Confusing INTERVAL keyword spelling
5. You want to find all orders from a table orders where the order_date is within the last 3 months from today. Which query correctly uses date arithmetic with intervals?
hard
A. SELECT * FROM orders WHERE order_date >= CURRENT_DATE - INTERVAL '3 months';
B. SELECT * FROM orders WHERE order_date <= CURRENT_DATE + INTERVAL '3 months';
C. SELECT * FROM orders WHERE order_date > CURRENT_DATE + INTERVAL '3 months';
D. SELECT * FROM orders WHERE order_date < CURRENT_DATE - INTERVAL '3 months';
Solution
Step 1: Understand the requirement
We want orders from the last 3 months, so order_date should be after (or equal to) 3 months ago.
Step 2: Use CURRENT_DATE minus 3 months
Subtracting INTERVAL '3 months' from CURRENT_DATE gives the date 3 months ago.
Step 3: Write condition for order_date
order_date >= CURRENT_DATE - INTERVAL '3 months' selects orders within last 3 months.
Final Answer:
SELECT * FROM orders WHERE order_date >= CURRENT_DATE - INTERVAL '3 months'; -> Option A
Quick Check:
Filter dates after CURRENT_DATE - 3 months [OK]
Hint: Use >= CURRENT_DATE - INTERVAL 'X months' for recent dates [OK]
Common Mistakes:
Using + INTERVAL instead of - INTERVAL
Using < instead of >= for filtering recent dates
Confusing direction of date comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.