面试啦 - 提供专业面试问题及答案、面试技巧、助您成功面试!
您的当前位置:首页 > 面试题库 > IT面试题 > Database面试题 > 正文

Sql面试题

1. 有如下表GameResult:
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
Select 日期,
sum(Case score
When ‘胜’ then 1
Else 0
End) as 胜,
sum(case score
When ‘负’ then 1
Else 0
end) as 负
From GameResult
Group by 日期
2. 请取出tb_send表中日期(SendTime字段)为当天的所有记录。(SendTime字段为datetime型,包含日期与时间)
select * from tb_send where datediff(day,SendTime,getdate())=0;
3. 请用一个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
部门 一月份 二月份 三月份
01 10 null null
02 10 8 null
03 5 null 8
04 null 9 null
Select B.depId,
sum(
case A.mon
when ‘一月’ then A.yeji
end)as ‘一月’,
sum(
case A.mon
when ‘二月’ then A.yeji
end)as ‘二月’,
sum(
case A.mon
when ‘三月’ then A.yeji
end)as ‘三月’
from table1 as A right join table2 as B on A.depId=B.depId
group by B.depId

4. 一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
select count(*) from TableName group by ID

5. 用户表userinfo,并有2个字段(id,username),其中id是自增长的,请用sql语句取出第31-40条记录。
(注意:id也许不是连续的)
select *from
(
select row_number() over(order by id) as col,* from username
) as A
where A.col>= 31 and A.col<=40
6. 学生表student,有如下字段(主键id,姓名realname,成绩result,班级class),请用sql语句计算各班的及格率和优良率。
(及格率=各班60分以上的人数/各班总人数)
(优良率=各班80分以上的人数/各班总人数)
select class,
sum(
case
when result>=60 then 1
end
) as ‘jige’,
sum(
case
when result>=80 then 1
end
) as ‘youxiu’,
sum(
case
when result>=0 and result<=100 or result = null then 1
end
) as ‘counts’
from student
group by class
词条:database面试题 数据库面试题
上一篇:存储过程的优点有哪些 下一篇:什么是Oracle的后台进程background processes?都有哪些后台进程?
与该文相关的文章

温馨提示:如果您对面试网有任何建议,请通过网站联系邮箱向我们反馈,感谢各位的建议与支持!