0
0
PostgreSQLquery~20 mins

Interval type for durations in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate total duration in hours using INTERVAL
Given a table tasks with a column duration of type INTERVAL, what is the total duration in hours for all tasks?
PostgreSQL
SELECT SUM(duration) AS total_duration FROM tasks;
ASELECT SUM(duration)::int AS total_hours FROM tasks;
BSELECT EXTRACT(EPOCH FROM SUM(duration))/3600 AS total_hours FROM tasks;
CSELECT SUM(duration) / INTERVAL '1 hour' AS total_hours FROM tasks;
DSELECT SUM(duration) * 3600 AS total_hours FROM tasks;
Attempts:
2 left
💡 Hint
Use EXTRACT with EPOCH to convert INTERVAL to seconds, then convert to hours.
📝 Syntax
intermediate
1:30remaining
Identify the correct INTERVAL literal syntax
Which of the following is the correct way to write an INTERVAL literal representing 3 days in PostgreSQL?
A'3 days'::INTERVAL
BINTERVAL 3 'days'
CINTERVAL '3 days'
DINTERVAL '3 day'
Attempts:
2 left
💡 Hint
INTERVAL literals require the value and unit inside single quotes as one string.
query_result
advanced
2:00remaining
Find tasks longer than 1 hour using INTERVAL comparison
Given a table tasks with a column duration of type INTERVAL, which query correctly returns tasks with duration longer than 1 hour?
ASELECT * FROM tasks WHERE duration > INTERVAL '1 hour';
BSELECT * FROM tasks WHERE duration > INTERVAL 1 HOUR;
CSELECT * FROM tasks WHERE duration > 1;
DSELECT * FROM tasks WHERE duration > '1 hour';
Attempts:
2 left
💡 Hint
INTERVAL literals must be quoted strings; direct numbers or unquoted units cause errors.
optimization
advanced
2:30remaining
Optimize query summing durations grouped by category
You have a tasks table with columns category (text) and duration (INTERVAL). Which query efficiently returns total duration per category?
ASELECT category, SUM(duration) AS total_duration FROM tasks GROUP BY category ORDER BY category;
BSELECT category, SUM(duration)::text AS total_duration FROM tasks GROUP BY category;
CSELECT category, SUM(duration) / INTERVAL '1 second' AS total_duration_seconds FROM tasks GROUP BY category;
DSELECT category, SUM(duration) * INTERVAL '1 second' AS total_duration FROM tasks GROUP BY category;
Attempts:
2 left
💡 Hint
Summing INTERVALs directly and grouping by category is efficient and clear.
🔧 Debug
expert
3:00remaining
Why does this INTERVAL addition query fail?
Consider the query:
SELECT start_time + '2 hours' FROM events; where start_time is a TIMESTAMP column. Why does this query fail?
ABecause TIMESTAMP cannot be added to INTERVAL in PostgreSQL.
BBecause addition operator '+' is not supported for TIMESTAMP and INTERVAL.
CBecause '2 hours' is a string, it must be cast to INTERVAL explicitly.
DBecause the syntax requires INTERVAL keyword before the string literal.
Attempts:
2 left
💡 Hint
PostgreSQL requires INTERVAL keyword before interval literals in expressions.