posts - 84,  comments - 54,  trackbacks - 0

          oracle數(shù)據(jù)庫(kù)中
          在scott用戶(hù)下的emp和dept表中查詢(xún)

          1,查詢(xún)平均工資最高的那個(gè)部門(mén)的名稱(chēng),最高工資,最低工資,平均工資。

          ?

          ?1 1 ,查詢(xún)平均工資最高的那個(gè)部門(mén)的名稱(chēng),最高工資,最低工資,平均工資。
          ?2 ???? 1 > 使用rownum的方式
          ?3 ???? select ?????d.dname?部門(mén)名稱(chēng),
          ?4 ??????????????? min (e.sal)?最低工資,
          ?5 ??????????????? max (e.sal)?最高工資,
          ?6 ??????????????? avg (e.sal)?平均工資
          ?7 ???? from ?emp?e,dept?d? where ?e.deptno = d.deptno? and ?e.deptno =
          ?8 ????????????????????????( select ?deptno? from ?
          ?9 ????????????????????????( select ? avg (sal)? as ?avgsal,deptno? from ?emp?
          10 ?????????????????????????? group ? by ?deptno? order ? by ?avgsal? desc )? where ?rownum? = ? 1 )?
          11 ???? group ? by ?d.dname;
          12
          13 ???? 2 > 不使用rownum的方式
          14
          15 ???? select ??d.dname?部門(mén)名稱(chēng),
          16 ???????? min (e.sal)?最低工資,
          17 ???????? max (e.sal)?最高工資,
          18 ???????? avg (e.sal)?平均工資
          19 ???????? from ?emp?e,dept?d? where ?e.deptno? = ?d.deptno?
          20 ???????? and ?d.deptno? = ?????( select ?aa.deptno? from ?( select ? avg (sal)? as ?avgsal,deptno? from ?emp? group ? by ?deptno)?aa?
          21 ???????????????????? where ?aa.avgsal? = ?( select ? MAX (avgsal)? from ?
          22 ????????????????????????????( select ? avg (sal)? as ?avgsal,deptno? from ?emp? group ? by ?deptno)))
          23 ???????? group ? by ?d.dname;

          ?

          2,求工資由高到低,排名第三的員工姓名

          1 select ? * ? from ( select ? * ? from ?emp? order ? by ?sal? desc )? where ?rownum? < ? 4
          2 minus
          3 select ? * ? from ( select ? * ? from ?emp? order ? by ?sal? desc )? where ?rownum? < ? 3

          3,求當(dāng)前系統(tǒng)的日期,不要年和月。

          1 select ?to_char(sysdate, ' ----mm--dd ' )? from ?dual;

          ?

          4,查詢(xún)處員工人數(shù)最多的那個(gè)部門(mén)的
          ?????? 部門(mén)編號(hào),部門(mén)名稱(chēng),部門(mén)人數(shù)

          ?1 1>不使用rownum的方式
          ?2 ????select?????e.deptno??部門(mén)編號(hào),
          ?3 ????????d.dname??????部門(mén)名稱(chēng),
          ?4 ????????count(*)??部門(mén)人數(shù)
          ?5 ????from?emp?e,dept?d?where?e.deptno?=?d.deptno?and?e.deptno?=
          ?6 ????(select?aa.deptno?from
          ?7 ????(select?count(empno)?as?empno_count,deptno?from?emp?group?by?deptno)?aa?where?aa.empno_count?=
          ?8 ????(select?max(empno_count)?as?max_count?from(
          ?9 ????????????????????????????????select?count(empno)?as?empno_count,deptno?from?emp?group?by?deptno)))
          10 ????group?by?d.dname,e.deptno;
          11
          12 ????2>使用rownum的方式
          13 ????select?e.deptno??部門(mén)編號(hào),
          14 ?????????d.dname??部門(mén)名稱(chēng),
          15 ????????count(*)?部門(mén)人數(shù)
          16 ????from?emp?e,dept?d?where?e.deptno?=?d.deptno?and?e.deptno?=
          17 ????(select?deptno?from
          18 ????(select?count(empno)?as?empno_count,deptno?from?emp?group?by?deptno?order?by?empno_count?desc)?where?rownum?=?1)
          19 ????group?by?e.deptno,d.dname;


          5,查詢(xún)工資成本最高的那個(gè)部門(mén)的
          ?????? 部門(mén)編號(hào),部門(mén)名稱(chēng),部門(mén)月工資成本



          6,按 某某人 的上級(jí)是 某某人的格式查詢(xún)處所有員工的信息,
          ????? 如果某某人沒(méi)有上級(jí),則上級(jí)的名字為“未知”

          6

          ?


          7,查詢(xún)處在公司工作時(shí)間最長(zhǎng)的原工信息
          ????? 部門(mén)名稱(chēng),員工姓名,在公司工作的時(shí)間

          7題


          8,顯示裁員信息,例如,那個(gè)部門(mén)只有一個(gè)就不裁,若部門(mén)員工人數(shù)大于1人,則裁員工工資最高的那個(gè)人。

          8題



          EMP表

          EMPNO ENAME????? JOB????????????MGR??? HIREDATE????????? SAL????? COMM????DEPTNO
          ----- ---------- --------- ----- ----------- --------- --------- ------
          ?7369 SMITH????? CLERK???????????????7902????1980-12-17????????800.00?????????????????????20
          ?7499 ALLEN????? SALESMAN??????7698????1981-2-20????????1600.00??? 300.00???? 30
          ?7521 WARD?????? SALESMAN??????7698????1981-2-22????????1250.00??? 500.00???? 30
          ?7566 JONES????? MANAGER?????????7839????1981-4-2?????????2975.00?????????????????????20
          ?7654 MARTIN???? SALESMAN??????7698?????1981-9-28???? 1250.00?? 1400.00???? 30
          ?7698 BLAKE????? MANAGER?????????7839????1981-5-1?????????2850.00?????????????????????30
          ?7782 CLARK????? MANAGER?????????7839?????1981-6-9???????? 2450.00?????????????????????10
          ?7788 SCOTT????? ANALYST??????????7566????1987-4-19????????3000.00?????????????????????20
          ?7839 KING?????? PRESIDENT??????????????????????1981-11-17??? 5000.00?????????????????????10
          ?7844 TURNER???? SALESMAN??????7698????1981-9-8?????????1500.00????? 0.00????????30
          ?7876 ADAMS????? CLERK???????????????7788????1987-5-23????????1100.00?????????????????????20
          ?7900 JAMES????? CLERK???????????????7698????1981-12-3?????????950.00????????????????????????30
          ?7902 FORD?????? ANALYST????????????7566????1981-12-3??????? 3000.00???????????????????? 20
          ?7934 MILLER???? CLERK???????????????7782????1982-1-23??????? 1300.00?????????????????????10


          dept表

          DEPTNO????DNAME?????????????LOC
          ------????--------------????-------------
          ??? 10???????ACCOUNTING???? NEW YORK
          ??? 20????RESEARCH?????????????DALLAS
          ??? 30????SALES??????????????????????CHICAGO
          ??? 40????OPERATIONS??????? BOSTON

          posted on 2006-09-04 22:43 JavaCoffe 閱讀(874) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): Oralce&&PL/SQL


          FeedBack:
          # re: 幾個(gè)高難度的select語(yǔ)句
          2006-09-10 20:18 | 徐海東
          第6題的另外一個(gè)答案:

          select e.deptno as 部門(mén) ,e.ename as 員工名稱(chēng),nvl(d.ename,'未知') as 上級(jí)名稱(chēng)
          from emp e left join emp d on e.mgr = d.empno  回復(fù)  更多評(píng)論
            
          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          常用鏈接

          留言簿(5)

          隨筆分類(lèi)(80)

          收藏夾(1)

          最新隨筆

          積分與排名

          • 積分 - 58578
          • 排名 - 894

          最新評(píng)論

          閱讀排行榜

          主站蜘蛛池模板: 慈利县| 兰州市| 湾仔区| 增城市| 嵩明县| 松原市| 毕节市| 类乌齐县| 天镇县| 郁南县| 鲜城| 松原市| 定襄县| 万源市| 安化县| 齐河县| 云安县| 五莲县| 浙江省| 牙克石市| 库车县| 岑巩县| 枣庄市| 互助| 习水县| 太仓市| 陕西省| 胶南市| 岐山县| 岳阳县| 塘沽区| 五河县| 临泽县| 宁波市| 岑巩县| 青铜峡市| 嘉荫县| 儋州市| 甘肃省| 神木县| 黔西|