Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

Finance says October revenue in your dashboard is 1.8 percent lower than in their export. The dashboard query is:

SQL
SELECT SUM(total)
FROM orders
WHERE created_at BETWEEN '2026-10-01' AND '2026-10-31';

created_at is timestamptz, the business is in New York. Find all the reasons the numbers differ and fix the query.

BETWEEN '2026-10-01' AND '2026-10-31' stops at 2026-10-31 00:00:00, so almost all of October 31 is missing, and both literals are read in the session time zone (often UTC) while finance counts New York days, which shifts four hours of September 30 in and four hours of October 31 out. Fix it with a half-open range on New York midnights, created_at >= '2026-10-01 00:00 America/New_York' AND created_at < '2026-11-01 00:00 America/New_York', then reconcile business definitions such as refunds, cancellations and currency, and put the result in one shared, tested reporting view.

Why this question is asked

When finance and engineering disagree on a number, trust in the dashboard disappears quickly, and the conversation becomes political. The cause is usually not one bug but several small ones that add up. Interviewers like this question because it rewards a methodical search: date boundaries, time zones, daylight saving and business definitions, then a fix that prevents the next disagreement. In this answer, we will recap how PostgreSQL reads a date literal against a timestamptz, find each cause of the 1.8 percent gap, write the corrected query, check the business definitions and make the logic shared and tested.

1A Quick Recap of Literals and timestamptz

created_at is a timestamptz, an absolute point in time. When we compare it with the string '2026-10-31', PostgreSQL must turn the string into a timestamptz. A date string without a time becomes midnight, 00:00:00, and a string without a zone is interpreted in the session's TimeZone setting. On most servers and in many connection pools that setting is UTC.

So the dashboard query really means: from 2026-10-01 00:00 UTC up to and including 2026-10-31 00:00 UTC. Both halves of that sentence are wrong for a New York business.

2Cause 1: BETWEEN Drops October 31

BETWEEN is inclusive on both ends, but the end is midnight at the start of October 31. Every order placed later on the 31st is outside the range. October 31, 2026 is a Saturday, and losing a whole day out of 31 is about 3.2 percent of the month on its own.

SQL
SELECT '2026-10-31'::timestamptz; -- 2026-10-31 00:00:00+00 (session TimeZone = UTC)
SELECT TIMESTAMPTZ '2026-10-31 15:20+00'
BETWEEN '2026-10-01' AND '2026-10-31'; -- false: an afternoon order on the 31st is excluded

The fix is a half-open range: greater than or equal to the first instant of October and less than the first instant of November. It includes every fraction of a second of October 31 and never overlaps with November.

3Cause 2: The Day Boundary Is in UTC, Not New York

In October New York is on daylight time, UTC-4. Midnight UTC on October 1 is 20:00 on September 30 in New York, so the dashboard counts the last four hours of September 30 and, with the end bound, misses the last four hours of October 31 even after we fix BETWEEN. Evenings are often busy for online shops, so those hours matter.

What the dashboard counted versus what finance counts
PeriodDashboard (UTC, BETWEEN)Finance (New York days)
Sep 30, 20:00 to 24:00 NYincludedexcluded
Oct 1 to Oct 30, NYincludedincluded
Oct 30, 20:00 to Oct 31, 00:00 NYincludedincluded
Oct 31, 00:00 to 24:00 NYexcludedincluded
Two boundary errors in opposite directions, plus definition differences.

Taken together, the dashboard misses most of a Saturday and gains a September evening. The net effect, plus definition differences we will see below, can easily land at 1.8 percent. The point is not to explain the exact figure from the SQL alone but to find every cause and confirm with data.

4Cause 3: Daylight Saving Near Month Boundaries

A tempting fix is to hard-code the offset, '2026-10-01 00:00-04'. It works for October, but in the US daylight saving time ends on the first Sunday of November, which in 2026 is November 1. From then New York is UTC-5. A November report written with -04 would start an hour early and end an hour early, and a reusable monthly query with a fixed offset is wrong for half the year.

So we name the zone, America/New_York, and let PostgreSQL apply the correct offset for each instant from its time zone database.

SQL
-- corrected October query
SELECT SUM(total)
FROM orders
WHERE created_at >= TIMESTAMPTZ '2026-10-01 00:00 America/New_York' -- 2026-10-01 04:00 UTC
AND created_at < TIMESTAMPTZ '2026-11-01 00:00 America/New_York'; -- 2026-11-01 04:00 UTC
-- the same, for any month passed as a date parameter
SELECT SUM(total)
FROM orders
WHERE created_at >= (:month_start)::timestamp AT TIME ZONE 'America/New_York'
AND created_at < (:month_start + interval '1 month')::timestamp AT TIME ZONE 'America/New_York';

Notice that both conditions compare created_at itself with constants, so an index on created_at still works. Setting the session with SET TIME ZONE 'America/New_York' would also fix the literals, but naming the zone in the query makes it correct no matter which connection runs it.

5Cause 4: Business Definitions

Once the dates are right, compare definitions with finance line by line. Does their export exclude cancelled orders, or subtract refunds, and in which month is a refund for a September order counted? Is revenue in one currency, converted at which rate and on which date? Is the date the order date, the payment capture date or the shipping date? Are tax and shipping included in total?

The practical method is reconciliation: take the finance export at order level, join it to our orders by id, and list the orders that appear on only one side or with different amounts. Grouping those differences by reason usually explains the rest of the gap within an hour.

6One Shared, Tested Definition

The lasting fix is to stop having two queries. We create one reporting view, owned jointly with finance, that encodes the agreed definitions, and both the dashboard and the export read from it.

SQL
CREATE VIEW reporting.daily_revenue_ny AS
SELECT (created_at AT TIME ZONE 'America/New_York')::date AS business_day,
SUM(total_usd) FILTER (WHERE status NOT IN ('cancelled')) AS gross,
SUM(refunded_usd) AS refunds
FROM orders
GROUP BY 1;
-- October: a plain date range on business_day
SELECT SUM(gross - COALESCE(refunds, 0)) FROM reporting.daily_revenue_ny
WHERE business_day >= DATE '2026-10-01' AND business_day < DATE '2026-11-01';

The column names here are illustrative; the important part is that the definitions live in one place with tests around them, and that the date logic is written once.

7Follow-up questions

How would you test reporting queries around time zone changes?

Build fixtures with orders at the edges: 23:59 and 00:00 New York time on the first and last day of a month, and orders on the daylight saving transition days, March 8 and November 1 in 2026, including the repeated hour in November. Assert which day and month each order lands in. Run the tests against a real PostgreSQL, for example with Testcontainers, and run them with different session TimeZone settings, such as UTC and Asia/Tokyo, to prove the query does not depend on the client.

Why not cast created_at to date and use BETWEEN on dates?

created_at::date uses the session time zone, so it repeats the UTC problem, and wrapping the column in a cast prevents a plain index on created_at from being used. Converting with AT TIME ZONE 'America/New_York' fixes the zone, and a half-open range on the raw column keeps the index.

Should the database server run in New York time?

No. Keep servers in UTC and store timestamptz, and apply the business zone explicitly in reporting. The business may expand to other zones, and server settings are easy to change by accident.

8Conclusion

In this answer, we found four reasons the dashboard disagreed with finance: BETWEEN with a date literal stops at midnight on October 31, the literals were read in the session's UTC zone instead of New York, a fixed offset breaks across daylight saving changes, and the two sides used different business definitions. We fixed the query with a half-open range on New York midnights named by zone, reconciled definitions at order level, and moved the logic into one shared, tested view.

What to say in the interviewAbout 90 seconds aloud

Open with

I see several causes, and I would check each one. First, BETWEEN with date literals: '2026-10-31' becomes midnight at the start of October 31, so almost the whole last day of the month, a Saturday, is missing.

Then, if they want more

I would always use a half-open range, created_at greater or equal to the start of October and less than the start of November. Second, the literals have no zone, so PostgreSQL interprets them in the session TimeZone, usually UTC on servers, while finance counts New York days.

In October New York is UTC minus four, so the dashboard includes four hours of September 30 and misses the evening of October 31. I would write the bounds as '2026-10-01 00:00 America/New_York' and '2026-11-01 00:00 America/New_York', or as a local timestamp AT TIME ZONE America/New_York for parameters.

Third, I would not hard-code minus four, because daylight saving ends on the first Sunday of November, November 1 this year, so the offset changes near month boundaries. Fourth, business definitions: cancelled orders, refunds and their timing, currency conversion, and whether the date is order or payment date.

I would reconcile at order level against the finance export to explain the rest. Finally, I would put the agreed logic in one shared reporting view that both the dashboard and the export use, with tests at month edges and daylight saving days.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Finance says October revenue in your dashboard is 1.8 percent lower than in their...": https://scrivio-interview-library.netlify.app/databases/sql-coding-problems/1efa1f8f5e/