Challenge - 5 Problems
Interval Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Use EXTRACT with EPOCH to convert INTERVAL to seconds, then convert to hours.
✗ Incorrect
PostgreSQL INTERVAL sums return an INTERVAL type. To get total hours as a number, extract total seconds (EPOCH) and divide by 3600.
📝 Syntax
intermediate1: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?
Attempts:
2 left
💡 Hint
INTERVAL literals require the value and unit inside single quotes as one string.
✗ Incorrect
The correct syntax is INTERVAL followed by a single quoted string with number and unit, e.g., INTERVAL '3 days'.
❓ query_result
advanced2: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?Attempts:
2 left
💡 Hint
INTERVAL literals must be quoted strings; direct numbers or unquoted units cause errors.
✗ Incorrect
Only option A uses correct INTERVAL syntax for comparison. Option A treats '1 hour' as text, causing error or wrong comparison. Option A compares INTERVAL to integer, invalid. Option A syntax is invalid.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Summing INTERVALs directly and grouping by category is efficient and clear.
✗ Incorrect
Option A sums INTERVALs per category correctly and orders results. Option A casts to text unnecessarily. Option A divides INTERVAL by INTERVAL, yielding numeric seconds but less readable. Option A multiplies INTERVAL by INTERVAL, invalid operation.
🔧 Debug
expert3: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?Attempts:
2 left
💡 Hint
PostgreSQL requires INTERVAL keyword before interval literals in expressions.
✗ Incorrect
The string '2 hours' alone is not recognized as INTERVAL. The correct syntax is start_time + INTERVAL '2 hours'. Without INTERVAL keyword, it causes an error.