常见SQL笔试题.doc
文本预览下载声明
SQL笔试题
1.统计查询SQL练习
数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察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)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
(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
4.统计查询SQL练习
有一张表,里面有3 个字段:语文,数学,英语。其中有3 条记录分别表示语文70
分,数学80 分,英语58 分,请用一条sql 语句查询出这三条记录并按以下条件显示
出来(并写出您的思路):
大于或等于80 表示优秀,大于或等于60 表示及格,小于60 分表示不及格。
显示格式:
语文数学英语
及格优秀不及格
答案:
select
(case when 语文=80 then 优秀
when 语文=60 then 及格
else 不及格) as 语文,
(case when 数学=80 then 优秀
when 数学=60 then 及格
else 不及格) as 数学,
(case when 英语=80 then 优秀
when 英语=60 then 及格
else 不及格) as 英语,
from table
7.请用一个sql 语句得出结果,从table1,table2 中取出如table3 所列格式数据
table1
月份mon 部门dep 业绩yj
答案:-------------
一月份01 10
一月份02 10
一月份03 5
二月份02 8
二月份04 9
三月份03 8
table2
部门dep 部门名称dname
答案:--------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部
table3 (result)
部门dep 一月份二月份三月份
答案:答案:--
01 10 null null
02 10 8 null
03 null 5 8
04 null null 9
答案:
1)
select a.部门名称dname,b.业绩yj as 一月份,c.业绩yj as 二月份,d.业绩yj as 三月份
from table1 a,table2 b,table2 c,table2 d
where a.部门dep = b.部门dep and b.月份mon = 一月份 and
a.部门dep = c.部门dep and c.月份mon = 二月份 and
a.部门dep = d.部门dep and d.月份mon = 三月份 and
2)
select a.dep,
su
显示全部