SQL for analysis · core
Dates, time zones and half-open ranges
Dates in a report must be cut in the business time zone (IST for India), and a date range should be written as start inclusive, end exclusive. A day cut in UTC puts orders from midnight to 5:30 am IST on the wrong day.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- India is UTC+5:30, so a UTC day runs from 5:30 am to 5:30 am IST. Orders placed from midnight to 5:30 am IST belong to the previous UTC date.
- Convert first, then cast: (created_at AT TIME ZONE 'Asia/Kolkata')::date in Postgres, DATE(ts, 'Asia/Kolkata') in BigQuery, tz_convert in pandas. Fix the WHERE clause too, not only the SELECT.
- Filter with explicit offsets on the raw column: created_at >= '2026-11-08 00:00+05:30' AND created_at < '2026-11-10 00:00+05:30'. A filter on the raw column can use an index or a partition.
- Use half-open ranges (>= start and < next day). BETWEEN '2026-11-08' AND '2026-11-09' stops at midnight at the start of the 9th, so it drops almost all of that day.
- timestamptz and plain timestamp behave differently with AT TIME ZONE. Know which type the column is before converting.
- Never add 5.5 hours by hand in each query. Pick one reporting time zone for the company and put it in a shared view or a semantic layer.
- The same problem appears in BigQuery, Spark and pandas. The tool changes, the rule does not.
The red pen
Common traps: mistake → fix
Mistake: The database session uses my time zone, so dates are fine
Fix: a server session often runs in UTC; set or convert the zone on purpose.
Mistake: BETWEEN a date and the next date covers two days
Fix: it includes only the first instant of the second day.
Mistake: Fixing the SELECT is enough
Fix: the WHERE clause also needs the zone, or the opening rush is still cut off.
Easy to hard
Questions interviewers ask, easy to hard
- What is the difference between a timestamp and a timestamptz column?
- Why is a UTC date wrong for an Indian daily orders report?
- Why is BETWEEN a bad choice for a date range on a timestamp column?
- A sale starts at midnight IST and the first 5 hours are missing from the report. Find the bug and fix it.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.