SQL WHERE vs HAVING: Filter Rows before You Filter Groups
WHERE and HAVING both filter a SQL result, but they answer different questions. WHERE chooses input rows. HAVING filters groups after aggregation.…
Two reports can disagree about “yesterday” even when they use the same events. One may use UTC dates while the other uses a local calendar day. Before investigating a Malta reporting difference, confirm what the day actually means.
Ask which time zone defines the business day and whether the event time means creation, completion or receipt. These timestamps are not interchangeable. An event received after midnight may describe work completed earlier. Agree which meaning the report should follow before changing the query.
PostgreSQL distinguishes timestamp without time zone from timestamp with time zone. Its documentation explains that values with a time zone are represented internally in UTC and displayed using the session time zone; the originally supplied zone is not retained. A timestamp without time zone does not provide that conversion behaviour. Do not assume that a column name ending in “utc” proves how values were stored.
A start-inclusive, next-start-exclusive interval can make adjacent reporting periods easier to reason about. Calculate the boundaries for the agreed zone rather than assuming every local day has exactly the same duration.
Record the event field, time zone and boundary convention. Use a small approved sample to compare results. In an interview, explaining these assumptions shows why a correct-looking date filter can still answer the wrong business question.
Browse current technology vacancies with Huwell Consultancy and ask us about the responsibilities that matter to you.
Explore opportunitiesWHERE and HAVING both filter a SQL result, but they answer different questions. WHERE chooses input rows. HAVING filters groups after aggregation.…
A report total can become wrong even when the SQL runs without an error. One common cause is joining data at different…
A finished report needs more than readable charts. Before sharing it with a team in Malta, check whether its rows and totals…