最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Oracle的四道經(jīng)典面試題分享

 更新時間:2019年05月09日 11:44:02   作者:gaojinshun  
這篇文章主要給大家介紹了關(guān)于Oracle的四道經(jīng)典面試題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Oracle具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

本文整理了4道Oracle 經(jīng)典面試題,與大家分享學(xué)習(xí)。這也許是你一直期待的文章,下面話不多說了,來一起看看詳細(xì)的介紹吧

第一題

create table test(
 id number(10) primary key,
 type number(10) ,
 t_id number(10),
 value varchar2(6)
);

insert into test values(100,1,1,'張三');
insert into test values(200,2,1,'男');
insert into test values(300,3,1,'50');

insert into test values(101,1,2,'劉二');
insert into test values(201,2,2,'男');
insert into test values(301,3,2,'30');

insert into test values(102,1,3,'劉三');
insert into test values(202,2,3,'女');
insert into test values(302,3,3,'10');

select * from test;

代碼生成表格如:

根據(jù)以上代碼生成的表寫出一條查詢語句,查詢結(jié)果如下:

姓名 性別 年齡
張三 50
劉二 30
劉三 10

/*
根據(jù)表格可以分析出type列中1代表姓名、2代表性別、3代表年齡,而t_id中id一樣的為同一個人的屬性
查詢結(jié)果中列依次為姓名、性別、年齡,而type列決定姓名、性別、年齡
*/

/*使用分組,先對t_id進(jìn)行分組,然后用decode函數(shù)過濾數(shù)據(jù),例:decode(type, 1, value) type=1就顯示為value
由于分組后select后面的列字段只能是分組的字段或者組函數(shù),所有使用max()。
同一個人的type沒有重復(fù)數(shù)值所以 decode(type, 1, value)返回的值只有一個,最大值也就是這個值
*/
select max(decode(type, 1, value)) "姓名",
  max(decode(type, 2, value)) "性別",
  max(decode(type, 3, value)) "年齡"
 from test
 group by t_id;

/*使用連表,通過where過濾生成3張type分別等于1(姓名)、2(性別)、3(年齡)的3張?zhí)摂M表 如:

再通過where 連接條件 三張表t_id相等的為同一個人或者說同一條記錄(行)
*/
select t1.value "姓名",t2.value "性別",t3.value "年齡" from 
(select value,t_id from test where type=1) t1,
(select value,t_id from test where type=2) t2,
(select value,t_id from test where type=3) t3
where t1.t_id=t2.t_id and t1.t_id=t3.t_id;

第二題

/*

2.一道SQL語句面試題,關(guān)于group by
表內(nèi)容:
2005-05-09 勝
2005-05-09 勝
2005-05-09 負(fù)
2005-05-09 負(fù)
2005-05-10 勝
2005-05-10 負(fù)
2005-05-10 負(fù)

如果要生成下列結(jié)果, 該如何寫sql語句?

   勝 負(fù)
2005-05-09 2 2
2005-05-10 1 2
------------------------------------------
create table tmp(rq varchar2(10),shengfu varchar2(5));

insert into tmp values('2005-05-09','勝');
insert into tmp values('2005-05-09','勝');
insert into tmp values('2005-05-09','負(fù)');
insert into tmp values('2005-05-09','負(fù)');
insert into tmp values('2005-05-10','勝');
insert into tmp values('2005-05-10','負(fù)');
insert into tmp values('2005-05-10','負(fù)');

select * from tmp;
*/

--使用分組
--按日期分組,用conut函數(shù)計(jì)算次數(shù)
select rq "日期",
  count(decode(shengfu, '勝', 1)) "勝",
  count(decode(shengfu, '負(fù)', 1)) "負(fù)"
 from tmp
 group by rq
 order by rq;

--使用連表
--這道題本身就需要分組,不建議使用連表做
--以下使用的是SQL1999的連表方式,語法不一樣效果與第一題使用的SQL1992的一樣
select t1.rq,t1.勝, t2.負(fù) from
(select count(decode(shengfu, '勝', 1)) "勝", rq from tmp group by rq) t1
join
(select count(decode(shengfu, '負(fù)', 1)) "負(fù)", rq from tmp group by rq) t2
on t1.rq=t2.rq;

第三題

/*3.生成題目所需的表

create table STUDENT_SCORE
(
 name VARCHAR2(20),
 subject VARCHAR2(20),
 score NUMBER(4,1)
);
insert into student_score (NAME, SUBJECT, SCORE) values ('張三', '語文', 78.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('張三', '數(shù)學(xué)', 88.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('張三', '英語', 98.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '語文', 89.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '數(shù)學(xué)', 76.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('李四', '英語', 90.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '語文', 99.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '數(shù)學(xué)', 66.0);
insert into student_score (NAME, SUBJECT, SCORE) values ('王五', '英語', 91.0);

3.1得到類似下面的結(jié)果
姓名 語文 數(shù)學(xué) 英語
王五 89 56 89
李四 xx xx xx
select * from STUDENT_SCORE;

3.2有一張表,里面有3個字段:語文,數(shù)學(xué),英語。其中有3條記錄分別表示語文70分,數(shù)學(xué)80分,英語58分,
請用一條sql語句查詢出這三條記錄并按以下條件顯示出來(并寫出您的思路): 
大于或等于80表示優(yōu)秀,大于或等于60表示及格,小于60分表示不及格。 
  顯示格式: 
  語文    數(shù)學(xué)    英語 
  及格    優(yōu)秀    不及格 
------------------------------------------
*/

--3.1
--使用分組
select name "姓名",
  max(decode(subject, '語文' ,score)) "語文",
  max(decode(subject, '數(shù)學(xué)' ,score)) "數(shù)學(xué)",
  max(decode(subject, '英語' ,score)) 英語
 from STUDENT_SCORE
 group by name;

--使用連表

select t1.name 姓名, t1.score 語文, t2.score 數(shù)學(xué), t3.score 英語 from
(select name,score from STUDENT_SCORE where subject='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數(shù)學(xué)') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on t1.name=t3.name;

--3.2
--在3.1的基礎(chǔ)上使用 case when then esle end
select t.姓名,
(case when t.語文>=80 then '優(yōu)秀'
   when t.語文>=60 then '及格'
   else '不及格' end) 語文,
(case when t.數(shù)學(xué)>=80 then '優(yōu)秀'
   when t.數(shù)學(xué)>=60 then '及格'
   else '不及格' end) 數(shù)學(xué),
(case when t.英語>=80 then '優(yōu)秀'
   when t.英語>=60 then '及格'
   else '不及格' end) 英語
 from 
(select t1.name 姓名, t1.score 語文, t2.score 數(shù)學(xué), t3.score 英語 from
(select name,score from STUDENT_SCORE where subject='語文') t1
join
(select name,score from STUDENT_SCORE where subject='數(shù)學(xué)') t2
on t1.name=t2.name
join
(select name,score from STUDENT_SCORE where subject='英語') t3
on t1.name=t3.name
) t;

第四題(這道題難度相對較高)

/*4.請用一個sql語句得出結(jié)果
從table1,table2中取出如table3所列格式數(shù)據(jù),注意提供的數(shù)據(jù)及結(jié)果不準(zhǔn)確,
只是作為一個格式向大家請教。


table1

月份mon 部門dep 業(yè)績yj
-------------------------------
一月份  01  10
一月份  02  10
一月份  03  5
二月份  02  8
二月份  04  9
三月份  03  8

table2

部門dep  部門名稱dname
--------------------------------
  國內(nèi)業(yè)務(wù)一部
  國內(nèi)業(yè)務(wù)二部
  國內(nèi)業(yè)務(wù)三部
  國際業(yè)務(wù)部

table3 (result)

部門dep 一月份  二月份  三月份
--------------------------------------
  10  null  null
  10   8  null
  null  5  8
  null  null  9

------------------------------------------

create table yj01(
  month varchar2(10),
  deptno number(10),
  yj number(10)
)

insert into yj01(month,deptno,yj) values('一月份',01,10);
insert into yj01(month,deptno,yj) values('二月份',02,10);
insert into yj01(month,deptno,yj) values('二月份',03,5);
insert into yj01(month,deptno,yj) values('三月份',02,8);
insert into yj01(month,deptno,yj) values('三月份',04,9);
insert into yj01(month,deptno,yj) values('三月份',03,8);

create table yjdept(
  deptno number(10),
  dname varchar2(20)
)

insert into yjdept(deptno,dname) values(01,'國內(nèi)業(yè)務(wù)一部');
insert into yjdept(deptno,dname) values(02,'國內(nèi)業(yè)務(wù)二部');
insert into yjdept(deptno,dname) values(03,'國內(nèi)業(yè)務(wù)三部');
insert into yjdept(deptno,dname) values(04,'國際業(yè)務(wù)部');
*/
select * from yj01;
select * from yjdept;
--使用分組
select deptno,
max(decode(month,'一月份',yj)) 一月份, 
max(decode(month,'二月份',yj)) 二月份, 
max(decode(month,'三月份',yj)) 三月份 
from yj01 group by deptno
order by deptno;

--這道題給出了兩張表,而用分組做,使用yj01表就能做出來了,所以這道題考察的應(yīng)該是連表的知識
/*這兩張表中有的月份有的部門業(yè)績是空的,而用前幾道題的做法,不匹配條件的值會被過濾掉,
例如month=一月份的只有1部門,形成的表里deptno只有1和二月份、三月份形成的表中的deptno無法匹配
而yjdept表中包含了所有部門編號deptno,這時就可以用到外連接的特性
(在滿足一張表的內(nèi)容都顯示的基礎(chǔ)上,連接另外一張表,如果連接匹配則正常顯示,連接不匹配,另外一張表補(bǔ)null)
*/
select t1.deptno, t1.yj 一月份, t2.yj 二月份, t3.yj 三月份
from
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='一月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t1
join
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='二月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t2
on t1.deptno=t2.deptno
join
(select y2.deptno,y1.yj from
(select yj, deptno from yj01 where month='三月份') y1 right join yjdept y2 on y1.deptno=y2.deptno)t3
on t1.deptno=t3.deptno
order by t1.deptno;

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • 深入oracle分區(qū)索引的詳解

    深入oracle分區(qū)索引的詳解

    本篇文章是對oracle分區(qū)索引進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 簡述Oracle中in和exists的不同

    簡述Oracle中in和exists的不同

    大家一直認(rèn)為exists比in速度快,其實(shí)是不準(zhǔn)確的,接下來腳本之家小編給大家分享Oracle中in和exists的不同,感興趣的朋友參考下吧
    2017-09-09
  • oracle中exp,imp的使用詳解

    oracle中exp,imp的使用詳解

    這篇文章主要介紹了oracle中exp,imp的使用詳解,需要的朋友可以參考下
    2015-07-07
  • oracle數(shù)據(jù)庫如何給用戶授權(quán)查詢權(quán)限

    oracle數(shù)據(jù)庫如何給用戶授權(quán)查詢權(quán)限

    這篇文章主要給大家介紹了關(guān)于oracle數(shù)據(jù)庫如何給用戶授權(quán)查詢權(quán)限的相關(guān)資料,授予權(quán)限是數(shù)據(jù)庫管理中的重要任務(wù),它可以確保用戶有權(quán)訪問所需的數(shù)據(jù)庫對象,并執(zhí)行必要的操作,需要的朋友可以參考下
    2024-01-01
  • 怎么才能限制SQL Server只能讓指定的機(jī)器連接

    怎么才能限制SQL Server只能讓指定的機(jī)器連接

    怎么才能限制SQL Server只能讓指定的機(jī)器連接...
    2007-03-03
  • oracle group by語句實(shí)例測試

    oracle group by語句實(shí)例測試

    本文將詳細(xì)介紹oracle group by語句,以實(shí)例進(jìn)行測試,需要的朋友可以參考下
    2012-11-11
  • 解決oracle用戶連接失敗的解決方法

    解決oracle用戶連接失敗的解決方法

    oracle用戶連接失敗的問題著實(shí)讓我們頭疼,下面就為您介紹oracle用戶連接失敗的解決方法,希望對您學(xué)習(xí)oracle用戶連接方面能有所幫助。
    2010-12-12
  • Oracle date如何比較大小分析

    Oracle date如何比較大小分析

    本文將詳細(xì)介紹Oracle date如何比較大小,需要了解更多的朋友可以參考下
    2012-11-11
  • Oracle常見分析函數(shù)實(shí)例詳解

    Oracle常見分析函數(shù)實(shí)例詳解

    分析函數(shù)是Oracle專門用于解決復(fù)雜報(bào)表統(tǒng)計(jì)需求的功能強(qiáng)大的函數(shù),它可以在數(shù)據(jù)行分組然后計(jì)算基于組的某種統(tǒng)計(jì)值,并且每一組的每一行都可以返回一個統(tǒng)計(jì),下面這篇文章主要給大家介紹了關(guān)于Oracle常見分析函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Oracle 存儲過程總結(jié)(一、基本應(yīng)用)

    Oracle 存儲過程總結(jié)(一、基本應(yīng)用)

    Oracle 存儲過程總結(jié) 基本應(yīng)用技巧,大家可以學(xué)習(xí)下oracle存儲過程最基本的東西。
    2009-07-07

最新評論

阳曲县| 抚州市| 西畴县| 伽师县| 保山市| 沾化县| 霍邱县| 大港区| 滦平县| 化州市| 沙雅县| 商洛市| 囊谦县| 浪卡子县| 崇仁县| 铜陵市| 清原| 汉寿县| 博乐市| 潮安县| 禄丰县| 遂平县| 天柱县| 兰考县| 达孜县| 明水县| 涟源市| 区。| 宁国市| 开化县| 白水县| 喀喇沁旗| 措勤县| 成都市| 龙泉市| 大名县| 莲花县| 博客| 浏阳市| 和林格尔县| 即墨市|