SQL 挑战50道题

开发工具与关键技术:SQL Server 2014
作者:华境聪
撰写时间: 2019年04月13日
这个是在学习了Oracle数据库之后,对SQL数据库的一些查询题目的运用,个人感觉,关键的函数用法都是大同小异。例如:SQL中的Substring相当于Oracle的Substr截取、SQL中的Convert(varchar(100),getdate)相当于Oracle的To_char(sysdate,’ yyyy-mm-dd hh24:mi:ss’) 日期转换字符串。

--1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.StuId,c.StuName
from (select * from tblScore where tblScore.CourseId = '001') a
join (select * from tblScore where tblScore.CourseId = '002') b
on a.StuId = b.StuId
join (select * from tblStudent) c on c.StuId = a.StuId
where a.Score>b.Score
--2、查询平均成绩大于60分的同学的学号和平均成绩;
select Round(avg(Score),1),StuId
from tblScore
group by StuID
--3、查询所有同学的学号、姓名、选课数、总成绩; 
select b.StuId,b.StuName,Count(CourseId) "选课数",sum(Score) "总成绩"
from tblScore a right join tblStudent b on a.StuId = b.StuId 
group by b.StuId,b.StuName
--4、查询姓“李”的老师的个数; 
select Count(TeaId)
from tblTeacher
where TeaName like '李%'
--5、查询没学过“叶平”老师课的同学的学号、姓名; 
select *
from tblStudent
where tblStudent.StuId not in (select tblStudent.StuId
from tblTeacher join tblCourse on tblCourse.TeaId = tblTeacher.TeaId
join tblScore on tblScore.CourseId = tblCourse.CourseId
join tblStudent on tblStudent.StuId = tblScore.StuId
where tblTeacher.TeaName = '叶平'
group by tblStudent.StuId,tblStudent.StuName,tblTeacher.TeaName)
--6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; 
select tblStudent.StuId,count(*),tblStudent.StuName
from tblScore
join tblStudent on tblScore.StuId = tblStudent.StuId
where CourseId = '001' or CourseId = '002'
group by tblStudent.StuId,tblStudent.StuName
having count(*) = 2
--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; 
select tblStudent.StuId,count(*),tblStudent.StuName
from tblTeacher join tblCourse on tblCourse.TeaId = tblTeacher.TeaId
join tblScore on tblScore.CourseId = tblCourse.CourseId
join tblStudent on tblStudent.StuId = tblScore.StuId
where tblTeacher.TeaName = '叶平'
group by tblStudent.StuId,tblStudent.StuName
having count(*) > 1
--8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; 
select c.StuId,c.StuName
from (select * from tblScore where CourseId = '002') a
join (select * from tblScore where CourseId = '001') b
on a.StuId = b.StuId join
(select * from tblStudent)c on b.StuId = c.StuId
where a.Score < b.Score
--9、查询所有课程成绩小于60分的同学的学号、姓名;
select a.StuId,b.StuName,sum(a.Score)
from tblScore a
join tblStudent b on a.StuId = b.StuId
group by a.StuId,b.StuName
having sum(a.Score) < 60
--10、查询没有学全所有课的同学的学号、姓名;
select b.StuId,b.StuName,count(*)
from tblScore a
join tblStudent b on a.StuId = b.StuId
group by b.StuId,b.StuName
having count(*) not in (select count(*) from tblCourse)
--11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
select b.StuId,b.StuName
from tblScore a
join tblStudent b on a.StuId = b.StuId
where a.CourseId in (select CourseId from tblScore where stuid = 1001)
group by b.StuId,b.StuName
--12、查询至少学过学号为“1001”同学所有课程的其他同学学号和姓名;
select b.StuId,b.StuName
from tblScore a
join tblStudent b on a.StuId = b.StuId
where a.CourseId in (select CourseId from tblScore where stuid = 1001) and a.stuid != 1001
group by b.StuId,b.StuName
having count(*) >= (select count(*) from tblScore where stuid = 1001)
--13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
select round(avg(Score),1),CourseId
from tblScore
where CourseId = (select c.CourseId from tblCourse c join tblTeacher d on c.CourseId = d.TeaId where d.TeaName = '叶平')
group by CourseId
--14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
select b.StuId,b.StuName
from tblScore a
join tblStudent b on a.StuId = b.StuId
where   a.stuid != 1002
group by b.StuId,b.StuName
having count(*) = (select count(*) from tblScore where stuid = 1002)
--15、删除学习“叶平”老师课的SC表记录;
delete tblScore
where CourseId = (select c.CourseId from tblCourse c join tblTeacher d on c.CourseId = d.TeaId where d.TeaName = '叶平')
--16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、'002'号课的平均成绩; 
insert into tblScore
select StuId,CourseId,avg(Score)
from tblScore
where StuId not in (select StuId
from tblScore
where CourseId = '003') and CourseId = '004'
group by StuId,CourseId;
--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分 
select a.StuId "学生ID",
sum(case 
when b.CourseName = '数据库' then a.Score
else 0
end) "数据库",
sum(case 
when b.CourseName = '企业管理' then a.Score
else 0
end) "企业管理",
sum(case 
when b.CourseName = '英语' then a.Score
else 0
end) "英语",
count(*) "有效课程数",
avg(a.Score) "有效平均分"
from tblScore a
join tblCourse b on a.CourseId = b.CourseId 
where b.CourseName in ('数据库','企业管理','英语')
group by a.StuId
--18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 
select b.CourseId "课程ID",max(a.Score) "最高分" ,min(a.Score) "最低分"
from tblScore a
join tblCourse b on a.CourseId = b.CourseId 
group by b.CourseId
--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序
select avg(score),sum(case when score >=60 then 1 else 0 end)/count(*) as '及格率'
from tblScore 
group by CourseId
order by avg(score) ,及格率 desc
--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)
select  avg(score),
sum(case when score >=60 and CourseId = '001' then 1 else 0 end)/count(*) as '企业管理及格率',
sum(case when score >=60 and CourseId = '002' then 1 else 0 end)/count(*) as '马克思及格率',
sum(case when score >=60 and CourseId = '003' then 1 else 0 end)/count(*) as 'OO&UML及格率',
sum(case when score >=60 and CourseId = '004' then 1 else 0 end)/count(*) as '数据库及格率'
from tblScore 
where CourseId in ('001','002','003','004')
--21、查询不同老师所教不同课程平均分从高到低显示 
select c.TeaName,b.CourseId,avg(a.Score)
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
join tblTeacher c on b.TeaId = c.TeaId
group by c.TeaName,b.CourseId
order by avg(a.Score) desc
--22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004) 格式:[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩
select * from 
(select a.StuId,c.StuName,ROW_NUMBER()over(order by sum(Score) desc) as d,
sum(case when  b.CourseId = '001' then Score else 0 end) as 企业管理,
sum(case when  b.CourseId = '002' then Score else 0 end) as 马克思,
sum(case when  b.CourseId = '003' then Score else 0 end) as UML,
sum(case when  b.CourseId = '004' then Score else 0 end) as 数据库,avg(Score) as 平均成绩,
sum(Score) as 三科成绩
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
join tblStudent c on a.StuId = c.StuId
where b.CourseId in ('001','002','003','004')
group by a.StuId,c.StuName)ef
where ef.d <= 6 and ef.d >= 3
--23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] 
select a.CourseId,
sum(case when a.Score between 85 and 100 then 1 else 0 end) "100-85",
sum(case when a.Score between 70 and 85 then 1 else 0 end) "85-70",
sum(case when a.Score between 60 and 70 then 1 else 0 end) "70-60",
sum(case when a.Score between 0 and 60 then 1 else 0 end) "<60"
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
join tblStudent d on a.StuId = d.StuId
group by a.CourseId
--24、查询学生平均成绩及其名次 
select avg(Score),StuId
from tblScore
group by StuId
order by avg(Score) desc
----25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
select * from (
select CourseId,StuId,Score,row_number()over(partition by CourseId order by Score desc) "排名情况"
from tblScore
) a
where a.排名情况 < 4
--26、查询每门课程被选修的学生数 
select count(*),CourseId 
from tblScore
group by CourseId
--27、查询出只选修了一门课程的全部学生的学号和姓名 
select count(*),a.StuId,b.StuName
from tblScore a
join tblStudent b on a.StuId = b.StuId
group by a.StuId,b.StuName
having count(*) = 1
--28、查询男生、女生人数
select count(*) ,StuSex
from tblStudent
group by StuSex
--29、查询姓“张”的学生名单
select *
from tblStudent
where StuName like '张%'
--30、查询同名同性学生名单,并统计同名人数
select StuName,count(*)
from tblStudent
group by StuName
having count(*)>1
--31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime) 
select *
from tblStudent
where SUBSTRING(convert(varchar(100),Sage),7,4) = 1981
--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 
select avg(score),CourseId
from tblScore
group by CourseId
order by avg(score),CourseId desc
--33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select avg(Score),a.StuId,c.StuName
from tblScore a
right join tblStudent c on a.StuId = c.StuId
 group by a.StuId,c.StuName
 having avg(Score) > 85
--34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数 
 select c.StuName,a.Score
 from tblScore a
 join tblCourse b on a.CourseId = b.CourseId
 join tblStudent c on a.StuId = c.StuId
 where CourseName = '数据库' and Score < 60
--35、查询所有学生的选课情况; 
select a.CourseId,b.StuName
from tblScore a 
right join tblStudent b on a.StuId  = b.StuId 
--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 
select c.StuName,b.CourseName,a.Score
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
join tblStudent c on a.StuId = c.StuId
where a.StuId in (select a.StuId
from tblScore a
join (select a.StuId,count(*) as d
from tblScore a
group by a.StuId)f on a.StuId = f.StuId
where a.Score  > 70  
group by a.StuId,f.d
having f.d = count(*))
--37、查询不及格的课程,并按课程号从大到小排列 
select a.CourseId
from tblScore a
where a.CourseId in (select a.CourseId
from tblScore a
join (select a.CourseId,count(*) as d
from tblScore a
group by a.CourseId)f on a.CourseId = f.CourseId
where a.Score  < 60  
group by a.CourseId,f.d
having f.d = count(*))
group by a.CourseId
order by a.CourseId asc
--38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select a.StuId,c.StuName
from tblScore a
 join tblStudent c on a.StuId = c.StuId
where CourseId = '003'and Score > 80
--39、求选了课程的学生人数 
select count(*)
from (select StuId
from tblScore
group by StuId) b

--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 
select a.StuName,b.Score from tblStudent a 
join tblScore b on b.StuId = a.StuId
join (
select max(d.Score) as sc
from (select a.StuId,a.Score
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
join tblTeacher d on b.TeaId = d.TeaId
where TeaName = '叶平'
group by a.StuId,a.Score)d)c on b.Score = c.sc
--41、查询各个课程及相应的选修人数 
select b.CourseId,count(*)
from tblScore a
right join  tblCourse b on a.CourseId = b.CourseId
group by b.CourseId
--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩 
select a.*
from tblScore a join tblScore b on a.Score = b.Score
where  a.CourseId != b.CourseId and a.StuId = b.StuId
--43、查询每门功成绩最好的前两名 
select * from
(select StuId,CourseId,Score,row_number()over(partition by CourseId order by Score desc) sdf
from tblScore) a
where a.sdf < 3
--44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  
select CourseId,count(*)
from tblScore
group by CourseId
order by count(*) desc 
--45、检索至少选修两门课程的学生学号 
select StuId
from tblScore
group by StuId
having count(*) >= 2
--46、查询全部学生都选修的课程的课程号和课程名 
select a.CourseId,count(*) as d,b.CourseName
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
group by a.CourseId,b.CourseName
having count(*) = (select count(*) from
(select StuId
from tblScore
group by StuId)m)
--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名 
select StuName
from tblStudent
where StuName not in (select distinct(c.StuName)
from tblScore a
join tblCourse b on a.CourseId = b.CourseId
join tblStudent c on a.StuId = c.StuId
join tblTeacher d on b.TeaId = d.TeaId
where TeaName = '叶平')
--48、查询两门以上不及格课程的同学的学号及其平均成绩 
select stuid,avg(score)
from tblScore
where score < 60
group by stuid
having count(*) > 2 
--49、检索“004”课程分数小于60,按分数降序排列的同学学号 (ok)
select StuId
from tblScore
where CourseId = 004 and Score < 60
order by StuId desc
--50、删除“002”同学的“001”课程的成绩 
delete  tblScore
where stuid = 002 and CourseId = 001
··

猜你喜欢

转载自blog.csdn.net/qq_41979469/article/details/89282467