SQL Select Count(person_id) > 3 From SQL Select Count(person_id) > 3 From database database

SQL Select Count(person_id) > 3 From


Use:

  SELECT t.person_id    FROM TABLE tGROUP BY t.personid  HAVING COUNT(t.personid) > 3

You can not use aggregate functions, or column aliases to derived columns using aggregate functions, in the WHERE clause. These can only be used in the HAVING clause, which requires defining a GROUP BY clause (if it doesn't already exist).

I don't recommend using the column alias in GROUP BY or HAVING clauses - there's a risk that the query will not be portable to other databases. SQL Server and MySQL are the only databases that I'm aware of that supports column aliases in the GROUP BY or HAVING clauses.


SELECT    person_idFROM tableGROUP BY person_idHAVING COUNT(*) > 3