Aggregate function in SQL WHERE-Clause

前端 未结 6 569
失恋的感觉
失恋的感觉 2020-11-27 15:22

In a test at university there was a question; is it possible to use an aggregate function in the SQL WHERE clause.

I always thought this isn\'t possibl

6条回答
  •  [愿得一人]
    2020-11-27 15:40

    HAVING is like WHERE with aggregate functions, or you could use a subquery.

    select EmployeeId, sum(amount)
    from Sales
    group by Employee
    having sum(amount) > 20000
    

    Or

    select EmployeeId, sum(amount)
    from Sales
    group by Employee
    where EmployeeId in (
        select max(EmployeeId) from Employees)
    

提交回复
热议问题