0
0
PostgreSQLquery~20 mins

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

Choose your learning style9 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.