Background
Lets say you have 2 tables -
- EMPLOYEE
- EMPLOYEE_SALARY
Tables creation and data insertion
Table Queries :
create table schema8.EMPLOYEE(ID int, name varchar2(255)); alter table schema8.EMPLOYEE ADD CONSTRAINT PRIMARY_KEY PRIMARY KEY (ID); create table schema8.EMPLOYEE_SALARY(EMPLOYEE_ID int, department varchar2(255), salary int); alter table schema8.EMPLOYEE_SALARY ADD CONSTRAINT FK_EMP_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES schema8.EMPLOYEE(ID);
Data Queries for EMPLOYEE table:
insert into schema8.EMPLOYEE values(1,'Aniket'); insert into schema8.EMPLOYEE values(2,'John'); insert into schema8.EMPLOYEE values(3,'Sam'); insert into schema8.EMPLOYEE values(4,'Ron'); insert into schema8.EMPLOYEE values(5,'Sky'); insert into schema8.EMPLOYEE values(6,'Paul'); insert into schema8.EMPLOYEE values(7,'Dan'); insert into schema8.EMPLOYEE values(8,'Jess'); insert into schema8.EMPLOYEE values(9,'Troy'); insert into schema8.EMPLOYEE values(10,'Mike');
Data Queries for EMPLOYEE_SALARY table:
insert into schema8.EMPLOYEE_SALARY values(1,'IT',10000); insert into schema8.EMPLOYEE_SALARY values(2,'Admin',500); insert into schema8.EMPLOYEE_SALARY values(3,'Sales',1200); insert into schema8.EMPLOYEE_SALARY values(4,'Sales',1500); insert into schema8.EMPLOYEE_SALARY values(5,'IT',9000); insert into schema8.EMPLOYEE_SALARY values(6,'Admin',4000); insert into schema8.EMPLOYEE_SALARY values(7,'Admin',5000); insert into schema8.EMPLOYEE_SALARY values(8,'IT',9500); insert into schema8.EMPLOYEE_SALARY values(9,'Sales',1000); insert into schema8.EMPLOYEE_SALARY values(10,'Admin',6000);
Final data :
select * from schema8.EMPLOYEE; select * from schema8.EMPLOYEE_SALARY;
Solution
We are going to use RANK to partition by department and order by salary -
select * from ( select id , name, department, salary, RANK() over (partition by department order by salary desc) as rank from( select e.id, e.name, es.department,es.salary from schema8.EMPLOYEE e left OUTER join schema8.EMPLOYEE_SALARY es on (e.id=es.employee_id)) ) where rank <= 2;
First we have done a left outer join so that we capture all employee records with their respective salaries and departments. In outer query we have ranked it based on their salaries in respective department. Finally we select all records that have rank <=2 i.e top 2 records.
Related Links
- Remove duplicate rows from table in Oracle(OSFG)
- Basic PL/SQL syntax and query executions on Oracle 11g R2 database(OSFG)
- How does database indexing work? (OSFG)