{
  "id": 13942900,
  "title": "WHERE vs HAVING in SQL: A Simple Way to Understand the Difference",
  "url": "https://urgent.news/2026/10/12/where-vs-having-in-sql-a-simple-way-to-understand-the-difference",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-12T11:30:10.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/solo_495/where-vs-having-in-sql-a-simple-way-to-understand-the-difference-2ihp"
  },
  "original_language": "en",
  "account": "WHERE and HAVING are both used to filter data in SQL, but they do so at different stages of the query processing. WHERE filters individual rows before any grouping occurs, while HAVING filters groups after the grouping and calculations are performed.\n\nTo illustrate, imagine a scenario where attendees are divided into tables at an event. The bouncer (WHERE) checks each individual at the door to allow entry based on their profile, such as excluding under-18s. Once everyone is seated, the host (HAVING) evaluates each table, merging those with fewer than 4 people. The bouncer cannot assess the table quality since there are no tables at the door, and thus aggregate functions like SUM() and COUNT() cannot be used there.\n\nUsing a small orders table, we can demonstrate the difference. In Step 1, WHERE filters rows with an amount greater than 100, returning 4 rows. In Step 2, we calculate the total amount spent by each customer using GROUP BY. Step 3 shows HAVING filtering groups where the total spent exceeds 200, removing Chloe due to her total of 180. Step 4 demonstrates using both WHERE and HAVING together to get the total money spent per customer, filtering out David due to a cancelled order and Brian's total dropping from 380 to 300 after removing a cancelled order. The crucial point is that SQL processes queries in a specific order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. WHERE evaluates raw rows, while HAVING assesses group totals, explaining the difference in their functionality.",
  "summary": "If you’ve ever mixed up WHERE and HAVING, you’re not alone. They both filter data, so it feels like you should be able to use either one. But they filter at different moments, and once you get that, the confusion goes away. The one-line answer WHERE filters individual rows before any grouping happens. HAVING filters groups after the grouping and math (SUM, COUNT, AVG) is done. A Quick Analogy…",
  "key_points": [
    "WHERE filters individual rows before grouping in SQL queries",
    "HAVING filters groups after calculations and grouping are performed",
    "WHERE cannot use aggregate functions like SUM() and COUNT()"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}