如何从这个架构获得公司明智的最高薪水员工姓名

时间:2017-12-05 19:22:22

标签: sql oracle

数据库架构是这样的:

雇员(EMP_ID#,PERSON_NAME,DOB,街道,城市)

公司(company_id#,company_name,city)

作品(EMP_ID,COM​​PANY_ID,薪水)

管理(EMP_ID,MANAGER_ID)

insert into employee values('e-1','dipankar pal','15-jul-1997','h.m raod','kolkata');
insert into employee values('e-2','subhadip roy','15-jan-1997','garia','kolkata');

管理表可能有点令人困惑,这里有一些我已经插入的数据来清楚我的观点

insert into manages values('e-3','e-1');
insert into manages values('e-4','e-1');
insert into manages values('e-5','e-1');
insert into manages values('e-6','e-2');
insert into manages values('e-7','e-2');

2 个答案:

答案 0 :(得分:1)

尝试

SELECT Z.COMPANY_NAME, E.PERSON_NAME, Z.HIGHEST_SALARY
FROM
    (SELECT C.COMPANY_ID, C.COMPANY_NAME, MAX(W.SALARY) AS HIGHEST_SALARY
    FROM
    WORKS W INNER JOIN COMPANY C
    ON W.COMPANY_ID = C.COMPANY_ID
    GROUP BY C.COMPANY_ID, C.COMPANY_NAME ) Z
INNER JOIN WORKS W
ON Z.COMPANY_ID = W.COMPANY_ID
AND Z.HIGHEST_SALARY = W.SALARY
INNER JOIN EMPLOYEE E
ON W.EMP_ID = E.EMP_ID
ORDER BY Z.HIGHEST_SALARY DESC;

答案 1 :(得分:0)

select company.company_name,employee.person_name as employee_name,y.salary as salary from employee,company,
(select works.emp_id,works.company_id,works.salary from works,
(select company_id,max(salary) as high from works group by(company_id)
)x where high=works.salary) y
where employee.emp_id=y.emp_id and company.company_id=y.company_id order by company_name
;