oracle pl/sql 面试,Oracle SQL 面试题(整理)
1、關于group by表內容:
2005-05-09?勝
2005-05-09?勝
2005-05-09?負
2005-05-09?負
2005-05-10?勝
2005-05-10?負
2005-05-10?負
如果要生成下列結果,?該如何寫sql語句?
勝 負
2005-05-09 2 2
2005-05-10 1 2
創建過程如下:
create table tmp(rq varchar(10),shengfu nchar(1));
insert into tmp values('2005-05-09','勝');insert into tmp values('2005-05-09','勝');insert into tmp values('2005-05-09','負');insert into tmp values('2005-05-09','負');insert into tmp values('2005-05-10','勝');insert into tmp values('2005-05-10','負');insert into tmp values('2005-05-10','負');
方法一:利用子查詢
select a.rq,a.勝,b.負 from?(select rq,count(shengfu) as 勝 from tmp where shengfu='勝' group by rq) a,(select rq,count(shengfu) as 負 from tmp where shengfu='負' group by rq) b?? where a.rq=b.rq
方法二:高級查詢---使用case表達式
case表達式可以在sql中實現if-then-else型的邏輯,而不用去使用PL/SQL。case工作方式與decode類似,但是我們應該使用case,因為它與ansi兼容。
有兩種類型的case表達式
a 簡單case表達式,使用case表達式確定返回值
b 搜索case表達式,使用條件確定返回值
select rq,sum(case when shengfu='勝' then 1 else 0 end) as 勝,
sum(case when shengfu='負'then 1 else 0 end) as 負from tmp?group by rq
方法三:通過表的內連接與子查詢聯合實現,可以說是另一種方式,思想類似于方法一。
select a.rq,a.勝,b.負 from?? (select rq,count(shengfu) 勝 from tmp where shengfu='勝'group by rq) a innerjoin?? (select rq,count(shengfu) 負 from tmp where shengfu='負'group by rq) b
on a.rq=b.rq
2.表中有A B C三列,用SQL語句實現:當A列大于B列時選擇A列否則選擇B列,當B列大于C列時選擇B列否則選擇C列。
create table tmp2(a int,b int ,c int);
insert into tmp values(10,20,30);
insert into tmp values(5,20,30);
insert into tmp values(10,7,30);
select?(case?when?a>b?then?a?else?b?end),(case?when?b>c?then?b?else?c?end)?from?tmp2;
3.一個日期判斷的sql語句
請取出tab5表中日期(SendTime字段)為當天的所有記錄?(SendTime字段為datetime型,包含日期與時間)
select?*?from?table?t?where?to_char(t.SendTime,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')
此題關鍵在于轉換sendtime字段的格式,轉換成日期格式。
4.有一張表,里面有3個字段:語文,數學,英語。其中有3條記錄分別表示語文70分,數學80分,英語58分,請用一條sql語句查詢出這三條記錄并按以下條件顯示出來(并寫出您的思路):
大于或等于80表示優秀,大于或等于60表示及格,小于60分表示不及格。
顯示格式:
語文??????????????數學????????????????英語
及格??????????????優秀????????????????不及格
-------------------------------------------------------------------------------------------
select
(case?when語文>=80?then?'優秀'?when語文>60?then?'及格'?else?'不及格'?end)?as?語文,
(case?when?數學>=80?then?'優秀'?when數學>60?then?'及格'?else?'不及格'?end)?as數學,
(case?when英語>=80?then?'優秀'?when英語>60?then?'及格'?else?'不及格'?end)?as?英語
from?table
想到利用case表達式是關鍵。
5.請用一個sql語句得出結果
從table1,table2中取出如table3所列格式數據
table1
月份mon?部門dep???業績yj
-------------------------------
一月份????? 01????????10
一月份????? 02????????10
一月份????? 03?????????5
二月份????? 02?????????8
二月份????? 04?????????9
三月份????? 03?????????8
table2
部門dep??????部門名稱depname
01??????國內業務一部
02??????國內業務二部
03??????國內業務三部
04??????國際業務部
table3?(result)
部門dep?????一月份??????二月份??????三月份
01????? 10??????? null?????????null
02????? 10???????? 8???????????null
03????? 5????????? null??????????8
04????? null????? 9???????????null
1)
select?t.depname,
(select?yj?from?table1?where?mon='一月份'?and?dep=t.dep)?一月份,
(select?yj?from?table1?where?mon='二月份'?and?dep=t.dep)?二月份,
(select?yj?from?table1?where?mon='三月份'?and?dep=t.dep)?三月份
from?table1 t
2)求總銷售額
select
sum(case?when?t1.mon='一月份'?then?t1.yj?else?0?end)?一月份,
sum(case?when?t1.mon='二月份'?then?t1.yj?else?0?end)?二月份,
sum(case?when?t1.mon='三月份'?then?t1.yj?else?0?end)?三月份
from?tab7 t,tab6 t1 where t.dep=t1.dep
6.一個表中的Id有多個記錄,把所有這個id的記錄查出來,并顯示共有多少條記錄數。
select?id,count(*)?from?tab8?group?by?id?having?count(*)>1
select?*?from?(select?tab8,count(id)?as?num?from?tab8?group?by?id) t?where?t.num>1
7.用一條SQL語句 查詢出每門課都大于80分的學生姓名
name?? kecheng?? fenshu張三?????語文81
張三?????數學75
李四?????語文76
李四?????數學90
王五?????語文81
王五?????數學100
王五?????英語90
a):select?distinct?name?from?tab9?where?name?not?in?(select?distinct?name?from?tab9?where?fengshu<=80)
b):select?*?from?tab9 t7?where?t7.name?not?in?(select?t5.name?from?(select?*?from?(select?t1.kecheng?from?tab9 t1?group?by?t1.kecheng),(select?t2.name?fromtab9 t2?group?by?t2.name)) t4,(select?*?from?tab9) t5?where?t4.name?= t5.name?and?t4.kecheng = t5.kecheng?and?t5.fengshu
8.一個叫department的表,里面只有一個字段name,一共有4條紀錄,分別是a,b,c,d,對應四個球對,現在四個球對進行比賽,用一條sql語句顯示所有可能的比賽組合.
select?t.bh||'vs'||t1.bh?from?tab10 t,tab10 t1?where?t.bh<>t1.bh這個是分主客場的
select?t.bh||'vs'||t1.bh?from?tab10 t,tab10 t1?where?t.bh<>t1.bh?and?t.bh>t1.bh這個是不分的
9.怎么把這樣一個表兒
year?? month amount
1991?? 1???? 1.1
1991?? 2???? 1.2
1991?? 3???? 1.3
1991?? 4???? 1.4
1992?? 1???? 2.1
1992?? 2???? 2.2
1992?? 3???? 2.3
1992?? 4???? 2.4
查成這樣一個結果
year m1?? m2?? m3?? m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
a):
select?t.year,
(select?a.amout?from?tab11 a?where?a.month=1?and?a.year=t.year) m1,
(select?b.amout?from?tab11 b?where?b.month=2?and?b.year=t.year) m2,
(select?c.amout?from?tab11 c?where?c.month=3?and?c.year=t.year) m3,
(select?d.amout?from?tab11 d?where?d.month=4?and?d.year=t.year) m4
from?tab11 t?group?by?t.year
10.拷貝表(拷貝數據,源表名:a?目標表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;
create table test as select * from dept; --從已知表復制數據和結構
create table test as select * from dept where 1=2; --從已知表復制結構但不包括數據
11.顯示文章、提交人和最后回復時間
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
13.兩張關聯表,刪除主表中已經在副表中沒有的信息
delete?from?fubiao a?where?a.fid?not?in(select?id?from?zhubiao)
14.有兩個表tab12和tab13,均有key和value兩個字段,如果tab13的key在tab12中也有,就把tab13的value換為tab12中對應的value
update?tab13?set?value=(select?value?from?tab12?where?tab12.key=tab13.key)
總結
以上是生活随笔為你收集整理的oracle pl/sql 面试,Oracle SQL 面试题(整理)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 个人信用卡不良记录能不能消除 秘诀只告诉
- 下一篇: linux cmake编译源码,linu