ERROR 1111 (HY000): Invalid use of group function

后端 未结 1 1592
误落风尘
误落风尘 2020-12-10 02:53

given the following table staff (ec,name,code,dob,salary)

Q. List the staff members earning more than the average salary

My soln. select* from staff wh

1条回答
  •  情深已故
    2020-12-10 03:08

    An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

    Example using WHERE clause :

    select *
    from staff 
    where salary > (select avg(salary) from staff)
    

    Example using HAVING clause :

    select deptid,COUNT(*) as TotalCount
    from staff
    group by deptid
    having count(*) > 2
    

    Having clause specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause. When GROUP BY is not used, HAVING behaves like a WHERE clause.

    0 讨论(0)
提交回复
热议问题