w3resource

SQL exercises on employee Database: Display the Grade, Number of employees, and maximum salary of each grade

SQL employee Database: Exercise-99 with Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

99. Write a query in SQL to display the Grade, Number of employees, and maximum salary of each grade.

Sample table: employees


Sample table: salary_grade


Sample Solution:

SELECT s.grade,
       count(*),
       max(salary)
FROM employees e,
     salary_grade s
WHERE e.salary BETWEEN s.min_sal AND s.max_sal
GROUP BY s.grade;

Sample Output:

 grade | count |   max
-------+-------+---------
     4 |     5 | 3100.00
     1 |     3 | 1200.00
     5 |     1 | 6000.00
     3 |     2 | 1700.00
     2 |     3 | 1400.00
(5 rows)

Practice Online


Sample Database: employee

employee database structure

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query in SQL to list the department where at least two employees are working.
Next: Write a query in SQL to display the department name, grade, no. of employees where at least two employees are working as a SALESMAN.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts