Sql面试常考题(持续添加)

王朝学院·作者佚名  2016-05-25
窄屏简体版  字體: 小  |  中  |  大  |  超大  

Sql面试常考题(持续添加)最近萌生换工作的念头,于是上网下载了一些公司的面试题,重新看了面试题中的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(*) 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.根据下图列表求和

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=1

View 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 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文档,现在题目还少,努力去涵盖面试中遇到的,谢谢观看。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航