Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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)

Relational Algebra Expression:

Relational Algebra Expression: Find the manager ID and the salary of the lowest-paid employee under that manager.

Relational Algebra Tree:

Relational Algebra Tree: Find the manager ID and the salary of the lowest-paid employee under that manager.

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?