Skip to Main Content
  • Questions
  • Find min and mean salary per department and employees earning the minimum

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Abdul Aziz.

Asked: August 31, 2016 - 12:12 pm UTC

Last updated: August 31, 2016 - 3:53 pm UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

Good Day Tom!

I have a question relating to subqueries and Joins etc

From TestDB > HR User

I need you to design a query for me

show the first_name, last_name, department_id, salary of all employees who take minimum salary in their respective departments and also average salary of all the salaries in their department in a single query.


thanks.

and Chris said...

You mean something like this?

select * from (
select department_id, 
       first_name, last_name, salary,
       round(avg(salary) over (partition by department_id)) avg_sal,
       min(salary) over (partition by department_id) min_sal
from   hr.employees
)
where  salary = min_sal
order  by 1, 2;

DEPARTMENT_ID  FIRST_NAME  LAST_NAME   SALARY  AVG_SAL  MIN_SAL  
10             Jennifer    Whalen      4,400   4,400    4,400    
20             Pat         Fay         6,000   9,500    6,000    
30             Karen       Colmenares  2,500   4,150    2,500    
40             Susan       Mavris      6,500   6,500    6,500    
50             TJ          Olson       2,100   3,476    2,100    
60             Diana       Lorentz     4,200   5,760    4,200    
70             Hermann     Baer        10,000  10,000   10,000   
80             Sundita     Kumar       6,100   8,956    6,100    
90             Lex         De Haan     17,000  19,333   17,000   
90             Neena       Kochhar     17,000  19,333   17,000   
100            Luis        Popp        6,900   8,601    6,900    
110            William     Gietz       8,300   10,154   8,300    
               Kimberely   Grant       7,000   7,000    7,000


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.