Bird
Raised Fist0
PostgreSQLquery~20 mins

Date, time, and timestamp types in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Timestamp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this timestamp extraction?
Given the table events with a column event_time of type timestamp, what is the result of this query?
SELECT EXTRACT(HOUR FROM event_time) AS hour FROM events WHERE event_time = '2024-06-15 14:45:30';
PostgreSQL
CREATE TABLE events (event_time timestamp);
INSERT INTO events VALUES ('2024-06-15 14:45:30');
SELECT EXTRACT(HOUR FROM event_time) AS hour FROM events WHERE event_time = '2024-06-15 14:45:30';
A14
B15
C30
D2024
Attempts:
2 left
💡 Hint
EXTRACT(HOUR FROM timestamp) returns the hour part of the time.
🧠 Conceptual
intermediate
1:30remaining
Which data type stores only the time of day without date?
In PostgreSQL, which data type should you use to store only the time of day, without any date information?
Atime
Bdate
Cinterval
Dtimestamp
Attempts:
2 left
💡 Hint
Think about what stores hours, minutes, and seconds but no day or year.
📝 Syntax
advanced
2:00remaining
Which query correctly adds 3 days to a timestamp?
You want to add 3 days to a timestamp column created_at. Which of these queries is syntactically correct in PostgreSQL?
ASELECT created_at + 3 DAY FROM orders;
BSELECT created_at + '3 days' FROM orders;
CSELECT created_at + INTERVAL 3 DAY FROM orders;
DSELECT created_at + INTERVAL '3 days' FROM orders;
Attempts:
2 left
💡 Hint
PostgreSQL uses INTERVAL with a string literal for time intervals.
🔧 Debug
advanced
2:30remaining
Why does this query raise an error?
Consider this query:
SELECT event_date + event_time FROM schedule;

where event_date is of type date and event_time is of type time. Why does it raise an error?
AThe column event_time is not allowed in arithmetic operations.
BThe table schedule does not exist.
CYou cannot add date and time types directly; you must cast one to timestamp.
DThe query is missing a WHERE clause.
Attempts:
2 left
💡 Hint
Think about how PostgreSQL handles adding date and time types.
❓ optimization
expert
3:00remaining
How to optimize filtering by date ignoring time in a timestamp column?
You have a large table logs with a created_at column of type timestamp. You want to find all rows from '2024-06-01' ignoring the time part. Which query is the most efficient for this filter?
ASELECT * FROM logs WHERE DATE_TRUNC('day', created_at) = '2024-06-01';
BSELECT * FROM logs WHERE created_at >= '2024-06-01 00:00:00' AND created_at < '2024-06-02 00:00:00';
CSELECT * FROM logs WHERE created_at::date = '2024-06-01';
DSELECT * FROM logs WHERE TO_CHAR(created_at, 'YYYY-MM-DD') = '2024-06-01';
Attempts:
2 left
💡 Hint
Consider how indexes work with range queries versus functions on columns.

Practice

(1/5)
1. Which PostgreSQL data type should you use to store only the date without any time information?
easy
A. TIMESTAMP
B. TIME
C. INTERVAL
D. DATE

Solution

  1. Step 1: Understand the data types

    DATE stores only the calendar date (year, month, day) without time.
  2. Step 2: Match the requirement

    Since we want only the date without time, DATE is the correct choice.
  3. Final Answer:

    DATE -> Option D
  4. Quick Check:

    Date only = DATE [OK]
Hint: Use DATE for day only, no time needed [OK]
Common Mistakes:
  • Choosing TIMESTAMP which includes time
  • Using TIME which stores only time
  • Confusing INTERVAL with date storage
2. Which of the following is the correct way to declare a column named created_at that stores date and time with time zone in PostgreSQL?
easy
A. created_at TIMESTAMP WITH TIME ZONE
B. created_at TIMESTAMP
C. created_at DATE WITH TIME ZONE
D. created_at TIME WITH TIME ZONE

Solution

  1. Step 1: Identify the correct type for date and time with time zone

    PostgreSQL uses TIMESTAMP WITH TIME ZONE to store both date and time including time zone info.
  2. Step 2: Check the options

    created_at TIMESTAMP WITH TIME ZONE matches the correct syntax and type. The other options are either missing time zone or invalid.
  3. Final Answer:

    created_at TIMESTAMP WITH TIME ZONE -> Option A
  4. Quick Check:

    Date & time + time zone = TIMESTAMP WITH TIME ZONE [OK]
Hint: Use TIMESTAMP WITH TIME ZONE for date, time, and zone [OK]
Common Mistakes:
  • Using TIMESTAMP without time zone when zone is needed
  • Trying to add time zone to DATE or TIME types
  • Incorrect syntax like DATE WITH TIME ZONE
3. Given the table events(event_time TIMESTAMP) and the query:
SELECT event_time::DATE FROM events WHERE event_time > '2024-01-01 12:00:00';
What will be the output type of the query?
medium
A. TIMESTAMP values
B. TIME values
C. DATE values
D. TEXT values

Solution

  1. Step 1: Understand the cast operation

    The ::DATE casts the TIMESTAMP to DATE, removing time part.
  2. Step 2: Analyze the query output

    The SELECT returns only the date part of event_time, so output type is DATE.
  3. Final Answer:

    DATE values -> Option C
  4. Quick Check:

    CAST TIMESTAMP to DATE = DATE output [OK]
Hint: Casting TIMESTAMP::DATE returns only date part [OK]
Common Mistakes:
  • Assuming output remains TIMESTAMP
  • Thinking output is TIME only
  • Confusing cast with string conversion
4. You wrote this query:
SELECT event_time + '1 day' FROM events;
But it gives an error. What is the likely cause?
medium
A. The string '1 day' must be cast to INTERVAL before adding
B. You cannot add intervals to TIMESTAMP in PostgreSQL
C. You must use DATE type for adding days
D. The column event_time is not a date or timestamp

Solution

  1. Step 1: Understand adding intervals to timestamps

    In PostgreSQL, you add intervals to timestamps, but the interval must be of type INTERVAL.
  2. Step 2: Identify the error cause

    The string '1 day' is text and must be cast to INTERVAL using '1 day'::INTERVAL or INTERVAL '1 day'.
  3. Final Answer:

    The string '1 day' must be cast to INTERVAL before adding -> Option A
  4. Quick Check:

    Add INTERVAL to TIMESTAMP, cast string first [OK]
Hint: Cast '1 day' to INTERVAL before adding to timestamp [OK]
Common Mistakes:
  • Adding string directly without casting
  • Assuming TIMESTAMP + string works
  • Using DATE type incorrectly
5. You want to store event start times in UTC but display them in users' local time zones. Which PostgreSQL data type and function combination is best to store and convert these times?
hard
A. Store as TIMESTAMP and use AT TIME ZONE to convert
B. Store as TIMESTAMP WITH TIME ZONE and use AT TIME ZONE to convert
C. Store as TIME and use SET TIMEZONE to convert
D. Store as DATE and use TIMEZONE() to convert

Solution

  1. Step 1: Choose the correct storage type

    TIMESTAMP WITH TIME ZONE stores the exact moment in time with zone info, ideal for UTC storage.
  2. Step 2: Use conversion function

    AT TIME ZONE converts stored UTC times to users' local time zones for display.
  3. Final Answer:

    Store as TIMESTAMP WITH TIME ZONE and use AT TIME ZONE to convert -> Option B
  4. Quick Check:

    Use TIMESTAMP WITH TIME ZONE + AT TIME ZONE for zone conversion [OK]
Hint: Use TIMESTAMP WITH TIME ZONE + AT TIME ZONE for conversions [OK]
Common Mistakes:
  • Storing as TIMESTAMP without time zone loses zone info
  • Using DATE or TIME types which lack full timestamp info
  • Confusing SET TIMEZONE session setting with conversion
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