MySQL query to find the highest salary from each department
In article we will explain how to write a MySQL query to find the highest salary from each department from the employees table.
Read Also : Find The Nth Highest Employee Salary From An Employee Table In MySql
Find the highest salary from each department
Let’s create a simple example of employees table. We will populate this table with id, name, salary and department of employees.
SELECT * FROM employees;
id | name | salary | department |
---|---|---|---|
1 | Tom | 4000 | Technology |
2 | Sam | 6000 | Sales |
3 | Bob | 3000 | Technology |
4 | Alen | 8000 | Technology |
5 | Jack | 12000 | Marketing |
6 | Charlie | 8000 | Human Resources |
7 | Harry | 6000 | Marketing |
8 | Jacob | 4000 | Human Resources |
The following MySQL statement find the maximum salary from each department, you will be required to use the GROUP BY clause with the SELECT query.
SELECT department, MAX(salary) FROM employees GROUP BY department ORDER BY MAX(salary) DESC;
department | MAX(salary) |
---|---|
Marketing | 12000 |
Human Resources | 8000 |
Technology | 8000 |
Sales | 6000 |
I think what you wrote was actually very logical.