日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
數(shù)據(jù)庫(kù)點(diǎn)滴之T-SQL面試語句,練練手

1. 用一條SQL語句 查詢出每門課都大于80分的學(xué)生姓名

臨沂網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,臨沂網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為臨沂千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的臨沂做網(wǎng)站的公司定做!

  • name kecheng fenshu
  • 張三   語文       81
  • 張三   數(shù)學(xué)       75
  • 李四   語文       76
  • 李四   數(shù)學(xué)       90
  • 王五   語文       81
  • 王五   數(shù)學(xué)     100
  • 王五   英語       90

思路:這里不能直接用 分?jǐn)?shù)>80這樣的比較條件來查詢的到結(jié)果,因?yàn)橐鬀]門成績(jī)都大于80。我們可以反過來思考,如果有一門成績(jī)小于80,那么就不符合要求。先找出成績(jī)表中成績(jī)<80的多有學(xué)生姓名,不能重復(fù),然后再用not in找出不再這個(gè)集合中的學(xué)生姓名。

 
 
 
  1. create table #成績(jī)(姓名varchar(20),課程名稱varchar(20),分?jǐn)?shù)int)
  2. insert into #成績(jī)values
  3. ('張三',     '語文',       81),
  4. ('張三',     '數(shù)學(xué)',       75),
  5. ('李四',     '語文',       76),
  6. ('李四',     '數(shù)學(xué)',       90),
  7. ('王五',     '語文',      81),
  8. ('王五',     '數(shù)學(xué)',       100),
  9. ('王五',     '英語',       90)
  10. select distinct(姓名) from #成績(jī) where 姓名 not in(select distinct(姓名) from #成績(jī) where 分?jǐn)?shù)<=80)

經(jīng)luofer提示還有一種思路,是用group by + hvaing,這絕對(duì)是一種好方法。我估計(jì)出這個(gè)題的人就是要考察這個(gè)知識(shí),代碼如下:

 
 
 
  1. select 姓名 from #成績(jī) 
  2. group by 姓名
  3. having min(分?jǐn)?shù))>80

2. 學(xué)生表 如下:

  • 自動(dòng)編號(hào)     學(xué)號(hào)       姓名 課程編號(hào) 課程名稱 分?jǐn)?shù)
  •        1         2005001    張三      0001          數(shù)學(xué)       69
  •        2         2005002    李四      0001          數(shù)學(xué)       89
  •        3         2005001    張三      0001          數(shù)學(xué)       69

刪除除了自動(dòng)編號(hào)不同,其他都相同的學(xué)生冗余信息

思路:這個(gè)和上面的一樣,也不能直接刪除,而是要先找出自動(dòng)編號(hào)不相同,其他都相同的行,這個(gè)要使用group by語句,并且將其他的字段都放在group by后面,這樣找出來的行都是沒有冗余的行,然后隨便保留其中一個(gè)自動(dòng)編號(hào),刪除其他的行。

 
 
 
  1. create table #成績(jī)(自動(dòng)編號(hào) int, 學(xué)號(hào) int,姓名 varchar(20),課程編號(hào) int,課程名稱 varchar(20),分?jǐn)?shù) int)
  2. insert into #成績(jī) values
  3. (1,2005001 ,'張三',  1,   '語文',       81),
  4. (2,2005001 ,'李四',  1,   '語文',       81),
  5. (3,2005001 ,'張三',  1,   '語文',       81),
  6. (4,2005001 ,'張三',  1,   '語文',       81)
  7. select * from #成績(jī)
  8. drop table #成績(jī)
  9. delete from #成績(jī) where 自動(dòng)編號(hào) not in
  10. (select MIN(自動(dòng)編號(hào)) from #成績(jī) group by 學(xué)號(hào),姓名,課程編號(hào),課程名稱,分?jǐn)?shù))

經(jīng)【廣島之戀】的提醒發(fā)現(xiàn)另外一種思路,代碼如下:

 
 
 
  1. delete from #成績(jī) where 自動(dòng)編號(hào) not in
  2. (select distinct(a.自動(dòng)編號(hào)) from #成績(jī) a join #成績(jī) b on a.自動(dòng)編號(hào)>b.自動(dòng)編號(hào) 
  3. where a.學(xué)號(hào)=b.學(xué)號(hào) and a.姓名=b.姓名 and a.課程編號(hào)=b.課程編號(hào) and a.分?jǐn)?shù)=b.分?jǐn)?shù))

3. 一個(gè)叫department的表,里面只有一個(gè)字段name,一共有4條紀(jì)錄,分別是a,b,c,d,對(duì)應(yīng)四個(gè)球?qū)?,現(xiàn)在四個(gè)球?qū)M(jìn)行比賽,用一條sql語句顯示所有可能的比賽組合。

思路:這是一個(gè)組合問題,就是說四個(gè)不同的元素有多少種不同的兩兩組合?,F(xiàn)在要把這個(gè)問題用sql語句實(shí)現(xiàn)。既然這四個(gè)元素是不相同的,我們可以將這個(gè)表當(dāng)成兩個(gè)集合,求他們的笛卡爾積,然后再?gòu)牡芽柗e中找到那些元素不相同的,并且不重復(fù)的組合。

 
 
 
  1. create table #department(taname char(1))
  2. insert into #department values
  3. ('a'),('b'),('c'),('d')
  4. --下面兩條語句都可以,多謝wanglinglong提醒
  5. select a.taname,b.taname from #department a,#department b where a.taname < b.taname
  6. select a.taname,b.taname from #department a,#department b where a.taname > b.taname

4.怎么把這樣一個(gè)表

  • 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

查成這樣一個(gè)結(jié)果

  • year  m1 m2 m3 m4
  • 1991 1.1 1.2 1.3 1.4
  • 1992 2.1 2.2 2.3 2.4

思路:這個(gè)很明顯是一個(gè)行列轉(zhuǎn)換,首先會(huì)想到pivot。結(jié)果中有m1,m2,m3,m4四個(gè)新的列,他們需要從原來的行中轉(zhuǎn)換。

 
 
 
  1. create table #sales(years int,months int,amount float)
  2. insert into #sales values
  3. (1991,   1,     1.1),
  4. (1991,   2,     1.2),
  5. (1991,   3,     1.3),
  6. (1991,   4,     1.4),
  7. (1992,   1,     2.1),
  8. (1992,   2,     2.2),
  9. (1992,   3,     2.3),
  10. (1992,   4,     2.4)
  11. select pt.years,[1] as m1,[2] as m2,[3] as m3,[4] as m4 
  12. from (select sod.amount,sod.months,sod.years as years from  #sales sod)  so 
  13. pivot
  14. (min(so.amount) for so.months in ([1], [2],[3],[4])) as pt

注意[1],[2],[3],[4]中括號(hào)不可缺少,否則會(huì)出錯(cuò)。還有一種寫法是使用子查詢,這個(gè)要新建4個(gè)子查詢進(jìn)而得到新的列:

 
 
 
  1. select a.years,
  2. (select m.amount from #sales m where months=1 and m.years=a.years) as m1,
  3. (select m.amount from #sales m where months=2 and m.years=a.years) as m2,
  4. (select m.amount from #sales m where months=3 and m.years=a.years) as m3,
  5. (select m.amount from #sales m where months=4 and m.years=a.years) as m4
  6. from #sales a group by a.years

5.有兩個(gè)表A和B,均有key和value兩個(gè)字段,如果B的key在A中也有,就把B的value換為A中對(duì)應(yīng)的value。這道題的SQL語句怎么寫?

思路:這個(gè)問題看似簡(jiǎn)單,只要一個(gè)update語句,然后找到相同的key,更新value字段就可以了??赡苣闶紫葧?huì)寫成這樣:update #b set #b.value=(select #a.value from #a where #a.keys=#b.keys)。但是要注意的是如果僅僅找相同的key會(huì)有很多匹配,更新的時(shí)候會(huì)出現(xiàn)錯(cuò)誤,所有要在外層限制。

 
 
 
  1. create table #a(keys int , value varchar(10))
  2. insert into #a values
  3. (1,'aa'),
  4. (2,'ab'),
  5. (3,'ac')
  6. create table #b(keys int , value varchar(10))
  7. insert into #b values
  8. (1,'aa'),
  9. (2,'a'),
  10. (3,'a')
  11. update #b set #b.value=(select #a.value from #a where #a.keys=#b.keys) where #b.keys in
  12. (select #b.keys from #b,#a where #a.keys=#b.keys and #a.value<>#b.value)

在luofer的提醒之,有了第二個(gè)思路

 
 
 
  1. update #b set #b.value=s.value
  2. from (select * from #a except select * from #b) s where s.keys=#b.keys

luofer是牛人啊!

6. 兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒有的信息。

思路:這個(gè)就是存在關(guān)系,可以使用in,也可以使用exists。

 
 
 
  1. create table #zhubiao(id int,name varchar(5))
  2. insert into #zhubiao values
  3. (1,'aa'),
  4. (2,'ab'),
  5. (3,'ac')
  6. create table #fubiao(id int, grade varchar(5))
  7. insert into #fubiao values
  8. (1,'aa'),
  9. (2,'ab')
  10. delete from #zhubiao where id not in(select b.id from #fubiao b)
  11. delete from #zhubiao where not exists(select 1 from #fubiao where #zhubiao.id=#fubiao.id)

7. 原表:

  • courseid coursename score
  • 1        java    70
  • 2        oracle    90
  • 3            xml    40
  • 4            jsp    30
  • 5      servlet    80

為了便于閱讀,查詢此表后的結(jié)果顯式如下(及格分?jǐn)?shù)為60):

  • courseid coursename score   mark
  • 1             java   70  pass
  • 2            oracle    90  pass
  • 3              xml   40  fail
  • 4             jsp    30  fail
  • 5       servlet    80     pass

思路:這個(gè)就很直接了,使用case語句判斷一下。

 
 
 
  1. create table #scores(course int,coursename varchar(10),score int)
  2. insert into #scores values
  3. (1, 'java', 70 ),
  4. (2, 'oracle', 90),
  5. (3, 'xmls', 40),
  6. (4, 'jsp', 30), 
  7. (5, 'servlet', 80 )
  8. select course,coursename,
  9. case when score>60 then 'pass' else 'fail' end as mark
  10. from #scores

8. 原表:

  • id proid proname
  • 1      1        M
  • 1      2         F
  • 2      1        N
  • 2      2        G
  • 3      1        B
  • 3      2        A

查詢后的表:

  • id pro1 pro2
  •  1  M      F
  •  2  N      G
  •  3  B      A

思路:依舊是行列轉(zhuǎn)換,這個(gè)在面試中的幾率很高。這個(gè)語句還是有兩種寫法,如下:

 
 
 
  1. create table #table1(id int,proid int,proname char)
  2. insert into #table1 values
  3. (1, 1, 'M'),
  4. (1, 2, 'F'), 
  5. (2, 1, 'N'), 
  6. (2, 2, 'G'), 
  7. (3, 1, 'B'), 
  8. (3, 2, 'A')
  9. select id, 
  10. (select proname from #table1 where proid=1 and id=b.id) as pro1,
  11. (select proname from #table1 where proid=2 and id=b.id) as pro2
  12. from #table1 b group by id
  13. select d.id,[1] as pro1,[2] as pro2 from
  14. (select b.id,b.proid,b.proname from #table1 b) as c
  15. pivot
  16. (min(c.proname) for c.proid in([1],[2])) as d

9. 如下

  • 表a
  • 列     a1 a2
  • 記錄 1   a
  •           1   b
  •           2   x
  •           2   y
  •           2   z

用select能選成以下結(jié)果嗎?

1 ab

2 xyz

思路:這個(gè)開始想使用行列轉(zhuǎn)換來寫,沒有成功,后來沒有辦法只好用游標(biāo),代碼如下:

 
 
 
  1. create table #table2(id int , value varchar(10))
  2. insert into #table2 values
  3. (1,'a'),
  4. (1,'b'),
  5. (2,'x'),
  6. (2,'y'),
  7. (2,'z')
  8. create table #table3(id int,value varchar(100) );insert into #table3(id,value) select distinct(id),'' from #table2
  9. declare @id int,@name varchar(10)
  10. declare mycursor cursor for select * from #table2
  11. open mycursor
  12. fetch next from mycursor into @id,@name
  13. while (@@Fetch_Status = 0)
  14. begin
  15. update #table3 set value=value+@name where id=@id
  16. fetch next from mycursor into @id,@name 
  17. end
  18. close mycursor
  19. deallocate mycursor
  20. select * from #table3

有兩個(gè)要注意的地方,

a.#table3里面的value字段初始值如果不設(shè)置的話默認(rèn)是null,后面更新的時(shí)候null+'a'任然是null,***得到的value永遠(yuǎn)是null。所以默認(rèn)是''

b.第二個(gè)fetch語句一定要放在begin和end之間,要不然會(huì)死循環(huán)的,不常用的語句寫起來很不爽快


網(wǎng)站題目:數(shù)據(jù)庫(kù)點(diǎn)滴之T-SQL面試語句,練練手
網(wǎng)站網(wǎng)址:http://www.5511xx.com/article/dhjhjhs.html