Challenge - 5 Problems
Date Handling Mastery in PostgreSQL
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this date extraction query?
Consider the following PostgreSQL query that extracts the year from a date column. What will be the output?
PostgreSQL
SELECT EXTRACT(YEAR FROM DATE '2023-06-15') AS year;
Attempts:
2 left
💡 Hint
EXTRACT gets a part of the date, like year, month, or day.
✗ Incorrect
The EXTRACT function returns the specified part of the date. Here, it returns the year 2023.
🧠 Conceptual
intermediate2:00remaining
Why is time zone important in PostgreSQL date handling?
Which of the following best explains why handling time zones correctly is important in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about users in different parts of the world viewing the same timestamp.
✗ Incorrect
PostgreSQL stores timestamps with or without time zone differently. Time zones ensure correct display and calculations across regions.
📝 Syntax
advanced2:00remaining
Which query correctly adds 1 month to a date in PostgreSQL?
You want to add exactly one month to the date '2023-01-31'. Which query will correctly do this without error?
Attempts:
2 left
💡 Hint
Casting to date and using INTERVAL is key.
✗ Incorrect
Options A and B correctly cast the string to a date and add an interval of one month. Option C uses invalid syntax. Option D tries to add interval to a string without casting, causing error.
❓ optimization
advanced2:00remaining
How to optimize queries filtering by date range on large tables?
You have a large table with a timestamp column. Which approach optimizes filtering rows between two dates?
Attempts:
2 left
💡 Hint
Avoid wrapping columns in functions in WHERE clauses for performance.
✗ Incorrect
Using direct range conditions allows PostgreSQL to use indexes efficiently. Applying functions on columns disables index use and slows queries.
🔧 Debug
expert2:00remaining
Why does this query return unexpected results with time zones?
Given the table events(event_time TIMESTAMP WITH TIME ZONE), why does this query return no rows?
SELECT * FROM events WHERE event_time::date = '2023-06-15';
Attempts:
2 left
💡 Hint
Think about how time zones affect date extraction from timestamps.
✗ Incorrect
Casting TIMESTAMP WITH TIME ZONE to date uses the session time zone, so local times on '2023-06-15' may fall on a different date, causing no matches.