SQL 45道题

数据表

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(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');

科目表 Course

create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

教师表 Teacher

create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

成绩表 SC

create table SC(SId varchar(10),CId varchar(10),score decimal(18,1))
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)

练习题目

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

#查询" 01 "课程的学生
SELECT SId,score FROM SC
WHERE CId=01
#查询" 02 "课程的学生
SELECT SId,score FROM SC
WHERE CId=02
#查询" 01 "课程比" 02 "课程成绩高的学生
SELECT *
FROM 
(SELECT * FROM SC
WHERE CId='01') A
JOIN
(SELECT * FROM SC
WHERE CId='02') B
ON A.SId=B.SId
WHERE A.score>B.score

#简化查询表格
SELECT A.SId,A.score class01,B.score class02
FROM (SELECT * FROM SC
WHERE CId='01') A
JOIN
(SELECT * FROM SC
WHERE CId='02') B
ON A.SId=B.SId
WHERE A.score>B.score

#查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
SELECT * FROM 
`student` 
RIGHT JOIN 
(
SELECT A.SId,A.score class01,B.score class02
FROM (SELECT * FROM SC
WHERE CId='01') A
JOIN
(SELECT * FROM SC
WHERE CId='02') B
ON A.SId=B.SId
WHERE A.score>B.score
) C
ON `student`.`SId`=C.`SId`

1.1 查询同时存在" 01 "课程和" 02 "课程的情况

#查询01课程的情况
SELECT * FROM SC
WHERE CId='01';
#查询02课程的情况
SELECT * FROM SC
WHERE CId='02'
 #查询同时存在" 01 "课程和" 02 "课程的情况
SELECT * FROM 
 (SELECT * FROM SC
WHERE CId='01') a,
(SELECT * FROM SC
WHERE CId='02') b
WHERE a.`SId`=b.SId
#整理
SELECT a.SId,a.CId CId01,a.score score01,b.CId CId02,b.score score02
FROM (SELECT * FROM SC
WHERE CId='01') a,
(SELECT * FROM SC
WHERE CId='02') b
WHERE a.`SId`=b.SId

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

 #存在" 01 "课程情况
 SELECT * FROM `sc`
 WHERE `CId`='01'
 
 #存在" 02 "课程情况
 SELECT * FROM sc
 WHERE CId='02'
 
 #查询存在" 01 "课程但可能不存在" 02 "课程的情况
 SELECT * FROM 
 (SELECT * FROM `sc`
 WHERE `CId`='01') a
LEFT JOIN
( SELECT * FROM sc
 WHERE CId='02') b
 ON a.`SId`=b.SId
 

1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

#查询" 01 "课程的学生编号
SELECT `SId` FROM 
SC
WHERE `CId`=01
#查询" 02 "课程的学生编号
SELECT `SId` FROM 
SC
WHERE `CId`=02

SELECT * FROM sc
WHERE SId NOT IN ( SELECT `SId` FROM 
SC
WHERE `CId`=01)
AND `CId`=02

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

#查询平均成绩大于等于 60 分的同学的学生编号和平均成绩
SELECT SId,AVG(`score`)
FROM `sc`
GROUP BY SId
HAVING AVG(`score`)>=60
#两表联合,查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT Student.`Sname`,a.SId,a.平均成绩
FROM `student`
RIGHT JOIN (SELECT `sc`.SId,AVG(`sc`.`score`) 平均成绩
FROM `sc`
GROUP BY SId
HAVING AVG(`score`)>=60) a
ON `student`.`SId`=a.`SId`

3.查询在 SC 表存在成绩的学生信息

SELECT DISTINCT student.*
FROM student ,sc
WHERE student.SId=sc.SId

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

#查询所有同学的学生编号、选课总数、所有课程的总成绩
SELECT SId,COUNT(CId) 选课总数,SUM(score) 所有课程的总成绩
FROM SC
GROUP BY SId

SELECT Student.`SId`,Student.`Sname`,a.选课总数,a.所有课程的总成绩
FROM Student
LEFT JOIN (SELECT SId,COUNT(CId) 选课总数,SUM(score) 所有课程的总成绩
FROM SC
GROUP BY SId
) a
ON Student.`SId`=a.SId

4.1 查有成绩的学生信息

SELECT DISTINCT `students`.*
FROM `students`
LEFT JOIN  SC
ON  SC.`SId`=Students.`SId`
WHERE score IS NOT NULL

SELECT *
FROM students
WHERE EXISTS(SELECT * FROM sc WHERE students.SId=sc.SId)

5.查询「李」姓老师的数量

SELECT COUNT(*)
FROM Teacher
WHERE Teacher.Tname LIKE '李%'

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

SELECT Student.* FROM
Teacher ,Course,SC,Student
WHERE Teacher.`TId`=Course.`TId`
AND Course.`CId`=SC.`CId`
AND SC.`SId`=Student.`SId`
AND Teacher.`Tname`='张三'

*7.查询没有学全所有课程的同学的信息

SELECT DISTINCT student.*
FROM 
(SELECT student.`SId` ,course.`CId`
FROM `student`,Course) a
LEFT JOIN (SELECT SC.`SId`,sc.`CId`
FROM sc) b
ON a.`SId`=b.`SId`
AND a.`CId`=b.`CId`,student
WHERE b.SId IS NULL
AND   a.SId=student.SId

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

SELECT DISTINCT student.*
FROM sc,student
WHERE cid IN (SELECT SC.CId FROM SC
WHERE SC.`SId`='01')
AND sc.SId=student.`SId`

*9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

SELECT DISTINCT Student.*
FROM (SELECT Student.`SId`,a.cid
FROM Student,(SELECT SC.CId FROM SC
WHERE SC.`SId`='01')a) b LEFT JOIN SC
ON b.`SId`=SC.`SId`
AND b.cid=sc.`CId`,student
WHERE sc.cid IS NULL 
AND b.SId=student.SId

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

SELECT Student.* FROM Student
WHERE Student.`SId` NOT IN (SELECT SC.`SId` FROM 
SC,(SELECT Course.`CId` FROM 
Course,(SELECT Teacher.`TId` FROM  Teacher
WHERE Teacher.`Tname`='张三') a
WHERE Course.`TId`=a.`TId`) b
WHERE SC.`CId`=b.`CId`)

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

SELECT Student.`SId`,Student.`Sname`,AVG(SC.`score`)
FROM Student,SC
WHERE Student.`SId`=sc.`SId`
AND   sc.score<60
GROUP BY sc.SId
HAVING COUNT(*)>=2

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

SELECT Student.*,A.SCORE FROM Student,
(SELECT SC.* FROM SC
WHERE CId='01'
AND score<60) A
WHERE Student.`SId`=A.`SId`
ORDER BY  A.SCORE DESC

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

SELECT SC.`SId`,SC.`CId`,SC.`score`,B.a
FROM SC,(SELECT SC.`SId`,AVG(SC.`score`) a
FROM sc
GROUP BY SC.`SId`) B
WHERE SC.`SId`=B.`SId`
ORDER BY a DESC

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

SELECT sc.CId ,Course.`Cname`,MAX(sc.score)AS 最高分,MIN(sc.score)AS 最低分,AVG(sc.score)AS 平均分,
COUNT(*)AS 选修人数,
SUM(CASE WHEN sc.score>=60 THEN 1 ELSE 0 END )/COUNT(*)AS 及格率,
SUM(CASE WHEN sc.score>=70 AND sc.score<80 THEN 1 ELSE 0 END )/COUNT(*)AS 中等率,
SUM(CASE WHEN sc.score>=80 AND sc.score<90  THEN 1 ELSE 0 END )/COUNT(*)AS 优良率,
SUM(CASE WHEN sc.score>=90 THEN 1 ELSE 0 END )/COUNT(*)AS 优秀率 
FROM sc LEFT JOIN Course
ON sc.`CId`=course.`CId`
GROUP BY sc.CId
ORDER BY COUNT(*)DESC,sc.CId ASC

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

SELECT sc.CId ,@curRank:=@curRank+1 AS rank,sc.score
FROM (SELECT @curRank:=0) AS t ,sc
ORDER BY sc.score DESC

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

SELECT sc.`CId`,sc.`score`,
CASE WHEN @fontscore=score THEN @curRank
WHEN @fontscore:=score THEN @curRank:=@curRank+1
END AS rank
FROM (SELECT @fontscore := NULL,@curRank:=0) t,sc
ORDER BY sc.score DESC

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

SELECT a.*,
@curRank:=@curRank+1 AS rank
FROM (SELECT  sc.sid,SUM(score) AS scos
FROM sc
GROUP BY sc.sid
ORDER BY scos DESC) a,
(SELECT @curRank:=0) t

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

SELECT a.*,
CASE WHEN @fontscore=a.scos THEN @curRank
WHEN @fontscore:=a.scos THEN @curRank:=@curRank+1
END AS rank
FROM (SELECT  sc.sid,SUM(score) AS scos
FROM sc
GROUP BY sc.sid
ORDER BY scos DESC) a,
(SELECT @sco=NULL ,@curRank:=0) t

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

SELECT Course.`Cname`,a.*
FROM (SELECT  SC.CId,
CONCAT(SUM(CASE WHEN score>=85 THEN 1 ELSE 0 END)/COUNT(*)*100,"%") AS '[85-100]',
CONCAT(SUM(CASE WHEN sc.score>=70 AND sc.score<85 THEN 1 ELSE 0 END)/COUNT(*)*100,"%") AS '[70-85]',
CONCAT(SUM(CASE WHEN score>=60 AND sc.score<70 THEN 1 ELSE 0 END)/COUNT(*)*100,"%") AS '[60-70]',
CONCAT(SUM(CASE WHEN score<60 THEN 1 ELSE 0 END)/COUNT(*)*100,"%") AS '[0-60]' 
FROM sc
GROUP BY sc.CId) a LEFT JOIN Course
ON a.CId=Course.`CId`

*18.查询各科成绩前三名的记录

SELECT * FROM sc
WHERE (SELECT COUNT(*) FROM sc a 
WHERE sc.`CId`=a.`CId` AND sc.score

19.查询每门课程被选修的学生数

select sc.CId,count(*)
from sc 
GROUP BY sc.CId

*20.查询出只选修两门课程的学生学号和姓名

SELECT SC.SId,Student.`Sname`
FROM SC LEFT JOIN Student
ON SC.`SId`=Student.`SId`
GROUP BY SC.`SId`
HAVING COUNT(*)=2

21.查询男生、女生人数

SELECT student.Ssex ,COUNT(*) AS 人数
FROM student 
GROUP BY student.Ssex

22.查询名字中含有「风」字的学生信息

SELECT student.* FROM student
WHERE Student.`Sname` LIKE "%风%"

*23.查询同名同性学生名单,并统计同名人数

SELECT *
FROM student LEFT JOIN (SELECT Sname,Ssex,COUNT(*)同名人数 FROM Student GROUP BY Sname,Ssex) AS t1
ON student.Sname =t1.Sname AND student.Ssex=t1.Ssex
WHERE t1.同名人数>1

*24.查询 1990 年出生的学生名单

SELECT * FROM student 
WHERE YEAR(student.`Sage`)=1990

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

SELECT SC.`CId`,AVG(SC.`score`) AS 平均成绩 FROM sc
GROUP BY SC.`CId` 
ORDER BY 平均成绩 DESC,SC.`CId` 

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

SELECT student.SId,student.Sname,t1.平均成绩 
FROM student,(SELECT  SC.SId,AVG(SC.`score`) 平均成绩 FROM sc
GROUP BY SC.`SId`
HAVING 平均成绩>=85) t1
WHERE student.`SId`=t1.`SId`

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

SELECT Student.`Sname`,t.`score`
FROM Student,(SELECT  SC.`SId`,SC.`score` FROM SC
WHERE SC.`CId` IN (SELECT Course.`CId` FROM Course
WHERE Course.`Cname`='数学')
AND SC.`score`<60)t
WHERE Student.`SId`=t.`SId`

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

SELECT student.sid,SC.`CId`,sc.`score`
FROM student LEFT JOIN SC
ON student.`SId`=sc.`SId`

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

SELECT Student.`Sname`,Course.`Cname`,SC.`score`
FROM Student,SC,Course
WHERE Student.`SId`=SC.`SId`
AND SC.`CId`=course.`CId`
AND SC.`score`>=70

30.查询不及格的课程

SELECT DISTINCT SC.`CId`
FROM SC
WHERE SC.`score`<60

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

SELECT Student.`SId`,Student.`Sname`
FROM Student,SC
WHERE Student.`SId`=sc.`SId`
AND SC.`CId`='01'
AND SC.`score`>80

32.求每门课程的学生人数

SELECT SC.`CId`,COUNT(*) 学生人数
FROM SC
GROUP BY SC.`CId`

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

select student.*,sc.score
from student ,course ,teacher ,sc
where course.CId=sc.CId
and course.TId=teacher.TId
and teacher.Tname='张三'
and student.SId =sc.SId
LIMIT 1

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

SELECT Student.*,c.score
FROM Student,(SELECT a.SId,a.score, 
CASE WHEN @fontage=a.score THEN @rank 
WHEN @fontage:=a.score THEN @rank:=@rank+1 END  AS rank
FROM 
(SELECT Sc.SId,SC.`score`  
FROM sc,course ,teacher 
WHERE sc.CId=course.CId
AND course.TId=teacher.TId
AND teacher.Tname='张三'
ORDER BY sc.score DESC) a,(SELECT @fontage:=NULL,@rank:=0) b) c
WHERE Student.`SId`=c.SId
AND c.rank=1

*35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

SELECT DISTINCT a.sid,a.cid,a.score FROM sc a LEFT JOIN sc b
ON a.SId=b.SId
WHERE a.cid!=b.cid
AND a.score=b.score

*36.查询每门功成绩最好的前两名

SELECT * FROM sc a
WHERE
(SELECT COUNT(*) FROM sc b
WHERE a.CId=b.CId
AND b.score>a.score)<2
ORDER BY a.cid

37.统计每门课程的学生选修人数(超过 5 人的课程才统计)

SELECT SC.`CId`,COUNT(*)
FROM sc
GROUP BY SC.`CId`
HAVING COUNT(*)>5

*38.检索至少选修两门课程的学生学号

SELECT sc.`SId`,COUNT(*) FROM sc
GROUP BY sc.`SId`
HAVING COUNT(*)>=2

39.查询选修了全部课程的学生信息

select student.*
from sc ,student 
where sc.SId=student.SId
GROUP BY sc.SId
HAVING count(*) = (select DISTINCT count(*) from course )

*40.查询各学生的年龄,只按年份来算

select *, year(now())-year(sage) as age from student;

*41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

SELECT *,TIMESTAMPDIFF(YEAR,sage,NOW())AS 学生年龄 
FROM student

*42.查询本周过生日的学生

SELECT *, SUBSTR(YEARWEEK(student.Sage),5,2) AS birth_week, 
SUBSTR(YEARWEEK(CURDATE()),5,2) AS now_week 
FROM student 
HAVING SUBSTR(YEARWEEK(student.Sage),5,2)=SUBSTR(YEARWEEK(CURDATE()),5,2);

*43.查询下周过生日的学生

select     *,    substr(YEARWEEK(student.Sage),5,2) as birth_week,    substr(YEARWEEK(CURDATE()),5,2) as now_week from student having substr(YEARWEEK(student.Sage),5,2)=        substr(YEARWEEK(CURDATE()),5,2)+1;

*44.查询本月过生日的学生

select *, month(sage) as birth_month, month(now()) as now_month from student where month(sage)=month(now());

*45.查询下月过生日的学生

select *, month(sage) as birth_month, month(now()) as now_month from student where month(sage)=month(now())+1

你可能感兴趣的:(SQL 45道题)