【正文】
name 張三 張三 李四 李四 王五 王五 王五 kecheng 語(yǔ)文 數(shù)學(xué) 語(yǔ)文 數(shù)學(xué) 語(yǔ)文 數(shù)學(xué) 英語(yǔ) fenshu 81 75 76 90 81 100 90 select Distinct And name from TEST A Where Not Exists(Select * from TEST Where Name = fenshu=80) select from group having name test by name min(fenshu)80 select name from test where name not in(select name from test where fens hu=80) 面試題九(福州) 有一表 table1 有兩個(gè)字段 FID,F(xiàn)no,寫一個(gè) SQL 語(yǔ)句列出該表中一個(gè) FID 對(duì)應(yīng)多個(gè)不同 的 Fno 的紀(jì) 錄。 類如: 101 1001 101 1001 102 1002 102 1003 37 Oracle 教程 103 1004 104 1005 104 1006 105 1007 105 1007 105 1007 105 1008 結(jié)果: 102 1002 102 1003 104 1005 104 1006 105 1007 105 1008 select distinct fid,fno from table1 where fid in (select fid from table1 group by fid having count(distinct fno)1) 面試題十(福州) 有員工表 empinfo ( Fempno varchar2(10) not null pk, Fempname varchar2(20) not null, Fage number not null, Fsalary number not null )。 假如數(shù)據(jù)量很大約 1000 萬(wàn)條; 寫一個(gè)你認(rèn)為最高效的 SQL, 用一個(gè) SQL 計(jì)算以下四種人: fsalary9999 and fage 35 fsalary9999 and fage 35 fsalary9999 and fage 35 fsalary9999 and fage 35 每種員工的數(shù)量; select sum(case when 9999 and fage 35 then 1 else 0 end) as fsalary9999 and fage 35, sum(case when 9999 and fage 35 then 1 else 0 end) as fsalary9999 and fage 35, 38 Oracle 教程 sum(case when 9999 and fage 35 then 1 else 0 end) as fsalary9999 and fage 35, sum(case when 9999 and fage 35 then 1 else 0 end) as fsalary9999 and fage 35 from empinfo tt。 面試題十一(上海) 表內(nèi)容: 20050509 勝 20050509 勝 20050509 負(fù) 20050509 負(fù) 20050510 勝 20050510 負(fù) 20050510 負(fù) 如果要生成下列結(jié)果, 該如何寫 sql 語(yǔ)句? 勝 負(fù) 20050509 2 2 20050510 1 2 1) select rq, sum(case when shengfu=39。勝39。 then 1 else 0 end)39。勝39。,sum(case when shengfu=39。負(fù)39。 then 1 else 0 end)39。負(fù)39。 from tmp group by rq 3)select , 勝, 負(fù) from (select col001,count(col001) a1 from temp1 where col002=39。勝39。 group by col001) a, (select col001,count(col001) b1 from temp1 where col002=39。負(fù)39。 group by col001) b where = 面試題十二(上海) 請(qǐng)用一個(gè) sql 語(yǔ)得出結(jié)果 從 table1,table2 中取出如 table3 所列格式數(shù)據(jù) table1 月份 mon 部門 dep 業(yè)績(jī) yj 一月份 01 10 一 月 份 02 10 一月份 03 5 二 月 份 02 8 39 Oracle 教程 二月份 04 9 三月份 03 8 table2 部門 dep 部門名稱 dname 01 02 03 04 05 國(guó)內(nèi)業(yè)務(wù)一部 國(guó)內(nèi)業(yè)務(wù)二部 國(guó)內(nèi)業(yè)務(wù)三部 國(guó)際業(yè)務(wù)部 其他部門 table3 (result) 部門 dep 部門名稱 01 02 03 04 05 一月份 二月份 三月份 國(guó)內(nèi)業(yè)務(wù)一部 10 0 0 國(guó)內(nèi)業(yè)務(wù)二部 10 8 0 國(guó)內(nèi)業(yè)務(wù)三部 0 5 8 國(guó)際業(yè)務(wù)部 0 0 9 其他部門 select , sum(case when =39。一月份39。 then else 0 end) as 39。一月份39。, sum(case when =39。二月份39。 then else 0 end) as 39。二月份39。, sum(case when =39。三月份39。 then else 0 end) as 39。三月份39。, from table2 a left join table1 b on = 面試題十二(上海) 華為一道面試題 一個(gè)表中的 Id 有多個(gè)記錄,把所有這個(gè) id 的記錄查出來(lái),并顯示共有多少條記錄數(shù)。 select id, Count(*) from tb group by id having count(*)1 第八章 高級(jí)查詢 、隨機(jī)返回 5 條記錄 Select * from (select ename,job from emp order by ()) where rownum=5 40 Oracle 教程 、處理空值排序 select * from emp order by m desc nulls last(first)。 、查詢跳過(guò)表中的偶數(shù)行 select ename from (select row_number() over (order by ename) rn,ename from emp) x where mod(rn,2)=1。 、查詢員工信息與其中工資最高最低員工 select ename,sal,max(sal) over(), min(sal) over() from emp。 、連續(xù)求和 select ename,sal,sum(sal) over(), sum(sal) over(order by ename) from emp。 sum(sal) over(order by ename) ename 來(lái)排序的。若有兩個(gè)這 樣的窗口函數(shù),以后面的排序?yàn)橹鳌? 41 Oracle 教程 、分部門連續(xù)求和 select deptno,sal ,sum(sal) over (partition by deptno order by ename) as s from emp。分部門連續(xù)求和 sum(sal) over (partition by deptno) 分部門求和 、得到當(dāng)前行上一行或者下一行的數(shù)據(jù) select ename,sal,lead(sal) over(order by sal) aaa ,lag(sal) over(order by sal) bbb from emp。 month person ine 月份 人員 收入 要求用一個(gè) SQL 語(yǔ)句,統(tǒng)計(jì)每個(gè)月及上月和下月的總收入 要求列表輸出為 月份 當(dāng)月收入 上月收入 下月收入 select month,sum(ine),lead(sum(ine)) over(order by sum(ine)) from table group by month。 、根據(jù)子串分組 Select to_char(hiredate,?yyyy?),avg(sal) from emp group by to_char(hiredate,?yyyy?)。 42 Oracle 教程 、確定一年內(nèi)的天數(shù) select dual。 add_months(trunc(sysdate,39。y39。),12)trunc(sysdate,39。y39。) from 、查詢 EMP 員工表下每個(gè)部門工資前二名的員工信息 select deptno, ename, sal from emp e1 where (select count(1) from emp e2 where = and != and ) 2 order by deptno, sal desc。 select * from (select deptno,ename,sal,row_number() over (partition by deptno order by sal desc) rn from emp) where rn3。 、student,course,choose_course,找出選了所有課程的學(xué)生信息 select distinct sname from student s where not exists(select * from course c where not exists(select * from choose_course cc where = and = ))。 select student_id,count(distinct cid) from choose_course group by student_id having count(distinct cid) = (select count(distinct cid)from course)。 第九章 數(shù)據(jù)字典 、查詢某用戶下所有表 select table_name from all_tables where owner=39。SCOTT39。 43 Oracle 教程 、查詢 EMP 表中所有字段(列) select * from all_tab_columns where table_name=39。EMP39。 、列出表的索引列 select * from where table_name=39。EMP39。 select * from where upper(table_name)=39。CAREUSERHAM39。 、列出表中約束 select * from all_constraints where table_name=39。EMP39。 、在 oracle 中描述數(shù)據(jù)字典視圖 select table_name ,ments from dictionary where table_name like 39。%TABLE%39。 第十章 Oracle 數(shù)據(jù)類型 、Oracle 主要數(shù)據(jù)類型 number(4,2) Char,nchar,varchar2,nvarchar2,number(),date,blob(binary 二進(jìn)制流的大對(duì)象),clob(文件大對(duì) 象) 注意: 由于 char 是以固定長(zhǎng)度的, 所以它的速度會(huì)比 varchar2 快得多!但程序處理起來(lái)要 麻煩一點(diǎn),要用 trim 之類的函數(shù)把兩邊的空格去掉 Varchar2 一般適用于英文和數(shù)字,Nvarchar2 適用中文和其他字符,其中 N 表示 Unicode 常量,可以解決多語(yǔ)言字符集之間的轉(zhuǎn)換問(wèn)題 Number(4,2) 指的是整數(shù)占 2 位,小數(shù)占 2 位( 可以。 不行,因 為是 四舍五入) Number 默認(rèn)為 38 位 44 Oracle 教程 、char,nchar,varchar2,nvarchar2 區(qū)別 、Java 對(duì) blob 字段的操作 第十一章 Oracle 體系結(jié)構(gòu)(DBA) 、數(shù)據(jù)庫(kù)(Database) 一系列物理文件(數(shù)據(jù)文件,控制文件,聯(lián)機(jī)日志等)的集合或與之對(duì)應(yīng)的邏輯結(jié)構(gòu)(表 空間,段等)被稱為數(shù)據(jù)庫(kù) 物理存儲(chǔ)結(jié)構(gòu) 數(shù)據(jù)文件、重做日志文件、控制文件 Desc v$logfile。 Select member from v$logfile。 V$controlfile V$datafile。 邏輯存儲(chǔ)結(jié)構(gòu) 表空間、段、區(qū)、塊 、數(shù)據(jù)庫(kù)實(shí)例(Database Instance) ORACLE 內(nèi)存結(jié)構(gòu)和后臺(tái)進(jìn)程被成為數(shù)據(jù)庫(kù)的實(shí)例 實(shí)例 內(nèi)存結(jié)構(gòu) 后臺(tái)進(jìn)程 SGA PGA 45 Oracle 教程 、Oracle 實(shí)時(shí)應(yīng)用集群(RAC, Real Appli