常见SQL笔试题分析和总结.pdfVIP

  • 2
  • 0
  • 约1.07万字
  • 约 14页
  • 2021-12-09 发布于上海
  • 举报
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,tabl

文档评论(0)

1亿VIP精品文档

相关文档