0

Why should I write this:

Having count(id) > 1

instead of this:

Where count(id) > 1

Is this just for English language?

I think if the programmer that invented where clause could made it accept aggregators

Or is there a reason for that? In loading the data or anything like that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mostafa Elkady
  • 5,645
  • 10
  • 45
  • 69

2 Answers2

4

WHERE operates on rows, HAVING operates on collections of rows ("groups"). Aggregate functions cannot be used on single rows, that just would not make any sense.

The MySQL documentation states:

group (aggregate) functions that operate on sets of values [...] If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.

Tobias
  • 7,723
  • 1
  • 27
  • 44
2

The reason is that SQL evaluates items in a certain order e.g.

  • Identify the tables in question
  • Apply the where clause to narrow the rows
  • Take values from the columns
  • Apply aggregates (group by clauses)
  • Apply aggregate limits (having clauses)
  • Apply sorting (order by clauses)

If you tried to evaluate an aggregate in a "where" clause it literally wouldn't make sense as it needs to apply the "where" clause before aggregation.

Jeff Watkins
  • 6,343
  • 16
  • 19