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
  • 704,970
  • 168
  • 1,303
  • 1,425
Mostafa Elkady
  • 5,469
  • 10
  • 43
  • 66

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,603
  • 1
  • 26
  • 43
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,293
  • 14
  • 18