今天参加完面试,遇到了一些sql题,现把这些题目一一列出,但愿有人看到,若是你在面试过程当中碰巧遇到此类问题,那么就直接秒杀吧,哈哈。面试
首先简历一个stuscore表。其中name为姓名,subject为科目,score为成绩。sql
create table stuscore( name varchar(20), subject varchar(20), score int )
一、查找出有两门成绩小于60分的学生的全部成绩的平均成绩函数
思路:首先求出有两门成绩小于60分的学生姓名,而后在求其平均成绩code
select name,avg(score) from stuscore where name in (select name from stuscore where score < 60 group by name having count(*) >= 2) group by name
二、查询每一个学生最大分数的科目及分数数学
思路:首先获取每一个学生最大的分数值,而后在链接查询table
select a.name,a.subject,a.score from stuscore a, (select name max(score) score from stuscore group by name) b where a.name = b.name and a.score = b.score
三、-- 行列转换class
select name as '姓名', max(case subject when '语文' then score else 0 end) as '语文', max(case subject when '数学' then score else 0 end) as '数学', max(case subject when '英语' then score else 0 end) as '英语' from stuscore group by name
说明:这里加上max函数的缘由是group by name。具体能够看下group by的用法以及注意事项select