SQL query on emp table to display the employee name, his salary, sum of his department salary

Neeraj Sharma

Neeraj Sharma

@neeraj-iAaNcG Oct 25, 2024
Write a SQL query on emp table to display the employee name, his salary, sum of his department salary and second highest salary of his department in different columns. This must be done for all the records in emp table. Any help??

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • [Prototype]

    [Prototype]

    @prototype-G9Gn5k Jul 18, 2012

    Considering you're using MySQL,

    SELECT * FROM `emp`
    Are you trying to query from php and wants to display the results on the webpage?
  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Jul 18, 2012

    [Prototype]
    Considering you're using MySQL,

    SELECT * FROM `emp`
    Are you trying to query from php and wants to display the results on the webpage?
    I am doing it on sqldeveloper. And I am not a noob that I dont know the query that you have given me. Please read my question again and then answer
  • Prashanth_p@cchi

    Prashanth_p@cchi

    @prashanth-p-at-cchi-rg0z63 Jul 18, 2012

    select empname sal sum(sal) from emp group by deptno;
    select sal from emp e where sal <
    (select max(sal) from emp ea where ea.deptno = e.deptno) order by sal;

    I am not sure of the above. It has been more that two years, me writing a SQL Query. Hope it might help you in arriving at the solution.
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Jul 18, 2012

    select e.emp_name, e.Emp_sal,
    (select sum(e1.emp_sal) from emp_tab e1
    where e.dept_id=e1.Dept_id  group by e.Dept_id) as Salary_Sum,
    (Select emp_sal from emp_tab e2 where rownum=2 and
    e2.dept_id=e.dept_it order by e2.emp_salary  desc ) as Sec_High_salary
    from emp_tab e where e.emp_ID='XXXXX'
  • Prashanth_p@cchi

    Prashanth_p@cchi

    @prashanth-p-at-cchi-rg0z63 Jul 18, 2012

    ianoop
    select e.emp_name, e.Emp_sal,
    (select sum(e1.emp_sal) from emp_tab e1
    where e.dept_id=e1.Dept_id  group by e.Dept_id) as Salary_Sum,
    (Select emp_sal from emp_tab e2 where rownum=2 and
    e2.dept_id=e.dept_it order by e2.emp_salary  desc ) as Sec_High_salary
    from emp_tab e where e.emp_ID='XXXXX'
    I think this should work... Nice!