50道SQL练习题
来自https://www.jianshu.com/p/476b52ee4f1b
sql语句的执行顺序
(9)Select
(10)distinct 字段名1,字段名2,
(7)[fun(字段名)]
(1)from 表1
(3)
(2)on
(4)where
(5)group by <字段>
(6)with
(8)having
(11)order by <排序字段>
(12)limit <起始偏移量,行数>
学生表 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-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(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 "课程成绩高的学生的信息及课程分数
SELECT
*
FROM
Student s
WHERE
s.SId IN (
SELECT
sc1.SId
FROM
SC sc1
JOIN SC sc2 ON sc1.SId = sc2.SId
AND sc1.CId = '01'
AND sc2.CId = '02'
AND sc1.score > sc2.score
);
2.查询同时存在" 01 "课程和" 02 "课程的学生情况
SELECT
*
FROM
Student s
WHERE
s.SId IN (
SELECT
sc1.SId
FROM
SC sc1
JOIN SC sc2 ON sc1.CId = '01'
AND sc2.CId = '02'
AND sc1.SId = sc2.SId
);
3.查询存在" 01 "课程但不存在" 02 "课程的学生情况(不存在时显示为 null )
SELECT
*
FROM
Student s
WHERE
s.SId IN (
SELECT
sc1.SId
FROM
( SELECT * FROM SC WHERE SC.CId = '01' ) AS sc1
LEFT JOIN ( SELECT * FROM SC WHERE SC.CId = '02' ) AS sc2 ON sc1.SId = sc2.SId
WHERE
sc2.SId IS NULL
);
4.查询不存在" 01 "课程但存在" 02 "课程的学生情况
SELECT
s.*
FROM
Student s,
(
SELECT
sc2.SId
FROM
( SELECT * FROM SC WHERE SC.CId = '01' ) AS sc1
RIGHT JOIN ( SELECT * FROM SC WHERE SC.CId = '02' ) AS sc2 ON sc1.SId = sc2.SId
WHERE
sc1.SId IS NULL
) AS sc2id
WHERE
s.SId = sc2id.SId;
5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT
s.SId,
s.Sname,
avgs1.as1
FROM
Student s,
( SELECT SId, AVG( score ) AS as1 FROM SC GROUP BY SId HAVING AVG( score ) > 60 ) AS avgs1
WHERE
s.SId = avgs1.SId;
6.查询在 SC 表存在成绩的学生信息
SELECT DISTINCT
s.*
FROM
Student s,
SC
WHERE
s.SId = SC.SId;
7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的成绩总和
SELECT
s.SId,
s.Sname,
sccs.scc,
sccs.scs
FROM
Student s
LEFT JOIN (
SELECT
SC.SId,
COUNT( SC.CId ) AS scc,
SUM( SC.score ) AS scs
FROM
SC
GROUP BY
SC.SId
) AS sccs ON s.SId = sccs.SId;
8.查有成绩的学生信息
SELECT
*
FROM
Student s
WHERE
EXISTS ( SELECT SC.SId FROM SC WHERE SC.SId = s.SId );
9.查询「李」姓老师的数量
SELECT
COUNT( * )
FROM
Teacher
WHERE
Tname LIKE '李%';
10.查询学过「张三」老师授课的同学的信息
SELECT
s.*
FROM
Teacher t,
Course c,
SC,
Student s
WHERE
t.Tname = '张三'
AND t.TId = c.TId
AND SC.CId = c.CId
AND s.SId = SC.SId;
11.查询没有学全所有课程的同学的信息
SELECT
*
FROM
Student s
WHERE
s.SId NOT IN (
SELECT
SC.SId
FROM
SC
GROUP BY
SC.SId
HAVING
COUNT( SC.CId ) = ( SELECT COUNT( c.CId ) FROM Course c )
);
12.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
SELECT DISTINCT
s.*
FROM
( SELECT * FROM SC WHERE SC.SId = '01' ) AS sc1
JOIN ( SELECT * FROM SC WHERE SC.SId != '01' ) AS sc2
JOIN Student s
WHERE
sc1.CId = sc2.CId
AND s.SId = sc2.SId;
13.查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
SELECT
SId
FROM
SC
WHERE
CId IN ( SELECT SC.CId FROM SC WHERE SC.SId = '01' )
GROUP BY
SC.SId
HAVING
COUNT( * ) = ( SELECT COUNT( * ) FROM SC WHERE SC.SId = '01' )
AND SC.SId != '01';
但是如果SId='02'增加一条CId='04',按照上面的语句也是符合条件。因为第三行的语句把这种CId='04'给过滤了。
SELECT
*
FROM
Student
WHERE
SId IN (
SELECT
SId
FROM
SC
GROUP BY
SId
HAVING
GROUP_CONCAT( CId ) = ( SELECT GROUP_CONCAT( CId ) FROM SC WHERE SId = '01' )
AND SId != '01'
);
14.查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT
s.SName
FROM
Student s
WHERE
s.SId NOT IN (
SELECT
SC.SId
FROM
Teacher t,
Course c,
SC
WHERE
t.Tname = '张三'
AND t.TId = c.TId
AND SC.CId = c.CId
);
15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT
s.SId,
s.Sname,
AVG( SC.score )
FROM
SC,
Student s
WHERE
s.SId = SC.SId
AND SC.score < 60 GROUP BY SC.SId HAVING COUNT( * ) > 1;
16.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
SELECT
s.*,
SC.score
FROM
SC,
Student s
WHERE
SC.CId = '01'
AND SC.score < 60
AND SC.SId = s.SId
ORDER BY
SC.score DESC;
17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT
SC.*,
sc1.avgs
FROM
SC
LEFT JOIN ( SELECT SId, AVG( SC.score ) AS avgs FROM SC GROUP BY SId ) AS sc1 ON SC.SId = sc1.SId
ORDER BY
sc1.avgs DESC;
18.查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT
SC.CId,
c.Cname,
MAX( SC.score ) AS tops,
MIN( SC.score ) AS lows,
AVG( SC.score ) AS avgs,
COUNT( * ) AS nump,
SUM( CASE WHEN SC.score >= 60 THEN 1 ELSE 0 END ) / COUNT( * ) AS pasr,
SUM( CASE WHEN SC.score >= 70 AND SC.score < 80 THEN 1 ELSE 0 END ) / COUNT( * ) AS medr,
SUM( CASE WHEN SC.score >= 90 THEN 1 ELSE 0 END ) / COUNT( * ) AS excr
FROM
SC,
Course c
WHERE
SC.CId = c.CId
GROUP BY
SC.CId
ORDER BY
COUNT( * ) DESC,
SC.CId ASC;
19.按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
SELECT
sc1.CId,
sc1.SId,
COUNT( sc2.score ) + 1 rank
FROM
SC sc1
LEFT JOIN SC sc2 ON sc1.CId = sc2.CId
AND sc1.score < sc2.score
GROUP BY
sc1.CId,
sc1.SId
ORDER BY
sc1.CId,
rank;
查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
SET @rank := 0;
SELECT
sc.SId,
total,
@rank := @rank + 1 AS Rank
FROM
( SELECT SC.SId, SUM( SC.score ) AS total FROM SC GROUP BY SC.SId ORDER BY total DESC ) sc;
20.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
SELECT
Course.CId,
Course.Cname,
p1.`[100-85]`,
p1.`[100-85]%`,
p2.`[85-70]`,
p2.`[85-70]%`,
p3.`[70-60]`,
p3.`[70-60]%`,
p4.`[60-0]`,
p4.`[60-0]%`
FROM
Course
LEFT JOIN (
SELECT
CId,
SUM( CASE WHEN SC.score <= 100 AND SC.score > 85 THEN 1 ELSE 0 END ) AS `[100-85]`,
ROUND(
(
SUM( CASE WHEN SC.score <= 100 AND SC.score > 85 THEN 1 ELSE 0 END ) * 100 / COUNT( * )
),
2
) `[100-85]%`
FROM
SC
GROUP BY
SC.CId
) p1 ON Course.CId = p1.CId
LEFT JOIN (
SELECT
CId,
SUM( CASE WHEN SC.score <= 85 AND SC.score > 70 THEN 1 ELSE 0 END ) AS `[85-70]`,
ROUND(
(
SUM( CASE WHEN SC.score <= 85 AND SC.score > 70 THEN 1 ELSE 0 END ) * 100 / COUNT( * )
),
2
) AS `[85-70]%`
FROM
SC
GROUP BY
SC.CId
) AS p2 ON Course.CId = p2.CId
LEFT JOIN (
SELECT
CId,
SUM( CASE WHEN SC.score <= 70 AND SC.score > 60 THEN 1 ELSE 0 END ) AS `[70-60]`,
ROUND(
(
SUM( CASE WHEN SC.score <= 70 AND SC.score > 60 THEN 1 ELSE 0 END ) * 100 / COUNT( * )
),
2
) AS `[70-60]%`
FROM
SC
GROUP BY
SC.CId
) AS p3 ON Course.CId = p3.CId
LEFT JOIN (
SELECT
CId,
SUM( CASE WHEN SC.score <= 60 AND SC.score > 0 THEN 1 ELSE 0 END ) AS `[60-0]`,
ROUND(
(
SUM( CASE WHEN SC.score <= 60 AND SC.score > 0 THEN 1 ELSE 0 END ) * 100 / COUNT( * )
),
2
) AS `[60-0]%`
FROM
SC
GROUP BY
SC.CId
) AS p4 ON Course.CId = p4.CId;
21.查询各科成绩前三名的记录
SELECT
sc1.*
FROM
SC sc1
LEFT JOIN SC sc2 ON sc1.CId = sc2.CId
AND sc1.score < sc2.score
GROUP BY
sc1.SId,
sc1.CId
HAVING
COUNT( sc2.CId ) < 3
ORDER BY
sc1.CId;
22.查询每门课程被选修的学生数
SELECT
CId,
COUNT( SId )
FROM
SC
GROUP BY
CId;
23.查询出只选修两门课程的学生学号和姓名
SELECT
Student.SId,
Student.Sname
FROM
SC,
Student
WHERE
SC.SId = Student.SId
GROUP BY
SC.SId
HAVING
COUNT( * ) = 2;
24.查询男生、女生人数
SELECT
Ssex,
COUNT( * )
FROM
Student
GROUP BY
Ssex;
25.查询名字中含有「风」字的学生信息
SELECT
*
FROM
Student
WHERE
Sname LIKE '%风%';
26.查询同名学生名单,并统计同名人数
SELECT
Sname,
COUNT( * )
FROM
Student
GROUP BY
Sname
HAVING
COUNT( * ) > 1;
27.查询 1990 年出生的学生名单
SELECT
*
FROM
Student
WHERE
YEAR ( Sage ) = 1990;
28.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT
SC.CId,
Course.Cname,
AVG( SC.score ) AS average
FROM
SC,
Course
WHERE
SC.CId = Course.CId
GROUP BY
SC.CId
ORDER BY
average DESC,
CId;
29.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
SELECT
Student.SId,
Student.Sname,
AVG( SC.score ) AS average
FROM
Student,
SC
WHERE
Student.SId = SC.SId
GROUP BY
SC.SId
HAVING
average >= 85;
30.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
SELECT
Student.Sname,
SC.score
FROM
Course,
SC,
Student
WHERE
Course.Cname = "数学"
AND Course.CId = SC.CId
AND SC.score < 60
AND Student.SId = SC.SId;
31.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
SELECT
Sname,
CId,
score
FROM
Student
LEFT JOIN SC USING ( SId );
32.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
SELECT
Student.Sname,
Course.Cname,
SC.score
FROM
SC,
Student,
Course
WHERE
SC.score > 70
AND Student.SId = SC.SId
AND SC.CId = Course.CId;
33.查询存在不及格的课程
SELECT DISTINCT
SC.CId
FROM
SC
WHERE
SC.score < 60;
34.查询课程编号为 01 且课程成绩在 80 分及以上的学生的学号和姓名
SELECT
Student.SId,
Student.Sname
FROM
Student,
SC
WHERE
Student.SId = SC.SId
AND CId = "01"
AND score >= 80;
35.求每门课程的学生人数
SELECT
SC.CId,
COUNT( * ) AS sn
FROM
SC
GROUP BY
CId;
36.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT
Student.*,
SC.CId,
SC.score
FROM
Teacher,
Course,
SC,
Student
WHERE
Teacher.Tname = "张三"
AND Teacher.TId = Course.TId
AND Course.CId = SC.CId
AND SC.SId = Student.SId
ORDER BY
SC.score DESC
LIMIT 1;
37.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT Student.*,SC.CId,SC.score
FROM Teacher,Course,SC,Student
WHERE Teacher.Tname="张三"
AND Teacher.TId=Course.TId
AND Course.CId=SC.CId
AND SC.SId=Student.SId
AND SC.score=
(SELECT MAX(SC.score)
FROM Teacher,Course,SC,Student
WHERE Teacher.Tname="张三"
AND Teacher.TId=Course.TId
AND Course.CId=SC.CId
AND SC.SId=Student.SId );
38.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT
sc1.*
FROM
SC sc1
JOIN SC sc2 USING ( SId )
WHERE
sc1.CId != sc2.CId
AND sc1.score = sc2.score
GROUP BY
sc1.CId;
39.查询每门课程成绩最好的前两名
SELECT
sc1.*
FROM
SC sc1
LEFT JOIN SC sc2 ON sc1.CId = sc2.CId
AND sc1.score < sc2.score
GROUP BY
sc1.SId,
sc1.CId
HAVING
COUNT( sc2.CId ) < 2
ORDER BY
sc1.CId;
40.统计每门课程的学生选修人数(超过 5 人的课程才统计)
SELECT
SC.CId,
COUNT( SId ) AS cc
FROM
SC
GROUP BY
CId
HAVING
cc > 5;
41.检索至少选修两门课程的学生学号
SELECT
SC.SId,
COUNT( SId ) AS cc
FROM
SC
GROUP BY
SId
HAVING
cc >= 2;
42.查询选修了全部课程的学生信息
SELECT
Student.*
FROM
SC,
Student
WHERE
SC.SId = Student.SId
GROUP BY
SC.SId
HAVING
COUNT( * ) = ( SELECT COUNT( * ) FROM Course );
43.查询各学生的年龄,只按年份来算
SELECT
SId,
Sname,
YEAR ( CURDATE( ) ) - YEAR ( Sage ) AS age
FROM
Student;
44.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
SELECT
SId,
Sname,
TIMESTAMPDIFF( YEAR, Sage, CURDATE( ) ) AS age
FROM
Student;
45.查询本周过生日的学生
SELECT
*
FROM
Student
WHERE
WEEKOFYEAR( Sage ) = WEEKOFYEAR( CURDATE( ) );
查询下周过生日的学生
SELECT
*
FROM
Student
WHERE
WEEKOFYEAR( Sage ) = WEEKOFYEAR( CURDATE( ) ) + 1;
46.查询本月过生日的学生
SELECT
*
FROM
Student
WHERE
MONTH ( Sage ) = MONTH ( CURDATE( ) );
47.查询下月过生日的学生
SELECT
*
FROM
Student
WHERE
MONTH ( Sage ) = MONTH ( CURDATE( ) ) + 1;