0
0
PostgreSQLquery~20 mins

Time zones and AT TIME ZONE in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Time Zone Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Convert timestamp to a different time zone
Given the timestamp '2024-06-01 15:00:00' in UTC, what is the result of converting it to 'America/New_York' using AT TIME ZONE in PostgreSQL?
PostgreSQL
SELECT '2024-06-01 15:00:00'::timestamp AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York';
A2024-06-01 15:00:00
B2024-06-01 20:00:00
C2024-06-01 10:00:00
D2024-06-01 11:00:00
Attempts:
2 left
💡 Hint
Remember that New York is UTC-4 during daylight saving time in June.
query_result
intermediate
2:00remaining
Effect of AT TIME ZONE on timestamp with time zone
What is the output of this query? SELECT '2024-06-01 15:00:00+00'::timestamptz AT TIME ZONE 'America/Los_Angeles';
PostgreSQL
SELECT '2024-06-01 15:00:00+00'::timestamptz AT TIME ZONE 'America/Los_Angeles';
A2024-06-01 08:00:00
B2024-06-01 22:00:00
C2024-06-01 15:00:00
D2024-06-01 07:00:00
Attempts:
2 left
💡 Hint
Los Angeles is UTC-7 during daylight saving time in June.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in time zone conversion
Which option contains a syntax error when converting timestamp to a time zone in PostgreSQL?
ASELECT '2024-06-01 15:00:00'::timestamp AT TIME ZONE 'UTC';
BSELECT '2024-06-01 15:00:00'::timestamp AT TIMEZONE 'UTC';
CSELECT '2024-06-01 15:00:00'::timestamp AT TIME ZONE 'America/New_York';
DSELECT '2024-06-01 15:00:00+00'::timestamptz AT TIME ZONE 'UTC';
Attempts:
2 left
💡 Hint
Check the exact spelling and spacing of the AT TIME ZONE clause.
optimization
advanced
2:00remaining
Optimizing queries with multiple time zone conversions
Which query is the most efficient way to convert a UTC timestamp to 'Europe/Paris' local time and then back to UTC in PostgreSQL?
ASELECT (ts AT TIME ZONE 'UTC') AT TIME ZONE 'Europe/Paris' AT TIME ZONE 'UTC' FROM (SELECT '2024-06-01 12:00:00'::timestamp AS ts) sub;
BSELECT ts AT TIME ZONE 'UTC' AT TIME ZONE 'Europe/Paris' AT TIME ZONE 'UTC' FROM (SELECT '2024-06-01 12:00:00'::timestamp AS ts) sub;
CSELECT (ts AT TIME ZONE 'Europe/Paris') AT TIME ZONE 'UTC' FROM (SELECT '2024-06-01 12:00:00+00'::timestamptz AS ts) sub;
DSELECT ts AT TIME ZONE 'Europe/Paris' FROM (SELECT '2024-06-01 12:00:00+00'::timestamptz AS ts) sub;
Attempts:
2 left
💡 Hint
Consider the data types and the order of conversions to avoid redundant operations.
🧠 Conceptual
expert
3:00remaining
Understanding AT TIME ZONE behavior with timestamp and timestamptz
Which statement correctly describes the behavior of AT TIME ZONE in PostgreSQL when applied to timestamp without time zone and timestamp with time zone types?
AFor timestamp without time zone, AT TIME ZONE treats the timestamp as local time in the given zone and converts it to UTC; for timestamptz, it converts UTC to the given time zone local time.
BFor timestamp without time zone, AT TIME ZONE converts the timestamp to UTC ignoring the given zone; for timestamptz, it converts UTC to local time in the given zone.
CFor timestamp without time zone, AT TIME ZONE converts the timestamp to the given time zone local time; for timestamptz, it converts local time to UTC.
DFor both timestamp and timestamptz, AT TIME ZONE converts the timestamp to UTC regardless of the given zone.
Attempts:
2 left
💡 Hint
Think about how PostgreSQL treats timestamp types differently with AT TIME ZONE.