w3resource

PostgreSQL Aggregate Functions and Group By: Find the manager ID and the salary of the lowest-paid employee under that manager


9. Write a query to find the manager ID and the salary of the lowest-paid employee under that manager.

Sample Solution:

Code:

SELECT manager_id, MIN(salary) 
FROM employees 
WHERE manager_id IS NOT NULL 
GROUP BY manager_id 
ORDER BY MIN(salary) DESC;

Sample table: employees


Output:

pg_exercises=# SELECT manager_id, MIN(salary)
pg_exercises-# FROM employees
pg_exercises-# WHERE manager_id IS NOT NULL
pg_exercises-# GROUP BY manager_id
pg_exercises-# ORDER BY MIN(salary) DESC;
 manager_id |   min
------------+----------
          0 | 24000.00
        102 |  9000.00
        205 |  8300.00
        145 |  7000.00
        146 |  7000.00
        108 |  6900.00
        147 |  6200.00
        149 |  6200.00
        148 |  6100.00
        201 |  6000.00
        100 |  5800.00
        101 |  4400.00
        103 |  4200.00
        123 |  2500.00
        124 |  2500.00
        114 |  2500.00
        120 |  2200.00
        122 |  2200.00
        121 |  2100.00
(19 rows)

Practice Online


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

Previous: Write a query to get the difference between the highest and lowest salaries.
Next: Write a query to get the department ID and the total salary payable in each department.

What is the difficulty level of this exercise?