01

表结构/表数据

第一小节:表结构

【 学生表 】
CREATE TABLE Student(
    s_id VARCHAR(20) COMMENT '学生编号',
    s_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '学生姓名',
    s_birth VARCHAR(20) NOT NULL DEFAULT '' COMMENT '出生年月',
    s_sex VARCHAR(10) NOT NULL DEFAULT '' COMMENT '学生性别',
    PRIMARY KEY(s_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '学生表';
【 课程表 】
CREATE TABLE Course(
    c_id  VARCHAR(20) COMMENT '课程编号',
    c_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '课程名称',
    t_id VARCHAR(20) NOT NULL COMMENT '教师编号',
    PRIMARY KEY(c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '课程表';
【 教师表 】
CREATE TABLE Teacher(
    t_id VARCHAR(20) COMMENT '教师编号',
    t_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '教师姓名',
    PRIMARY KEY(t_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '教师表';
【 成绩表 】
CREATE TABLE Score(
    s_id VARCHAR(20) COMMENT '学生编号',
    c_id  VARCHAR(20) COMMENT '课程编号',
    s_score INT(3) COMMENT '分数',
    PRIMARY KEY(s_id,c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '成绩表';

第二小节:表数据(方便大家操作后续模块sql语句)

-- 插入学生表测试数据
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('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
 
-- 教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
 
-- 成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);

02

经典30题 sql

这里将开始我们的 sql 之旅,在这里希望对 sql 能力稍弱的同学,有一定的帮助。 如果大家在以下 sql 学习中,发现更具有优化性的建议,可以留言给小编或者加技术群交流,让我们一起成长。(底部有WeChat方式)

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


SELECT
    a.*, b.s_score AS score1,
    c.s_score AS score2
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
LEFT JOIN score c ON a.s_id = c.s_id
AND (c.c_id = '02' OR c.c_id =NULL)
WHERE
    b.s_score > c.s_score ;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

SELECT
    a.*, b.s_score AS score1,
    c.s_score AS score2
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
AND (b.c_id = '01' OR b.c_id =NULL)
LEFT JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02' 
WHERE
    b.s_score < c.s_score ;

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

SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING
    AVG(b.s_score) >= 60;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

SELECT
    a.s_id,a.s_name,
    ROUND(AVG(b.s_score),2) AS avg_score
FROM
    student a
JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING AVG(b.s_score) < 60
UNION
SELECT a.s_id,a.s_name,0 AS avg_score FROM 
student a
WHERE a.s_id NOT IN (SELECT DISTINCT s_id FROM score);

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩,并从高到低排序

SELECT
    a.s_id,
    a.s_name,
    COUNT(b.s_id) AS 选课总数 ,
    SUM(b.s_score) AS 总成绩
FROM
    student a
LEFT JOIN score b
ON
    a.s_id = b.s_id
GROUP BY a.s_id
ORDER BY SUM(b.s_score) DESC;

6、查询各学生的年龄
SELECT s_id,s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y'))-
(CASE WHEN DATE_FORMAT(NOW(),'%m%d')< DATE_FORMAT(s_birth,'%m%d') THEN 1 ELSE 0 END) AS age
FROM student

7、查询学过"张三"老师授课的同学的信息

SELECT *FROM student WHERE 
s_id IN (SELECT s_id FROM score WHERE 
c_id = (SELECT c_id FROM course WHERE
t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))
);

8、查询每门功课成绩最好的前两名

SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @i:=@i+1 as 排名 FROM score a,(SELECT @i:=0)b WHERE a.c_id='01' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
UNION
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @j:=@j+1 as 排名 FROM score a,(SELECT @j:=0)b WHERE a.c_id='02' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
UNION
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @k:=@k+1 as 排名 FROM score a,(SELECT @k:=0)b WHERE a.c_id='03' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
 

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息,及两门课程成绩

SELECT
    a.*, b.s_score,
    c.s_score 
FROM
    student a
JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02';

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT *FROM student a
WHERE
a.s_id IN (SELECT s_id FROM score  WHERE c_id = '01')
AND a.s_id NOT IN (SELECT s_id FROM score  WHERE c_id = '02');

11、查询没有学全所有课程的同学的信息
SELECT *FROM student d WHERE d.s_id IN(
    SELECT e.s_id FROM score e WHERE e.s_id NOT IN(
        SELECT a.s_id FROM score a 
            JOIN score b ON a.s_id=b.s_id AND b.c_id='02'
            JOIN score c ON a.s_id=c.s_id AND c.c_id='03'
            WHERE a.c_id='01')
);

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

SELECT * FROM student a
WHERE
a.s_id IN (SELECT DISTINCT b.s_id FROM score b WHERE 
b.c_id IN (SELECT c.c_id FROM score c WHERE c.s_id ='01')
);

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

SELECT * FROM student WHERE s_id IN(
    SELECT DISTINCT s_id FROM score WHERE s_id!='01' AND c_id IN (SELECT c_id FROM score WHERE s_id ='01') 
    GROUP BY s_id
    HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE s_id='01')
)

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

SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩 
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY a.s_id
HAVING
a.s_id IN(SELECT s_id FROM score WHERE s_score<60 GROUP BY s_id HAVING COUNT(1) >= 2)
 

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

SELECT
    a.*, b.s_score
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
WHERE
    b.c_id = '01'
AND b.s_score < 60
ORDER BY
    b.s_score DESC;

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

SELECT 
 a.s_id,
 b.s_score,
 c.s_score,
 d.s_score,
 ROUND(avg(a.s_score), 2) AS 平均分
FROM
    score a
LEFT JOIN score b ON a.s_id = b.s_id AND b.c_id='01'
LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id='02'
LEFT JOIN score d ON a.s_id = d.s_id AND d.c_id='03'
GROUP BY a.s_id
ORDER BY 平均分 DESC;

17、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT
    a.c_id AS 课程ID,
    b.c_name AS 课程name,
    MAX(a.s_score) AS 最高分,
    MIN(a.s_score) AS 最低分,
    ROUND(AVG(a.s_score),2) AS 平均分,
  ROUND(100*(SUM(CASE WHEN a.s_score >= 60 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '及格率',
    ROUND(100*(SUM(CASE WHEN a.s_score >= 70 AND  a.s_score <80 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '中等率',
  ROUND(100*(SUM(CASE WHEN a.s_score >= 80 AND  a.s_score <90 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '优良率',
  ROUND(100*(SUM(CASE WHEN a.s_score >= 90 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '优秀率'
FROM
    score a
LEFT JOIN course b ON a.c_id = b.c_id
GROUP BY
    b.c_id;

18、按各科成绩进行排序,并显示排名(mysql没有rank顺序函数)

select a.s_id,a.c_id,
        @i:=@i +1 as i保留排名,
        @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='02' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='03' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s

19、查询学生的总成绩并进行排名

SELECT a.s_id,
    @i:=@i+1 AS i,
    @k:=(CASE WHEN @score=a.sum_score THEN @k ELSE @i END) AS rank,
    @score:=a.sum_score AS score
FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) AS a,
(SELECT @i:=0,@score:=0) AS b

20、查询不同老师所教不同课程平均分从高到低显示

SELECT
    a.t_name,
    b.c_id,
    b.c_name,
    ROUND(AVG(c.s_score) ,2) AS 平均分
FROM
    teacher a
LEFT JOIN course b ON a.t_id = b.t_id
LEFT JOIN score c ON b.c_id=c.c_id
GROUP BY c.c_id
ORDER BY AVG(c.s_score) DESC;

21、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@i:=@i+1 AS 排名 FROM score a,(SELECT @i:=0)b WHERE a.c_id='01') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@j:=@j+1 AS 排名 FROM score a,(SELECT @j:=0)b WHERE a.c_id='02') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@k:=@k+1 AS 排名 FROM score a,(SELECT @k:=0)b WHERE a.c_id='03') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3;

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

SELECT
    a.c_id AS 课程编号, a.c_name AS 课程名称,
  c.`[100-85]的人数`, c.`[100-85]所占百分比`,
    d.`[85-70]的人数`, d.`[85-70]所占百分比`,
    e.`[70-60]的人数`, e.`[70-60]所占百分比`,
    f.`[0-60]的人数`, f.`[0-60]所占百分比`
FROM
    course a
LEFT JOIN score b ON a.c_id = b.c_id
LEFT JOIN
    (SELECT *,SUM(CASE WHEN s_score >85 AND s_score <=100 THEN 1 ELSE 0 END) AS '[100-85]的人数' ,
    ROUND(SUM(CASE WHEN s_score >85 AND s_score <=100 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[100-85]所占百分比'
    FROM score GROUP BY c_id) c ON a.c_id=c.c_id
LEFT JOIN
    (SELECT*,SUM(CASE WHEN s_score >70 AND s_score <=85 THEN 1 ELSE 0 END) AS '[85-70]的人数' ,
    ROUND(SUM(CASE WHEN s_score >70 AND s_score <=85 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[85-70]所占百分比'
    FROM score GROUP BY c_id) d ON a.c_id=d.c_id
LEFT JOIN
    (SELECT*,SUM(CASE WHEN s_score >60 AND s_score <=70 THEN 1 ELSE 0 END) AS '[70-60]的人数' ,
    ROUND(SUM(CASE WHEN s_score >60 AND s_score <=70 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[70-60]所占百分比'
    FROM score GROUP BY c_id) e ON a.c_id=e.c_id
LEFT JOIN
    (SELECT *,SUM(CASE WHEN s_score >0 AND s_score <=60 THEN 1 ELSE 0 END) AS '[0-60]的人数' ,
    ROUND(SUM(CASE WHEN s_score >0 AND s_score <=60 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[0-60]所占百分比'
    FROM score GROUP BY c_id) f ON a.c_id=f.c_id
GROUP BY a.c_id

23、查询学生平均成绩及其名次

SELECT
    b.s_id,
    @i:=@i+1 AS 相同分数的不同名次,
    @k:=(CASE WHEN @avg_s=b.avg_score THEN @k ELSE @i END) AS 相同分数的相同名次,
    @avg_s:=b.avg_score AS 平均成绩
FROM
(SELECT
    a.s_id,
    ROUND(AVG(a.s_score), 2) AS avg_score
FROM
    score a
GROUP BY
    a.s_id
ORDER BY AVG(a.s_score) DESC) b,(SELECT @i:=0,@avg_s:=0,@k:=0) c

24、查询出只有两门课程的全部学生的学号和姓名

SELECT a.s_id,a.s_name FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING COUNT(1)=2

25、查询同名同性学生名单,并统计同名人数

SELECT a.s_name,a.s_sex,COUNT(1) AS 人数 FROM student a 
JOIN student b ON a.s_name=b.s_name AND a.s_sex=b.s_sex AND a.s_id!=b.s_id
GROUP BY a.s_name,a.s_sex

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

SELECT c_id,ROUND(avg(s_score),2)FROM score 
GROUP BY c_id
ORDER BY avg(s_score) DESC,c_id ASC 

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

SELECT a.s_name,b.s_score FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
WHERE c_id=(
SELECT c_id FROM course WHERE c_name='数学'
) AND b.s_score < 60

28、查询所有学生的课程及分数情况;

SELECT a.s_id,a.s_name,
SUM(CASE c.c_name WHEN '语文' THEN b.s_score ELSE 0 END) AS '语文',
SUM(CASE c.c_name WHEN '数学' THEN b.s_score ELSE 0 END) AS '数学',
SUM(CASE c.c_name WHEN '英语' THEN b.s_score ELSE 0 END) AS '英语',
SUM(b.s_score) as  '总分'
FROM student a 
LEFT JOIN score b ON a.s_id = b.s_id 
LEFT JOIN course c ON b.c_id = c.c_id 
GROUP BY a.s_id,a.s_name

29、查询不及格的学生id,姓名,及其课程名称,分数

SELECT a.s_id,a.s_name,c.c_name,b.s_score FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
LEFT JOIN course c ON b.c_id=c.c_id
WHERE b.s_score < 60

30、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT c.*,d.s_score FROM student c
LEFT JOIN score d ON c.s_id=d.s_id AND d.c_id=
(SELECT c_id FROM course b WHERE t_id=(SELECT t_id FROM teacher a WHERE a.t_name='张三'))
HAVING  MAX(d.s_score)

————————end————————

欢迎大家加入WeChat 技术交流群「wechat:xzzs730」 技术群交流 长按 ↑ 上方二维码 扫码加群

链接地址:技术群分享-存档记录

https://github.com/gtcarry888/WeChat-Sharing-records

如有其他问题,可留言至 gtcarry999@163.com 邮箱。