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 | 24030.00
        102 |  9030.00
        205 |  8330.00
        145 |  7030.00
        146 |  7030.00
        108 |  6930.00
        147 |  6230.00
        149 |  6230.00
        148 |  6130.00
        201 |  6030.00
        100 |  5830.00
        101 |  4430.00
        103 |  4230.00
        123 |  2530.00
        124 |  2530.00
        114 |  2530.00
        120 |  2230.00
        122 |  2230.00
        121 |  2130.00
(19 rows)

Practice Online


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

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming