can anyone tell me, why is this query giving an error
Write a query to find the maximum total earnings (earnings) for all employees as well as the total number of employees (num_employees) who have maximum total earnings. total earnings = months worked *salary We define an employee's total earnings to be their monthly (salary×months) worked
SELECT
(salary*months) as earnings
,count(*) as num_employees
FROM employee
group by salary*months
having max(salary * months)=salary*months
there is a logic issue in your code. you see, the
HAVINGclause is used to perform filtering on the results of the grouped data, so usingMAXin the having clause might not get you the results you expect.if you want to find the maximum total earnings and the number of employees with that maximum total earnings, you can use a subquery to first find the maximum total earnings and then join it back to the original table like this: