Bird
Raised Fist0
PostgreSQLquery~10 mins

Interval type for durations in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Interval type for durations
Start: Define Interval
↓
Use INTERVAL keyword
↓
Specify duration (e.g., '2 days')
↓
Store or use interval value
↓
Perform operations (add, subtract, compare)
↓
Get result as duration
The flow shows how to define and use interval values for durations in PostgreSQL, from specifying to using them in calculations.
Execution Sample
PostgreSQL
SELECT INTERVAL '2 days' AS duration;
SELECT NOW() + INTERVAL '1 hour' AS future_time;
SELECT INTERVAL '1 day 2 hours' - INTERVAL '3 hours' AS diff;
These queries create interval durations, add intervals to timestamps, and subtract intervals to find differences.
Execution Table
StepQueryActionResult
1SELECT INTERVAL '2 days' AS duration;Create interval of 2 daysduration = 2 days
2SELECT NOW() + INTERVAL '1 hour' AS future_time;Add 1 hour interval to current timestampfuture_time = current time + 1 hour
3SELECT INTERVAL '1 day 2 hours' - INTERVAL '3 hours' AS diff;Subtract 3 hours from 1 day 2 hoursdiff = 23 hours
4ENDNo more queriesExecution complete
💡 All interval operations executed and returned expected duration results.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
durationNULL2 days2 days2 days2 days
future_timeNULLNULLcurrent time + 1 hourcurrent time + 1 hourcurrent time + 1 hour
diffNULLNULLNULL23 hours23 hours
Key Moments - 2 Insights
Why does subtracting '3 hours' from '1 day 2 hours' result in '23 hours' instead of '21 hours'?
Because '1 day 2 hours' equals 26 hours total. Subtracting 3 hours leaves 23 hours, as shown in execution_table row 3.
Can intervals be added directly to timestamps?
Yes, as in execution_table row 2, adding INTERVAL '1 hour' to NOW() returns a timestamp 1 hour in the future.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1, what is the value of 'duration'?
A2 days
B1 hour
C23 hours
DNULL
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does 'future_time' get assigned a value?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at variable_tracker row for 'future_time' and see when it changes from NULL.
If you subtract INTERVAL '2 hours' instead of '3 hours' in step 3, what would 'diff' be?
A23 hours
B22 hours
C24 hours
D21 hours
💡 Hint
Refer to execution_table row 3 and adjust the subtraction accordingly.
Concept Snapshot
INTERVAL type stores durations like '2 days' or '1 hour'.
Use INTERVAL keyword with quoted strings.
Add intervals to timestamps for date/time math.
Subtract intervals to find duration differences.
Intervals can combine days, hours, minutes, seconds.
Full Transcript
This lesson shows how PostgreSQL uses the INTERVAL type to represent durations. You write INTERVAL followed by a quoted string like '2 days' to create a duration. You can add intervals to timestamps, for example adding '1 hour' to the current time. You can also subtract intervals from each other to find differences, like subtracting '3 hours' from '1 day 2 hours' resulting in '23 hours'. The execution table traces these steps, showing how variables change and what results are returned. Key points include understanding that intervals represent total time durations and can be used in arithmetic with timestamps and other intervals.

Practice

(1/5)
1. What does the interval type in PostgreSQL represent?
easy
A. A numeric value for counting rows
B. A specific date and time
C. A duration of time like hours, days, or months
D. A text string describing time

Solution

  1. Step 1: Understand the interval type purpose

    The interval type is designed to store durations, not fixed points in time.
  2. Step 2: Compare with other types

    Unlike timestamp or date types, interval holds a length of time like '3 days' or '2 hours'.
  3. Final Answer:

    A duration of time like hours, days, or months -> Option C
  4. Quick Check:

    Interval = Duration [OK]
Hint: Interval means duration, not a fixed date/time [OK]
Common Mistakes:
  • Confusing interval with timestamp
  • Thinking interval stores exact dates
  • Assuming interval is a string
2. Which of the following is the correct way to declare an interval of 5 days in PostgreSQL?
easy
A. INTERVAL 5 days
B. INTERVAL '5 days'
C. '5 days' INTERVAL
D. INTERVAL 'days 5'

Solution

  1. Step 1: Recall interval literal syntax

    PostgreSQL requires the interval value to be a string inside single quotes after the INTERVAL keyword.
  2. Step 2: Check each option

    Only INTERVAL '5 days' matches the correct syntax; others are invalid or reversed.
  3. Final Answer:

    INTERVAL '5 days' -> Option B
  4. Quick Check:

    Interval syntax = INTERVAL 'value unit' [OK]
Hint: Use INTERVAL followed by quoted string for durations [OK]
Common Mistakes:
  • Omitting quotes around the duration
  • Placing INTERVAL after the string
  • Swapping number and unit order
3. What is the result of this query?
SELECT CURRENT_DATE + INTERVAL '3 days';
medium
A. The current date minus 3 days
B. The current date as text
C. An error because you cannot add interval to date
D. The date 3 days after today

Solution

  1. Step 1: Understand adding interval to date

    Adding an interval to a date shifts the date forward by that duration.
  2. Step 2: Apply the interval '3 days'

    Adding '3 days' to CURRENT_DATE results in a date 3 days later than today.
  3. Final Answer:

    The date 3 days after today -> Option D
  4. Quick Check:

    Date + Interval = Date shifted forward [OK]
Hint: Adding interval to date moves date forward [OK]
Common Mistakes:
  • Thinking it subtracts days
  • Expecting a text output
  • Believing it causes an error
4. Identify the error in this query:
SELECT CURRENT_TIMESTAMP + '2 hours';
medium
A. Missing INTERVAL keyword before '2 hours'
B. CURRENT_TIMESTAMP cannot be used with intervals
C. Quotes around '2 hours' should be double quotes
D. No error, query is correct

Solution

  1. Step 1: Check interval usage with timestamp

    To add a duration, the value must be an interval type, declared with INTERVAL keyword.
  2. Step 2: Analyze the query

    The string '2 hours' lacks INTERVAL keyword, so PostgreSQL will error.
  3. Final Answer:

    Missing INTERVAL keyword before '2 hours' -> Option A
  4. Quick Check:

    Interval keyword required for durations [OK]
Hint: Always use INTERVAL before quoted duration strings [OK]
Common Mistakes:
  • Omitting INTERVAL keyword
  • Using wrong quotes
  • Assuming timestamp can't add intervals
5. You want to find the date 1 month and 15 days after January 10, 2024. Which query gives the correct result?
hard
A. SELECT DATE '2024-01-10' + INTERVAL '1 month' + INTERVAL '15 days';
B. SELECT DATE '2024-01-10' + INTERVAL '1 month + 15 days';
C. SELECT DATE '2024-01-10' + INTERVAL '45 days';
D. SELECT DATE '2024-01-10' + INTERVAL '1.5 months';

Solution

  1. Step 1: Understand interval addition

    Adding intervals separately ensures correct handling of months and days.
  2. Step 2: Evaluate each option

    SELECT DATE '2024-01-10' + INTERVAL '1 month + 15 days'; tries to combine units in one string, which is invalid syntax. SELECT DATE '2024-01-10' + INTERVAL '45 days'; adds 45 days, which is not exactly 1 month and 15 days. SELECT DATE '2024-01-10' + INTERVAL '1.5 months'; uses fractional months, which PostgreSQL does not support directly.
  3. Final Answer:

    SELECT DATE '2024-01-10' + INTERVAL '1 month' + INTERVAL '15 days'; -> Option A
  4. Quick Check:

    Separate intervals for months and days [OK]
Hint: Add months and days as separate intervals for accuracy [OK]
Common Mistakes:
  • Combining units in one interval string
  • Using fractional months
  • Adding days as total instead of separate
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard