Challenge - 5 Problems
Timestamp Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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';
Attempts:
2 left
💡 Hint
EXTRACT(HOUR FROM timestamp) returns the hour part of the time.
✗ Incorrect
The EXTRACT function with HOUR returns the hour component from the timestamp. Here, the time is 14:45:30, so the hour is 14.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about what stores hours, minutes, and seconds but no day or year.
✗ Incorrect
The
time data type stores only the time of day (hours, minutes, seconds) without any date.📝 Syntax
advanced2: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?Attempts:
2 left
💡 Hint
PostgreSQL uses INTERVAL with a string literal for time intervals.
✗ Incorrect
Option D uses the correct syntax: INTERVAL '3 days'. Options A and C are invalid syntax. Option D tries to add a string directly, which causes an error.
🔧 Debug
advanced2:30remaining
Why does this query raise an error?
Consider this query:
where
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?Attempts:
2 left
💡 Hint
Think about how PostgreSQL handles adding date and time types.
✗ Incorrect
PostgreSQL does not allow direct addition of
date and time types. You must cast the date to timestamp or combine them properly.❓ optimization
expert3: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?Attempts:
2 left
💡 Hint
Consider how indexes work with range queries versus functions on columns.
✗ Incorrect
Option B uses a range condition that can use an index on created_at efficiently. Options B, C, and D apply functions on the column, which prevents index use and slows queries.