SQL数据库经典面试题(笔试题).pdf
文本预览下载声明
28
SQL 数据库经典面试题 (笔试题 )
1.一道 SQL语句面试题,关于 group by
表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果 , 该如何写 sql 语句 ?
胜 负
2005-05-09 2 2
2005-05-10 1 2
页脚内容 1
28
create table #tmp(rq varchar(10),shengfu nchar(1))
insert into #tmp values(2005-05-09,胜)
insert into #tmp values(2005-05-09,胜)
insert into #tmp values(2005-05-09,负)
insert into #tmp values(2005-05-09,负)
insert into #tmp values(2005-05-10,胜)
insert into #tmp values(2005-05-10,负)
insert into #tmp values(2005-05-10,负)
1)select rq, sum(case when shengfu=胜 then 1 else 0 end)胜,sum(case when shengfu=负 then 1 else 0 end)
负 from #tmp group by rq
2) select N.rq,N.勝 ,M.負 from (
select rq,勝 =count(*) from #tmp where shengfu=胜 group by rq)N inner join
(select rq,負=count(*) from #tmp where shengfu=负group by rq)M on N.rq=M.rq
3)select a.col001,a.a1胜,b.b1 负 from
页脚内容 2
28
(select col001,count(col001) a1 from temp1 where col002=胜 group by col001) a,
(select col001,count(col001) b1 from temp1 where col002=负 group by col001) b
where a.col001=b.col001
2.请教一个面试中遇到的 SQL语句的查询问题
表中有 A B C三列 ,用 SQL语句实现:当 A 列大于 B 列时选择 A 列否则选择 B 列,当 B 列大于 C列时
选择 B 列否则选择 C列。
select (case when ab then a else b end ),
(case when bc then b esle c end)
from table_name
3.面试题:一个日期判断的 sql语句?
请取出 tb_send表中日期 (SendTime字段 )为当天的所有记录 ?(SendTime字段为 datetime型,包含日期
与时间 )
select * from tb where datediff(dd,SendTime,getdate())=0
显示全部