国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Sql面試常考題(持續添加)

2024-07-21 02:47:24
字體:
來源:轉載
供稿:網友
Sql面試常考題(持續添加)

  最近萌生換工作的念頭,于是上網下載了一些公司的面試題,重新看了面試題中的Sql部分,這些查詢題有時候只是兜一個彎角來考,對于給EF慣壞的孩子來說還是有點難度的(給面試官鄙視了幾下的結果),所以列出最近感覺比較有意思的Sql查詢題。

1.查詢出子節點最多的NodeName,如下圖的table,

NodeName子節點
節點11
節點22
節點31
節點31
節點31
節點42
節點43

 1 declare @t table( id int ,NodeName varchar(50 ),parentId int) 2  3 insert into @t 4 select 4, '節點1' ,1 5 union all 6 select 5, '節點2' ,2 7 union all 8 select 6, '節點3' ,1 9 union all10 select 7, '節點3' ,111 union all12 select 1, '節點3' ,113 union all14 select 2, '節點4' ,215 union all16 select 3, '節點4' ,317 18 select * from @t19 20 select top 1  nodename, COUNT(*) from @t group by NodeName order by COUNT(*) desc
View Code

2.有表A如下圖,需要轉換成表B格式

單號金額

Rk1 10

Rk2 20

Rk3 -30

Rk4 -10

  表A

單號 收入 支出

Rk1 10 0

Rk2 20 0

Rk3 0 30

Rk4 0 10

  表B

 1 declare @t table(danhao nvarchar(20),amount int) 2 insert into @t 3 select 'PK1',10 UNION 4 select 'PK2',20 UNION 5 select 'PK3',-10 UNION 6 select 'PK4',-30  7 select * from @t 8 select danhao, 9 (case when amount>0 then amount else 0 end) as N'收入',10 (case when amount>0 then 0 else amount end) as N'支出'11 from @t
View Code

3.有一張表T_Scores,記錄比賽成績

Date Name Score2008-8-8 拜仁 勝2008-8-9 奇才 勝2008-8-9 湖人 勝2008-8-10 拜仁 負2008-8-8 拜仁 負2008-8-12 奇才 勝要求輸出下面的格式:Name 勝 負拜仁 1 2湖人 1 0奇才 2 0
 1 declare @t table(DateT datetime,name nvarchar(20),Score nvarchar(20)) 2 insert into @t 3 select '2008-8-8',N'拜仁',N'勝' union all 4 select '2008-8-8',N'奇才',N'勝' union all 5 select '2008-8-8',N'湖人',N'勝' union all 6 select '2008-8-8',N'拜仁',N'負' union all 7 select '2008-8-8',N'拜仁',N'勝' union all 8 select '2008-8-8',N'拜仁',N'勝' union all 9 select '2008-8-8',N'奇才',N'勝' union all10 select '2008-8-8',N'湖人',N'負'11 select name,12 SUM(case Score when N'勝' then 1 else 0 end)as N'勝',13 SUM(case Score when N'負' then 1 else 0 end)as N'負'14 from @t15 group by name
View Code

4.根據下圖列表求和

idvalue
11
22
52
62
83
94

 1 declare @t table( id int ,value int ) 2 insert into @t 3 select 1, 1 4 union all 5 select 2, 2 6 union all 7 select 5, 2 8 union all 9 select 6, 210 union all11 select 8, 312 union all13 select 9, 414 15 select * from @t16 --1.按id 排序,取得所有的奇數 (單數) 行value之和17 18 select SUM (m. value) from(19 select ROW_NUMBER () over (order by id )row, id,value from @t)m20 WHERE m .row% 2=121 22 --2.取得所有id 為奇數的行 value之和23 select SUM (value) from @t where id% 2=1
View Code

5.行轉列5.1與列轉行5.2

5.1如下圖所示

nameclassscore
張三語文74
張三數學83
張三物理93
李四語文74
李四數學84
李四物理94
轉換成
name語文數學物理
張三748393
李四748494
 1 declare @t table ( name varchar (10), 課程 varchar (10), score int ) 2  3 insert into @t 4 select ' 張三', '語文' ,74 5 union all 6 select ' 張三', '數學' ,83 7 union all 8 select ' 張三', '物理' ,93 9 union all10 select ' 李四', '語文' ,7411 union all12 select ' 李四', '數學' ,8413 union all14 select ' 李四', '物理' ,9415 16 select * from @t17 18 select name,19 max(case 課程 when '語文' then score else 0 end) 語文 ,20 max(case 課程 when '數學' then score else 0 end) 數學 ,21 max(case 課程 when '物理' then score else 0 end) 物理22 from @t23 group by name
View Code

5.2列轉行

 1 declare @t table ( 姓名 varchar (10), 語文 int ,數學 int,物理 int) 2  3 insert into @t 4 select ' 張三', 74,83 ,93 5 union all 6 select ' 李四', 74,84 ,94 7  8 select * from 9 (10  select 姓名,課程 ='語文 ',分數 =語文 from @t11  union all12  select 姓名,課程 ='數學 ',分數 =數學 from @t13  union all14  select 姓名,課程 ='物理 ',分數 =物理 from @t15 )m
View Code

  后期等待多了之后再用心整理成一份Sql文檔,現在題目還少,努力去涵蓋面試中遇到的,謝謝觀看。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 恩平市| 龙泉市| 黄石市| 南宁市| 武定县| 佛学| 营山县| 常州市| 长乐市| 新闻| 新蔡县| 吴川市| 上犹县| 沾化县| 沧源| 留坝县| 栾城县| 合江县| 怀柔区| 梁河县| 台前县| 石柱| 岑溪市| 固安县| 炎陵县| 宜兰县| 龙岩市| 辉南县| 钟祥市| 琼海市| 成都市| 云浮市| 宜都市| 化德县| 棋牌| 台安县| 开阳县| 凤台县| 木里| 祁连县| 临高县|