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 all
10 select 7, '节点3' ,1
11 union all
12 select 1, '节点3' ,1
13 union all
14 select 2, '节点4' ,2
15 union all
16 select 3, '节点4' ,3
17 
18 select * from @t
19 
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   Score 
2008-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 all
10 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 @t
15 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, 2
10 union all
11 select 8, 3
12 union all
13 select 9, 4
14 
15 select * from @t
16 --1.按id 排序,取得所有的奇数 (单数) 行value之和
17 
18 select SUM (m. value) from(
19 select ROW_NUMBER () over (order by id )row, id,value from @t)m
20 WHERE m .row% 2=1
21 
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 all
10 select ' 李四', '语文' ,74
11 union all
12 select ' 李四', '数学' ,84
13 union all
14 select ' 李四', '物理' ,94
15 
16 select * from @t
17 
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 @t
23 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 @t
11  union all
12  select 姓名,课程 ='数学 ',分数 =数学 from @t
13  union all
14  select 姓名,课程 ='物理 ',分数 =物理 from @t
15 )m
View Code

 

   后期等待多了之后再用心整理成一份Sql文档,现在题目还少,努力去涵盖面试中遇到的,谢谢观看。

posted @ 2015-04-07 22:12  PowerDK  阅读(1675)  评论(3编辑  收藏  举报