最近萌生換工作的念頭,于是上網下載了一些公司的面試題,重新看了面試題中的Sql部分,這些查詢題有時候只是兜一個彎角來考,對于給EF慣壞的孩子來說還是有點難度的(給面試官鄙視了幾下的結果),所以列出最近感覺比較有意思的Sql查詢題。
1.查詢出子節點最多的NodeName,如下圖的table,
NodeName | 子節點 |
節點1 | 1 |
節點2 | 2 |
節點3 | 1 |
節點3 | 1 |
節點3 | 1 |
節點4 | 2 |
節點4 | 3 |
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(*) descView 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 @tView 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 01 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 nameView Code
4.根據下圖列表求和
id | value |
1 | 1 |
2 | 2 |
5 | 2 |
6 | 2 |
8 | 3 |
9 | 4 |
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=1View Code
5.行轉列5.1與列轉行5.2
5.1如下圖所示
name | class | score |
張三 | 語文 | 74 |
張三 | 數學 | 83 |
張三 | 物理 | 93 |
李四 | 語文 | 74 |
李四 | 數學 | 84 |
李四 | 物理 | 94 |
name | 語文 | 數學 | 物理 |
張三 | 74 | 83 | 93 |
李四 | 74 | 84 | 94 |
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 nameView 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 )mView Code
后期等待多了之后再用心整理成一份Sql文檔,現在題目還少,努力去涵蓋面試中遇到的,謝謝觀看。
新聞熱點
疑難解答