MySQL 50题练习

MySQL 50题练习 表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name…

MySQL 50题练习[数据库教程]

表名和字段
–1.学生表
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) –教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) –学生编号,课程编号,分数

set sql_mode = ‘‘;
set sql_mode = ‘NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES‘;

测试数据

–建表

–学生表

CREATE TABLE Student(
s_id VARCHAR(20),
s_name VARCHAR(20) NOT NULL DEFAULT ‘‘,
s_birth VARCHAR(20) NOT NULL DEFAULT ‘‘,
s_sex VARCHAR(10) NOT NULL DEFAULT ‘‘,
PRIMARY KEY(s_id)
);

–课程表

CREATE TABLE Course(
c_id VARCHAR(20),
c_name VARCHAR(20) NOT NULL DEFAULT ‘‘,
t_id VARCHAR(20) NOT NULL,
PRIMARY KEY(c_id)
);

–教师表

CREATE TABLE Teacher(
t_id VARCHAR(20),
t_name VARCHAR(20) NOT NULL DEFAULT ‘‘,
PRIMARY KEY(t_id)
);

–成绩表

CREATE TABLE Score(
s_id VARCHAR(20),
c_id VARCHAR(20),
s_score INT(3),
PRIMARY KEY(s_id,c_id)
);

–插入学生表测试数据

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);
insert into Score values(‘03‘ , ‘03‘ , 80);
insert into Score values(‘04‘ , ‘01‘ , 50);
insert into Score values(‘04‘ , ‘02‘ , 30);
insert into Score values(‘04‘ , ‘03‘ , 20);
insert into Score values(‘05‘ , ‘01‘ , 76);
insert into Score values(‘05‘ , ‘02‘ , 87);
insert into Score values(‘06‘ , ‘01‘ , 31);
insert into Score values(‘06‘ , ‘03‘ , 34);
insert into Score values(‘07‘ , ‘02‘ , 89);
insert into Score values(‘07‘ , ‘03‘ , 98);

MySQL50题

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

解法1:分别把课程01的成绩和课程02的成绩查询出来之后再比较

SELECT a.*, d.01_score, d.02_score FROM student a INNER JOIN
(SELECT b.s_id, b.01_score, c.02_score FROM
(SELECT s_id, s_score as 01_score FROM score WHERE c_id=‘01‘)b INNER JOIN
(SELECT s_id, s_score as 02_score FROM score WHERE c_id=‘02‘)c ON b.s_id=c.s_id
WHERE b.01_score > c.02_score)d ON a.s_id=d.s_id;

解法2:全部查出来多重条件筛选

SELECT a.*, b.s_score as 01_score, c.s_score as 02_score FROM student a, score b, score c
WHERE a.s_id = c.s_id AND
a.s_id = b.s_id AND
b.c_id = ‘01‘ AND
c.c_id = ‘02‘ AND
b.s_score > c.s_score;

解法3:

select a.* ,b.s_score as 01_score,c.s_score as 02_score from
student a
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″课程成绩低的学生的信息及课程分数

同上题解法

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

SELECT 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_score >= 60
ORDER BY avg_score DESC;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

    # (包括有成绩的和无成绩的)

代码100分

SELECT b.s_name, b.s_id, ROUND(SUM(a.s_score)/COUNT(a.c_id),2) as avg_score
FROM score a JOIN student b ON a.s_id=b.s_id
GROUP BY a.s_id HAVING avg_score < 60
UNION SELECT a.s_name, a.s_id, 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.c_id)as sum_course, SUM(b.s_score) as sum_score FROM student a JOIN score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name
UNION SELECT a.s_id, a.s_name, 0 as sum_sourse, 0 as sum_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score);

6、查询”李”姓老师的数量

SELECT COUNT(t_id) as ‘李姓老师数量‘ FROM teacher WHERE t_name LIKE “李%” GROUP BY t_id;

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

SELECT a.* FROM student a, score b, teacher c, course d
WHERE a.s_id=b.s_id AND
b.c_id = d.c_id AND
c.t_id = d.t_id AND
c.t_name = ‘张三‘;

8、查询没学过”张三”老师授课的同学的信息

SELECT a.* FROM student a WHERE a.s_id NOT in (SELECT a.s_id FROM student a, score b, teacher c, course d
WHERE a.s_id=b.s_id AND
b.c_id = d.c_id AND
c.t_id = d.t_id AND
c.t_name = ‘张三‘);

9、查询学过编号为”01″并且也学过编号为”02″的课程的同学的信息

SELECT a.* FROM student a JOIN
(SELECT a.s_id FROM (SELECT s_id FROM score WHERE c_id=‘01‘)a INNER JOIN (SELECT s_id FROM score WHERE c_id=‘02‘)b on a.s_id=b.s_id)d on a.s_id=d.s_id;

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

SELECT c.* FROM student c JOIN
(SELECT a.s_id FROM score a WHERE a.c_id=‘01‘ AND a.s_id NOT in (SELECT s_id FROM score WHERE c_id=‘02‘))d ON c.s_id=d.s_id;

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

SELECT a.*, COUNT(b.c_id) as sum_course FROM student a JOIN score b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course < 3;

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

SELECT DISTINCT a.* FROM student a JOIN score b ON a.s_id=b.s_id WHERE b.c_id in
(SELECT c.c_id FROM score c WHERE c.s_id=‘01‘);

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

GROUP_CONCAT(expr) 配合 GROUP BY 可以将同一个分组中产生的值连接起来

SELECT c.*, d.course FROM student c JOIN
(SELECT a.s_id, a.course FROM
(SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id)a JOIN (SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id HAVING s_id=‘01‘)b
ON a.course=b.course)d ON c.s_id=d.s_id WHERE c.s_id not in (1);

14、查询各科成绩最高分、最低分和平均分: 以如下形式显示:

课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT a.c_id, b.c_name, MAX(a.s_score) as max_score, MIN(a.s_score) as min_score, ROUND(AVG(a.s_score), 2) as avg_score, COUNT(a.s_id)as count_stu,
CONCAT(ROUND(SUM(CASE when a.s_score >= 60 THEN 1 ELSE 0 END)/COUNT(), 2)100, ‘%‘) as ‘及格率‘,
CONCAT(ROUND(SUM(CASE when a.s_score >= 70 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(), 2)100, ‘%‘) as ‘中等率‘,
CONCAT(ROUND(SUM(CASE when a.s_score >= 80 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(),2)100, ‘%‘) as ‘优良率‘,
CONCAT(ROUND(SUM(CASE when a.s_score >= 90 THEN 1 ELSE 0 END)/COUNT(),2)100, ‘%‘) as ‘优秀率‘
FROM score a JOIN course b ON a.c_id=b.c_id GROUP BY a.c_id ORDER BY count_stu DESC, a.c_id ASC;

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

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

select a.s_id,a.c_id,
@i:[email protected] +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 GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s;

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

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

SELECT a.s_id, a.sum_score,
@i:[email protected]+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)a, (SELECT @k:=0, @i:=0, @score:=0)s;

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

SELECT score.c_id, c_name,
CONCAT(ROUND(sum(CASE WHEN s_score <=100 and s_score>85 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[100-85]‘,
CONCAT(ROUND(sum(CASE WHEN s_score <=85 and s_score>70 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[85-70]‘,
CONCAT(ROUND(sum(CASE WHEN s_score <=70 and s_score>60 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[70-60]‘,
CONCAT(ROUND(sum(CASE WHEN s_score <=60 and s_score>=0 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[60-0]‘
FROM score JOIN course ON score.c_id=course.c_id GROUP BY score.c_id;

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

(SELECT c_id, s_score FROM score WHERE c_id = ‘01‘ ORDER BY s_score DESC LIMIT 3)
UNION (SELECT c_id, s_score FROM score WHERE c_id = ‘02‘ ORDER BY s_score DESC LIMIT 3)
UNION (SELECT c_id, s_score FROM score WHERE c_id = ‘03‘ ORDER BY s_score DESC LIMIT 3);

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

SELECT c_id, COUNT(s_id)as sum_stu FROM score GROUP BY c_id;

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

SELECT a.s_id, b.s_name, COUNT(a.c_id)as sum_course FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course = 2;

21、查询男生、女生人数

SELECT s_sex, COUNT(s_id)as sex_count FROM student GROUP BY s_sex;

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

SELECT * FROM student WHERE s_name LIKE ‘%风%‘;

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

SELECT s_name,s_sex, count()as ‘同名人数‘
FROM student
GROUP BY s_name,s_sex
HAVING COUNT(
) >1;

24、查询 1990 年出生的学生名单

SELECT * FROM student WHERE YEAR(s_birth)=‘1990‘;

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

SELECT c_id, ROUND(AVG(s_score),2)as avg_score FROM score GROUP BY c_id ORDER BY avg_score DESC, c_id;

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

SELECT a.s_id,b.s_name, ROUND(SUM(a.s_score)/COUNT(a.c_id), 2) as avg_score FROM score a JOIN student b on a.s_id=b.s_id GROUP BY a.s_id ORDER BY avg_score DESC

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

SELECT a.s_id, b.c_name,c.s_name, a.s_score FROM score a JOIN course b on a.c_id=b.c_id JOIN student c ON a.s_id=c.s_id WHERE b.c_name=‘数学‘ AND a.s_score < 60;

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

SELECT * FROM score

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

取反,先把所有课程成绩在70分以下的学生id找出来(总课程计数和70以下课程计数一样的就是全部课程成绩在70分以下的),取反就是任何一门课程成绩在70分以上的同学

SELECT d.s_name, f.c_name, e.s_score FROM student d JOIN score e ON d.s_id=e.s_id JOIN course f ON e.c_id = f.c_id WHERE d.s_id NOT in
(SELECT a.s_id FROM
(SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id)a JOIN
(SELECT s_id, COUNT(c_id)as course_count FROM score WHERE s_score < 70 GROUP BY s_id)b ON (a.course_count=b.course_count and a.s_id=b.s_id));

30、查询不及格的课程

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

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

SELECT a.s_id, b.s_name, a.c_id,a.s_score FROM score a JOIN student b ON a.s_id=b.s_id WHERE a.c_id=‘01‘ AND a.s_score >= 80;

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

SELECT c_id, COUNT(s_id) as count_stu FROM score GROUP BY c_id;

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

SELECT d.*, b.c_name, c.s_score FROM teacher a
JOIN course b ON a.t_id=b.t_id
JOIN score c ON b.c_id=c.c_id
JOIN student d ON c.s_id=d.s_id
WHERE a.t_name=‘张三‘ ORDER BY c.s_score DESC LIMIT 1;

select a.*,b.s_score,b.c_id,c.c_name 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.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name=‘张三‘)
and b.s_score in (select MAX(s_score) from score where c_id=‘02‘)

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

select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score;

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

查询两张score表,找出b表中比a表中单科成绩高的计数(双循环比较 O(n^2)),小于等于2那就是找到了前两位

select a.s_id,a.c_id,a.s_score from score a
where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id;

36、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT c_id, COUNT(s_id)as stu_count FROM score GROUP BY c_id HAVING stu_count > 5 ORDER BY stu_count DESC, c_id;

37、检索至少选修两门课程的学生学号

SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id HAVING course_count >= 2;

38、查询选修了全部课程的学生信息

SELECT b.*,COUNT(a.c_id) as course_count FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING course_count=(SELECT COUNT(c_id) FROM course);

39、查询各学生的年龄

代码100分-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

MySQL 50题练习

原文:https://www.cnblogs.com/wsilj/p/12769760.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8820.html

(0)
上一篇 2023-02-21
下一篇 2023-02-21

相关推荐

  • Jupyter安装方法

    Jupyter安装方法Jupyter是一款支持多种编程语言的交互式计算环境,可以帮助用户轻松地将代码、文本、图像、视频等多种形式的内容融合在一起,进行数据分析和数据可视化。

    2024-05-11
    77
  • 虚拟机安装数据库_怎么安装mysql数据库

    虚拟机安装数据库_怎么安装mysql数据库从模板创建虚拟机设置虚拟机名字如果需要自定义硬件,可以设置等待虚拟机克隆完成克隆完成后,设置虚拟机修改IP地址修改主机名,加域安装数据库之前必须先安装.net3.5安装数据库安装数据库引擎服务设置混…

    2023-04-09
    151
  • 用Python创建精美的画布

    用Python创建精美的画布画布是图形界面程序的核心,可以用于显示图形、图片、动画等,同时也可以用于数据可视化和图表绘制。下面我们将介绍如何用Python创建精美的画布,让你的程序更加生动有趣。主要包括以下几个方面:

    2024-02-14
    91
  • CI session类「建议收藏」

    CI session类「建议收藏」初始化 Session 类 Session 通常会在每个页面载入的时候全局运行,所以 Session 类必须首先被初始化。 您可以在 控制器 的构造函数中初始化它, 也可以在系统中 自动加载。Ses…

    2023-01-26
    180
  • Python 路径拼接详解

    Python 路径拼接详解在我们的日常编程中,我们经常需要处理文件或文件夹的路径,Python提供了一组标准库函数来完成这些任务。本文将详细介绍Python路径拼接的相关知识。

    2024-08-21
    28
  • impala jdbc连接_db2 jdbc

    impala jdbc连接_db2 jdbc首先要引入ImpalaJDBC41.jar包。可以网上找,也可以直接下面的地址下载。 链接:https://pan.baidu.com/s/1MTJ0Wn1pwEmtXwp-_E4gIw 提取码:j…

    2023-02-10
    175
  • localthread用法_速度是位移的导数

    localthread用法_速度是位移的导数1. LOAD DATA INFILE 为什么比 INSERT 快?
    2. sysbench 压测 MySQL 的四个标准步骤。
    3. 怎么让 sysbench 支持 LOAD DATA LOCAL

    2023-05-11
    144
  • mysql表示日期的数据类型_sql定义日期数据类型

    mysql表示日期的数据类型_sql定义日期数据类型类型名称日期格式日期范围存储需求 YEAR YYYY 1901 ~ 2155 1 个字节 TIME HH:MM:SS -838:59:59 ~ 838:59:59 3 个字节 DATE YYYY-MM

    2023-02-16
    150

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注