解析一道笔试题目:查找各个部门工资最高的前3名员工信息
一张员工表(tab_employee)和一张部门表(tab_department)的样例数据如下所示:
SQL>
SQL> select * from tab_employee;
ID NAME SALARY DEPARTMENTID
---------- ---------- ---------- ------------
1 Joe 70000 1
2 Henry 80000 2
3 Sam 60000 2
4 Max 90000 1
5 Randy 85000 1
已用时间: 00: 00: 00.01
SQL> select * from tab_department;
ID NAME
---------- ----------
1 IT
2 Sales
已用时间: 00: 00: 00.00
SQL>
要求编写一条查询语句,查找各个部门中工资最高的前3名员工。
解析:
创建两种数据表和准备测试数据如下:
CREATE TABLE tab_Employee
(
Id NUMBER(4),
Name VARCHAR2(50),
Salary NUMBER(7,2),
DepartmentId NUMBER(4)
);
CREATE TABLE tab_Department
(Id NUMBER(4), Name VARCHAR2(50)
);
insert into tab_Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '70000', '1');
insert into tab_Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2');
insert into tab_Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2');
insert into tab_Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1');
insert into tab_Employee (Id, Name, Salary, DepartmentId) values ('5', 'Randy', '85000&#