I've seen [in](https://stackoverflow.com/questions/287474/whats-the-difference-between-having-and-where) [other](https://stackoverflow.com/questions/9253244/sql-having-vs-where?noredirect=1&lq=1) [questions](https://stackoverflow.com/questions/12093405/difference-between-where-and-having-with-respect-to-aliases) that the difference between `HAVING` and `WHERE` in SQL is that `HAVING` is used post-aggregation whereas `WHERE` is used pre-aggregation. However, I am still unsure about when to use pre-aggregation filtering or post-aggregation filtering. 

As a concrete example, why don't these two queries yield the same result (the second sums `quantity` prematurely in a way that squashes the `GROUP BY` call)? 

Using `WHERE` to obtain number of condo sales of each real estate agent.

```
SELECT agentId, SUM(quantity) total_sales 
  FROM sales s, houses h
  WHERE s.houseId = h.houseId AND h.type = "condo"
  GROUP BY agentId
  ORDER BY total_sales;

```

Attempted use of `HAVING` to obtain the same quantity as above.

```
SELECT agentId, SUM(quantity) total_sales 
  FROM sales s, houses h
  GROUP BY agentId
  HAVING s.houseId = h.houseId AND h.type = "condo"
  ORDER BY total_sales;

```

Note: these were written/tested/executed in sqlite3.