Oracle SQL sample database

前端 未结 6 2118
离开以前
离开以前 2021-02-11 08:59

I\'m trying to learn Oracle SQL by database Supplied by it. I found somewhere tasks to be done. Database structure is supplied by Oracle:

CREATE TABLE EMP
(EMPNO         


        
6条回答
  •  天涯浪人
    2021-02-11 09:18

    You are grouping by employee name, as well as department and salary grade. That means you will return a row for every combination of employee name, department and salary grade in your dataset.

    To ensure that you only return one row per department and salary grade, you will need to remove the employee name from the group by clause. This will enable you to find the maximum salary per grade and department, but not which employees have that salary - to do that, you need to join the results back to the employee table again, like so:

    select e.ename, s.grade, d.dname, e.salary
    from (select max(emp.salary) max_salary, 
                 salgrade.grade,
                 emp.deptno 
          from emp, salgrade
          WHERE emp.sal BETWEEN salgrade.losal AND salgrade.hisal
          group by salgrade.grade, dept.dname) s
         join emp e on e.salary = s.max_salary
         join dept d on e.deptno = d.deptno
    

    Note that if multiple employees in the same department are earning the same maximum salary within their grade, then both employees will be returned - this should happen with FORD and SCOTT in RESEARCH.

提交回复
热议问题