50道SQL练习题及答案与详细分析

50道SQL练习题及答案与详细分析

数据表介绍

–1.学生表
Student(SId,Sname,Sage,Ssex)
–SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别

–2.课程表
Course(CId,Cname,TId)
–CId 课程编号,Cname 课程名称,TId 教师编号

–3.教师表
Teacher(TId,Tname)
–TId 教师编号,Tname 教师姓名

–4.成绩表
SC(SId,CId,score)
–SId 学生编号,CId 课程编号,score 分数

学生表 Student

CREATE TABLE Student
(
	SId varchar(20) NOT NULL,-- 学号
	Sname  varchar(20) NOT NULL,	-- 姓名
	Sage  datetime not null,	--出生日期
	Ssex   char(2) not null CHECK(Ssex='男' OR Ssex='女'),-- 性别
	
	
)
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');

科目表 Course

-- 科目
create table Course
(
	CId varchar(20) not null,
	Cname varchar(20) not null ,	
	TId INT not null 

)
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

教师表 Teacher

CREATE TABLE Teacher
(
	TId varchar(20) not null,
	Tname varchar(20) not null,
	
)
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

成绩表 SC

CREATE TABLE SC
(
	SId  varchar(20) NOT NULL,
	CId  varchar(20) NOT NULL,
	score int null check(score<=100 and score>=0) ,
	
)
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
SELECT * FROM Student
SELECT * FROM SC
SELECT * FROM Course
SELECT * FROM Teacher

在这里插入图片描述

练习题目

–1 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数

SELECT * from student RIGHT JOIN  (SELECT 
a.SId,a.score class1,b.score class2 FROM (SELECT * 
from sc WHERE sc.CId='01')as a,(SELECT * from sc 
WHERE sc.CId='02')as b WHERE a.SId=b.SId AND 
a.score>b.score)r ON student.SId=r.SId

–2.查询同时存在" 01 “课程和” 02 "课程的情况

SELECT a.* , b.score 课程01的分数 ,c.score 课程02的分数 
FROM Student a , SC b , SC c where a.SID = b.SID and 
a.SID = c.SID and b.CID = '01' and c.CID = '02' and 
b.score < c.score

–3 查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )

SELECT a.* , b.score 课程01的分数 ,c.score 课程02的分数 
FROM Student a left join SC b on a.SID = b.SID and b.CID = 
'01' left join SC c on a.SID = c.SID and c.CID = '02' where 
isnull(b.score,0) < c.score

–4 查询不存在" 01 “课程但存在” 02 "课程的情况

SELECT a.* , b.score 课程01的分数 ,c.score 课程02的分数 
FROM Student a left join SC b on a.SID = b.SID and b.CID = 
'02' left join SC c on a.SID = c.SID and c.CID = '01' where 
isnull(b.score,0) > c.score

–5 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

--SELECT student.sid,student.sname,AVG(sc.score) FROM 
student,sc WHERE student.sid=sc.sid GROUP BY 
student.sid HAVING AVG(sc.score)>=60 
SELECT a.SID , a.Sname , cast(avg(b.score) as 
decimal(18,2)) avg_score from Student a , sc b where 
a.SID = b.SID group by a.SID , a.Sname having 
cast(avg(b.score) as decimal(18,2)) >= 60 order by 
a.SID

–6 查询在 SC 表存在成绩的学生信息

SELECT a.sid, a.sname,a.sage,a.Ssex FROM student a 
JOIN (SELECT DISTINCT SId FROM SC WHERE score<>0)b ON 
a.sid = b.sid

–7 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )

select a.SID 学生编号 , a.Sname 学生姓名 , count(b.CID) 
选课总数, sum(score) 所有课程的总成绩 from Student a , SC 
b where a.SID = b.SID group by a.SID,a.Sname order by 
a.SID

–8 查有成绩的学生信息

select * from student where exists (select sc.sid 
from sc where student.sid = sc.sid);

–9 查询「李」姓老师的数量

SELECT count(Tname) FROM teacher where Tname LIKE '李%';

–10 查询学过「张三」老师授课的同学的信息

select m.* from Student m where SID  in (select 
distinct SC.SID from SC , Course , Teacher where 
SC.CID = Course.CID and Course.TID = Teacher.TID and
 Teacher.Tname = '张三') order by m.SID

–11 查询没有学全所有课程的同学的信息

select Student.* from Student , SC where Student.SID
 = SC.SID group by Student.SID , Student.Sname , 
 Student.Sage , Student.Ssex having count(CID) < 
 (select count(CID) from Course)

–12 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

select distinct Student.* from Student , SC where 
Student.SID = SC.SID and SC.CID in (select CID from 
SC where SID = '01') and Student.SID <> '01'

–13 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

select Student.* from Student where SID in (select 
distinct SC.SID from SC where SID <> '01' and SC.CID 
in (select distinct CID from SC where SID = '01') 
group by SC.SID having count(1) = (select count(1) 
from SC where SID='01'))select Student.* from Student 
where SID in
(select distinct SC.SID from SC where SID <> '01' and 
SC.CID in (select distinct CID from SC where SID = 
'01') group by SC.SID having count(1) = (select 
count(1) from SC where SID='01'))

–14 查询没学过"张三"老师讲授的任一门课程的学生姓名

select student.* from student where student.SID not
 in (select distinct sc.SID from sc , course , 
 teacher where sc.CID = course.CID and course.TID = 
 teacher.TID and teacher.tname = '张三') order by 
 student.SID

–15 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select student.SID , student.sname , cast(avg(score) 
as decimal(18,2)) avg_score from student , sc where 
student.SID = SC.SID and student.SID in (select SID 
from SC where score < 60 group by SID having count(1)
 >= 2) group by student.SID , student.sname

–16 检索" 01 "课程分数小于 60,按分数降序排列的学生信息

select student.* , sc.CID , sc.score from student , 
sc where student.SID = SC.SID and sc.score < 60 and 
sc.CID = '01' order by sc.score desc

–17 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select a.SID 学生编号 , a.Sname 学生姓名 ,
max(case c.Cname when '语文' then b.score else null 
end) 语文 ,
max(case c.Cname when '数学' then b.score else null 
end) 数学 ,
max(case c.Cname when '英语' then b.score else null 
end) 英语 ,
cast(avg(b.score) as decimal(18,2)) 平均分 from 
Student a left join SC b on a.SID = b.SID left join 
Course c on b.CID = c.CID group by a.SID , a.Sname 
order by 平均分 desc

–18 查询各科成绩最高分、最低分和平均分:
–以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
–及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
–要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select m.CID 课程编号 , m.Cname 课程名称 ,
(select max(score) from SC where CID = m.CID) 最高分 ,
(select min(score) from SC where CID = m.CID) 最低分 ,
(select cast(avg(score) as decimal(18,2)) from SC where CID = m.CID) 平均分 ,
cast((select count(1) from SC where CID = m.CID and score >= 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 及格率,
cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,
cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,
cast((select count(1) from SC where CID = m.CID and score >= 90)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优秀率
from Course m order by m.CID

–19 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺

select t.* , px = (select count(1) from SC where CID
 = t.CID and score > t.score) + 1 from sc t order by 
 t.cid , px

–20 按各科成绩进行排序,并显示排名, Score 重复时合并名次

select t.* , px = (select count(distinct score) from 
SC where CID = t.CID and score >= t.score) from sc t 
order by t.cid , px

–21 查询学生的总成绩,并进行排名,总分重复时保留名次空缺

select t.* , px = rank() over(order by 总成绩 desc) 
from (select m.SID 学生编号 ,m.Sname 学生姓名 
,isnull(sum(score),0) 总成绩 from Student m left join 
SC n on m.SID = n.SID group by m.SID , m.Sname) t 
order by px

–22 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺

select t.* , px = DENSE_RANK() over(order by 总成绩 
desc) from (select m.SID 学生编号 ,m.Sname 学生姓名 
,isnull(sum(score),0) 总成绩 from Student m left join
 SC n on m.SID = n.SID group by m.SID , m.Sname) t
  order by px

–23 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比

select course.cname, course.cid,
sum(case when sc.score<=100 and sc.score>85 then 1 else 0 end) as "[100-85]",
sum(case when sc.score<=85 and sc.score>70 then 1 else 0 end) as "[85-70]",
sum(case when sc.score<=70 and sc.score>60 then 1 else 0 end) as "[70-60]",
sum(case when sc.score<=60 and sc.score>0 then 1 else 0 end) as "[60-0]"
from sc left join course
on sc.cid = course.cid
group by sc.cid,course.cname,course.cid;

–24 查询各科成绩前三名的记录

select m.* , n.CID , n.score from Student m, SC n 
where m.SID = n.SID and n.score in (select top 3 
score from sc where CID = n.CID order by score desc) 
order by n.CID , n.score desc

–25 查询每门课程被选修的学生数

select Cid , count(SID) 学生数 from sc group by CID

–26 查询出只选修两门课程的学生学号和姓名

select Student.SID , Student.Sname from Student , SC 
where Student.SID = SC.SID group by Student.SID , 
Student.Sname having count(SC.CID) = 2 order by 
Student.SID

–27 查询男生、女生人数

--1.select count(Ssex) as 男生人数 from Student where Ssex = N'男'
--2.select count(Ssex) as 女生人数 from Student where Ssex = N'女'
--3.select sum(case when Ssex = N'男' then 1 else 0 end) 男生人数 ,sum(case when Ssex = N'女' then 1 else 0 end) 女生人数 from student
select case when Ssex = N'男' then N'男生人数' else N'女生人数' end 男女情况 , count(1) 人数 from student group by case when Ssex = N'男' then N'男生人数' else N'女生人数' end

–28 查询名字中含有「风」字的学生信息

--1.select * from student where sname like N'%风%'
select * from student where charindex(N'风' , sname) > 0

–29 查询同名同性学生名单,并统计同名人数

select Sname 学生姓名 , count(*) 人数 from Student group by Sname having count(*) > 1

–30 查询 1990 年出生的学生名单

--1.select * from Student where year(sage) = 1990
--2.select * from Student where datediff(yy,sage,'1990-01-01') = 0
--3.select * from Student where datepart(yy,sage) = 1990
select * from Student where convert(varchar(4),sage,120) = '1990'

–31 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select m.CID , m.Cname , cast(avg(n.score) as 
decimal(18,2)) avg_score from Course m, SC n where 
m.CID = n.CID group by m.CID , m.Cname order by 
avg_score desc, m.CID asc

–32 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩

select a.SID , a.Sname , cast(avg(b.score) as 
decimal(18,2)) avg_score from Student a , sc b where 
a.SID = b.SID group by a.SID , a.Sname having 
cast(avg(b.score) as decimal(18,2)) >= 85 order by
 a.SID

–33 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数

select sname , score from Student , SC , Course where 
SC.SID = Student.SID and SC.CID = Course.CID and 
Course.Cname = N'数学' and score < 60

–34 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

select Student.* , Course.Cname , SC.CID , SC.score 
from Student, SC , Course where Student.SID = SC.SID 
and SC.CID = Course.CID order by Student.SID , SC.CID

–35 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select Student.* , Course.Cname , SC.CID , SC.score 
from Student, SC , Course where Student.SID = SC.SID 
and SC.CID = Course.CID and SC.score >= 70 order by 
Student.SID , SC.CID

–36 查询不及格的课程

select Student.* , Course.Cname , SC.CID , SC.score 
from Student, SC , Course where Student.SID = SC.SID 
and SC.CID = Course.CID and SC.score < 60 order by 
Student.SID , SC.CID

–37 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

select Student.* , Course.Cname , SC.CID , SC.score 
from Student, SC , Course where Student.SID = SC.SID 
and SC.CID = Course.CID and SC.CID = '01' and 
SC.score >= 80 order by Student.SID , SC.CID

–38 求每门课程的学生人数

select Course.CID , Course.Cname , count(*) 学生人数 
from Course , SC where Course.CID = SC.CID group by 
Course.CID , Course.Cname order by Course.CID , 
Course.Cname

–39 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select top 1 Student.* , Course.Cname , SC.CID , 
SC.score from Student, SC , Course , Teacher where 
Student.SID = SC.SID and SC.CID = Course.CID and 
Course.TID = Teacher.TID and Teacher.Tname = N'张三'
 order by SC.score desc

–40 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select student.*, sc.score, sc.cid from student, teacher, course,sc 
where teacher.tid = course.tid
and sc.sid = student.sid
and sc.cid = course.cid
and teacher.tname = '张三'
and sc.score = (
    select Max(sc.score) 
    from sc,student, teacher, course
    where teacher.tid = course.tid
    and sc.sid = student.sid
    and sc.cid = course.cid
    and teacher.tname = '张三'
);

–41 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

select m.* from SC m ,(select CID , score from SC 
group by CID , score having count(1) > 1) n where 
m.CID= n.CID and m.score = n.score order by m.CID ,
 m.score , m.SID

–42 查询每门功成绩最好的前两名

select t.* from sc t where score in (select top 2 
score from sc where CID = T.CID order by score desc) 
order by t.CID , t.score desc

–43 统计每门课程的学生选修人数(超过 5 人的课程才统计)。

select Course.CID , Course.Cname , count(*) 学生人数 
from Course , SC where Course.CID = SC.CID group by 
Course.CID , Course.Cname having count(*) >= 5 order 
by 学生人数 desc , Course.CID

–44 检索至少选修两门课程的学生学号

select student.SID , student.Sname from student , SC 
where student.SID = SC.SID group by student.SID , 
student.Sname having count(1) >= 2 order by 
student.SID

–45 查询选修了全部课程的学生信息

select student.* from student where SID in(select SID 
from sc group by SID having count(1) = (select 
count(1) from course))

–46 查询各学生的年龄,只按年份来算

select * , datediff(yy , sage , getdate()) 年龄 from student

–47 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

select * , case when right(convert(varchar(10),getdate(),120),5) < 
right(convert(varchar(10),sage,120),5) then 
datediff(yy , sage , getdate()) - 1 else datediff(yy , sage , getdate()) end 年龄 from student

–48 查询本周过生日的学生

select * from student where datediff(week,datename(yy,getdate()) + 
right(convert(varchar(10),sage,120),6),getdate()) = 0

–49 查询下周过生日的学生

select * from student where datediff(week,datename(yy,getdate()) + 
right(convert(varchar(10),sage,120),6),getdate()) = -1

–50 查询本月过生日的学生

select * from student where datediff(mm,datename(yy,getdate()) + 
right(convert(varchar(10),sage,120),6),getdate()) = 0

–51 查询下月过生日的学生

select * from student where datediff(mm,datename(yy,getdate()) + 
right(convert(varchar(10),sage,120),6),getdate()) = -1
发布了115 篇原创文章 · 获赞 198 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/chonbi/article/details/104776615