Oracle開發之SQL語句案例 分析函式的使用

2021-05-09 05:17:32 字數 1971 閱讀 1827

建立雇員表:

create table emp(deptno number(10),ename varchar2(100),sal number(10,2));

插入資料

begin

insert into emp values('10','king',5000);

insert into emp values('10','clark',2450);

insert into emp values('10','miller',1300);

insert into emp values('20','scott',3000);

insert into emp values('20','ford',3000);

insert into emp values('20','jones',2975);

insert into emp values('20','adams',1100);

insert into emp values('20','smith',800);

insert into emp values('30','blake',2850);

insert into emp values('30','allen',1600);

insert into emp values('30','turner',1500);

insert into emp values('30','ward',1250);

insert into emp values('30','martin',1250);

insert into emp values('30','james',950);

commit;

end;

1、使用row_number()、rank()、dense_rank()查出各部門薪水最高的三個員工姓名、薪水,多於三個的只取三個。

select *

from (select e.deptno,

e.ename,

e.sal,

row_number()over(partition by e.deptno order by sal desc) rankno

from emp e)

where rankno <= 3;

select *

from (select e.deptno,

e.ename,

e.sal,

rank()

over(partition by e.deptno order by sal desc) rankno

from emp e)

where rankno <= 3;

select *

from (select e.deptno,

e.ename,

e.sal,

row_number()

over(partition by e.deptno order by sal desc) rankno

from emp e)

where rankno <= 3;

select *

from (select e.deptno,

e.ename,

e.sal,

dense_rank()

over(partition by e.deptno order by sal desc) rankno

from emp e)

where rankno <= 3;

注:row_number()排名的名次不會出現重複;rank()則會出現排名名次相同且如果兩人的名次都為2,則下個人的名次則為4而不是為3,而dense_rank()則與rank()恰好相反。

oracle之sql語句優化

sql語句的優化 1.在where子句中使用 is null 或 is not null 時,oracle優化器就不能使用索引了.2.對於有連線的列,即使最有乙個是靜態的值,優化器也不會使用索引 比如 select from employss where first name last name b...

SQL分頁語句三方案

方法一 select top 頁大小 from table1 where id not in select top 頁大小 頁數 1 id from table1 order by id order by id 方法二 select top 頁大小 from table1 where id sele...

oracle入門sql語句

1 顯示當前連線使用者 sql show user 2 檢視系統擁有哪些使用者 sql select from all users 3 新建使用者並授權 sql create user a identified by a 預設建在system表空間下 sql grant connect,resour...