Question: We define an employee's total earnings to be their monthly worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as space-separated integers.
This is link to the question for better understanding <https://www.hackerrank.com/challenges/earnings-of-employees/problem>

I am a beginner in SQL and couldn't understand the solution which was given

```
1.SELECT (months*salary) as earnings,
 2.COUNT(*) FROM Employee
3. GROUP BY earnings
4. ORDER BY earnings DESC
 5.LIMIT 1;

```

I understood the first step where we are giving months\*salary an Alias which is earnings
in the 2nd step we are counting the no. of employees from employee table. I didn't understand why we are using group by here, 4th and 5th step is also clear, we used order by clause in order to rank earnings from highest to lowest, and limit 1 will fetch me the highest value. But why GROUP BY?
 Ignore 1,2,3,4,5. These are just numbers I used for better clarity

Answer: 
**Step 3:** `GROUP BY earnings` was used to GROUP TOGETHER same value earnings. If you have, for example, earnings of $3,000, and there were 3 of them, they will be grouped together. `GROUP BY` is also required in combination with the aggregate function `COUNT(*)`. Otherwise, `COUNT(*)` will not work and return an error.

**Step 4:** `ORDER BY earnings DESC` was used to order the GROUPED EARNINGS in DESCENDING order. Meaning, from HIGHEST EARNINGS down to the LOWEST EARNINGS.

**Step 5:** `LIMIT 1` limits the returned row count to only 1. 

Hope this helps! :)
Try this query:

```
SELECT months*salary,COUNT(*) FROM Employee GROUP BY months*salary having count(*) = 7 ORDER BY months*salary DESC;

```