MySQL讲义第 48 讲——select 查询之查询练习(六)

news/2024/12/22 18:47:48/

MySQL讲义第 48 讲——select 查询之查询练习(六)

文章目录

  • MySQL讲义第 48 讲——select 查询之查询练习(六)
        • 42、查询【高等数学】课程分数低于 60 分的学生姓名和分数
        • 43、查询所有成绩在 90 分以上课程的学生姓名、课程名称和分数
        • 44、查询选修【钟南山】老师所授课程的学生中,成绩最高的学生信息及其成绩
        • 45、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
        • 46、查询每门课程成绩最好的前两名学生信息
        • 47、检索至少选修 6 门课程的学生信息
        • 48、查询学生的年龄(按照出生日期来算,当前月日 < 出生年月的月日则年龄减一)
        • 49、查询本周过生日的学生信息
        • 51、查询下周过生日的学生信息
        • 52、查询本月过生日的学生信息
        • 53、查询下月过生日的学生信息

以下查询使用到四张表,分别是:student、teacher、course 和 electives,表结构如下:

CREATE TABLE student(s_id char(5) primary key,s_name char(20),birth datetime,phone char(20),addr varchar(100)
);INSERT INTO student
VALUES('S2011','张晓刚','1999-12-3','13163735775','信阳市'),
('S2012','刘小青','1999-10-11','13603732255','新乡市'),
('S2013','曹梦德','1998-2-13','13853735522','郑州市'),
('S2014','刘艳','1998-6-24','13623735335','郑州市'),
('S2015','刘岩','1999-7-6','13813735225','信阳市'),
('S2016','刘若非','2000-8-31','13683735533','开封市'),
('S2021','董雯花','2000-7-30','13533735564','开封市'),
('S2022','周华建','1999-5-25','13243735578','郑州市'),
('S2023','特朗普','1999-6-21','13343735588','新乡市'),
('S2024','奥巴马','2000-10-17','13843735885','信阳市'),
('S2025','周健华','2000-8-22','13788736655','开封市'),
('S2026','张学有','1998-7-6','13743735566','郑州市'),
('S2031','李明博','1999-10-26','13643732222','郑州市'),
('S2032','达芬奇','1999-12-31','13043731234','郑州市');CREATE TABLE teacher(t_id char(5) primary key,t_name char(20),job_title char(20),phone char(20)
);INSERT INTO teacher
VALUES('T8001','欧阳修','教授','13703735666'),
('T8002','华罗庚','教授','13703735888'),
('T8003','钟南山','教授','13703735675'),
('T8004','钱学森','教授','13703735638'),
('T8005','李白','副教授','13703735828'),
('T8006','孔子','教授','13703735457'),
('T8007','王安石','副教授','13703735369');CREATE TABLE course(c_id char(4) primary key,c_name char(20),t_id char(5)
);INSERT INTO course
VALUES('C101','古代文学','T8001'),
('C102','高等数学','T8002'),
('C103','线性代数','T8002'),
('C104','临床医学','T8003'),
('C105','传染病学','T8003'),
('C106','大学物理','T8004'),
('C107','诗歌欣赏','T8005'),
('C108','教育学','T8006'),
('C109','刑事诉讼法','T8007'),
('C110','经济法','T8007');CREATE TABLE score(s_id char(5),c_id char(4),score int,primary key(s_id, c_id)
);INSERT INTO score
VALUES('S2011','C102',84),('S2011','C105',90),('S2011','C106',79),('S2011','C109',65),
('S2012','C101',67),('S2012','C102',52),('S2012','C103',55),('S2012','C104',86),
('S2012','C105',87),('S2012','C106',64),('S2012','C107',62),
('S2012','C108',73),('S2012','C109',78),('S2012','C110',89),
('S2013','C102',97),('S2013','C103',68),('S2013','C104',66),('S2013','C105',68),
('S2014','C102',90),('S2014','C103',85),('S2014','C104',77),('S2014','C105',96),
('S2015','C101',69),('S2015','C102',66),('S2015','C103',88),('S2015','C104',69),
('S2015','C105',66),('S2015','C106',88),('S2015','C107',69),
('S2015','C108',66),('S2015','C109',88),('S2015','C110',69),
('S2016','C101',65),('S2016','C102',69),('S2016','C107',82),('S2016','C108',56),
('S2021','C102',72),('S2021','C103',90),('S2021','C104',90),('S2021','C105',57),
('S2022','C102',88),('S2022','C103',93),('S2022','C109',47),('S2022','C110',62),
('S2023','C102',68),('S2023','C103',86),('S2023','C109',56),('S2023','C110',91),
('S2024','C102',87),('S2024','C103',97),('S2024','C109',80),('S2024','C110',81),
('S2025','C102',61),('S2025','C105',62),('S2025','C106',87),('S2025','C109',82),
('S2026','C102',59),('S2026','C105',48),('S2026','C106',90),('S2026','C109',73);

基于以上数据,完成如下查询:

42、查询【高等数学】课程分数低于 60 分的学生姓名和分数

SQL 语句如下:

SELECTs.s_name,sc.score
FROMstudent s JOIN score scON s.s_id = sc.s_idJOIN course cON c.c_id = sc.c_id
WHEREc.c_name = '高等数学' AND sc.score < 60
;
+-----------+-------+
| s_name    | score |
+-----------+-------+
| 刘小青    |    52 |
| 张学有    |    59 |
+-----------+-------+
2 rows in set (0.01 sec)

43、查询所有成绩在 90 分以上课程的学生姓名、课程名称和分数

SQL 语句如下:

SELECT s.s_name,c.c_name,sc.score
FROMstudent s JOIN score scON s.s_id = sc.s_idJOIN course cON c.c_id = sc.c_id
WHEREsc.score > 90
;
+-----------+--------------+-------+
| s_name    | c_name       | score |
+-----------+--------------+-------+
| 曹梦德    | 高等数学     |    97 |
| 刘艳      | 传染病学     |    96 |
| 周华建    | 线性代数     |    93 |
| 特朗普    | 经济法       |    91 |
| 奥巴马    | 线性代数     |    97 |
+-----------+--------------+-------+
5 rows in set (0.00 sec)

44、查询选修【钟南山】老师所授课程的学生中,成绩最高的学生信息及其成绩

SQL 语句如下:

SELECTs.s_id,s.s_name,sc.score
FROMstudent s JOIN score scON s.s_id = sc.s_id
WHEREsc.c_id IN (SELECT c_idFROM courseWHERE t_id = (SELECT t_idFROM teacherWHERE t_name = '钟南山'))
ORDER BY sc.score DESC
LIMIT1
;
+-------+--------+-------+
| s_id  | s_name | score |
+-------+--------+-------+
| S2014 | 刘艳   |    96 |
+-------+--------+-------+
1 row in set (0.00 sec)

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

SQL 语句如下:

SELECTs1.s_id, s2.c_id, s1.score
FROMscore s1 JOIN score s2ON s1.score = s2.scoreAND s1.c_id != s2.c_id AND s1.s_id = s2.s_id
;
+-------+------+-------+
| s_id  | c_id | score |
+-------+------+-------+
| S2013 | C105 |    68 |
| S2013 | C103 |    68 |
| S2015 | C104 |    69 |
| S2015 | C107 |    69 |
| S2015 | C110 |    69 |
| S2015 | C105 |    66 |
| S2015 | C108 |    66 |
| S2015 | C106 |    88 |
| S2015 | C109 |    88 |
| S2015 | C101 |    69 |
| S2015 | C107 |    69 |
| S2015 | C110 |    69 |
| S2015 | C102 |    66 |
| S2015 | C108 |    66 |
| S2015 | C103 |    88 |
| S2015 | C109 |    88 |
| S2015 | C101 |    69 |
| S2015 | C104 |    69 |
| S2015 | C110 |    69 |
| S2015 | C102 |    66 |
| S2015 | C105 |    66 |
| S2015 | C103 |    88 |
| S2015 | C106 |    88 |
| S2015 | C101 |    69 |
| S2015 | C104 |    69 |
| S2015 | C107 |    69 |
| S2021 | C104 |    90 |
| S2021 | C103 |    90 |
+-------+------+-------+
28 rows in set (0.01 sec)

46、查询每门课程成绩最好的前两名学生信息

SQL 语句如下:

SELECTsc1.c_id,sc1.s_id,sc1.score,COUNT(*) AS cnt
FROMscore sc1 JOIN score sc2ON sc1.c_id = sc2.c_idAND sc1.score <= sc2.score
GROUP BYsc1.c_id, sc1.s_id, sc1.score
HAVINGcnt <= 2
ORDER BYsc1.c_id, sc1.score DESC
;
+------+-------+-------+-----+
| c_id | s_id  | score | cnt |
+------+-------+-------+-----+
| C101 | S2015 |    69 |   1 |
| C101 | S2012 |    67 |   2 |
| C102 | S2013 |    97 |   1 |
| C102 | S2014 |    90 |   2 |
| C103 | S2024 |    97 |   1 |
| C103 | S2022 |    93 |   2 |
| C104 | S2021 |    90 |   1 |
| C104 | S2012 |    86 |   2 |
| C105 | S2014 |    96 |   1 |
| C105 | S2011 |    90 |   2 |
| C106 | S2026 |    90 |   1 |
| C106 | S2015 |    88 |   2 |
| C107 | S2016 |    82 |   1 |
| C107 | S2015 |    69 |   2 |
| C108 | S2012 |    73 |   1 |
| C108 | S2015 |    66 |   2 |
| C109 | S2015 |    88 |   1 |
| C109 | S2025 |    82 |   2 |
| C110 | S2023 |    91 |   1 |
| C110 | S2012 |    89 |   2 |
+------+-------+-------+-----+
20 rows in set (0.00 sec)

47、检索至少选修 6 门课程的学生信息

SQL 语句如下:

SELECTs.s_id,s.s_name
FROMstudent s JOIN score scON s.s_id = sc.s_id
GROUP BYs.s_id
HAVINGCOUNT(*) >= 6
;
+-------+-----------+
| s_id  | s_name    |
+-------+-----------+
| S2012 | 刘小青    |
| S2015 | 刘岩      |
+-------+-----------+
2 rows in set (0.00 sec)

48、查询学生的年龄(按照出生日期来算,当前月日 < 出生年月的月日则年龄减一)

SQL 语句如下:

SELECT s_id,s_name,birth,YEAR(NOW()) - YEAR(birth) - IF(MONTH(birth) > MONTH(NOW()) OR MONTH(birth) = MONTH(NOW()) AND DAY(birth) > DAY(NOW()),1, 0) AS age
FROMstudent
;
+-------+-----------+---------------------+------+
| s_id  | s_name    | birth               | age  |
+-------+-----------+---------------------+------+
| S2011 | 张晓刚    | 1999-12-03 00:00:00 |   20 |
| S2012 | 刘小青    | 1999-10-11 00:00:00 |   21 |
| S2013 | 曹梦德    | 1998-02-13 00:00:00 |   22 |
| S2014 | 刘艳      | 1998-06-24 00:00:00 |   22 |
| S2015 | 刘岩      | 1999-07-06 00:00:00 |   21 |
| S2016 | 刘若非    | 2000-08-31 00:00:00 |   20 |
| S2021 | 董雯花    | 2000-07-30 00:00:00 |   20 |
| S2022 | 周华建    | 1999-05-25 00:00:00 |   21 |
| S2023 | 特朗普    | 1999-06-21 00:00:00 |   21 |
| S2024 | 奥巴马    | 2000-10-17 00:00:00 |   20 |
| S2025 | 周健华    | 2000-08-22 00:00:00 |   20 |
| S2026 | 张学有    | 1998-07-06 00:00:00 |   22 |
| S2031 | 李明博    | 1999-10-26 00:00:00 |   21 |
| S2032 | 达芬奇    | 1999-12-31 00:00:00 |   20 |
| S2033 | 李明博    | 1999-10-26 00:00:00 |   21 |
| S2034 | 周健华    | 1999-12-31 00:00:00 |   20 |
| S2035 | 张学有    | 1999-10-26 00:00:00 |   21 |
| S2036 | 周健华    | 1999-12-31 00:00:00 |   20 |
+-------+-----------+---------------------+------+
18 rows in set (0.05 sec)

49、查询本周过生日的学生信息

SQL 语句如下:

SELECTs_id,s_name,birth,week(birth)
FROMstudent
WHEREWEEK(NOW()) = WEEK(birth)
;Empty set (0.00 sec)
--查询结果为空

51、查询下周过生日的学生信息

SQL 语句如下:

SELECTs_id,s_name,birth,week(birth)
FROMstudent
WHEREWEEK(NOW()) +1 = WEEK(birth)
;
Empty set (0.00 sec)
--查询结果为空

52、查询本月过生日的学生信息

SQL 语句如下:

SELECTs_id,s_name,birth
FROMstudent
WHEREMONTH(NOW()) = MONTH(birth)
;
Empty set (0.00 sec)
--查询结果为空

53、查询下月过生日的学生信息

SQL 语句如下:

SELECTs_id,s_name,birth
FROMstudent
WHEREMONTH(NOW()) +1 = MONTH(birth)
;
+-------+-----------+---------------------+
| s_id  | s_name    | birth               |
+-------+-----------+---------------------+
| S2011 | 张晓刚    | 1999-12-03 00:00:00 |
| S2032 | 达芬奇    | 1999-12-31 00:00:00 |
| S2034 | 周健华    | 1999-12-31 00:00:00 |
| S2036 | 周健华    | 1999-12-31 00:00:00 |
+-------+-----------+---------------------+
4 rows in set (0.00 sec)

http://www.ppmy.cn/news/438517.html

相关文章

数据库系统原理与应用教程(040)—— MySQL 查询(二):设置要查询的列名或表达式

数据库系统原理与应用教程&#xff08;040&#xff09;—— MySQL 查询&#xff08;二&#xff09;&#xff1a;设置要查询的列名或表达式 目录 数据库系统原理与应用教程&#xff08;040&#xff09;—— MySQL 查询&#xff08;二&#xff09;&#xff1a;设置要查询的列名或…

MySQL讲义第 45 讲——select 查询之查询练习(三)

MySQL讲义第 45 讲——select 查询之查询练习&#xff08;三&#xff09; 文章目录 MySQL讲义第 45 讲——select 查询之查询练习&#xff08;三&#xff09;22、查询有两门及以上课程不及格的学生的信息23、查询选修了编号为【C105】的课程的学生信息&#xff0c;按分数降序排…

MySQL讲义第 53 讲——select 查询之 select 语句执行过程分析

MySQL讲义第 53 讲——select 查询之 select 语句执行过程分析 文章目录 MySQL讲义第 53 讲——select 查询之 select 语句执行过程分析一、数据准备二、SELECT 语句的书写顺序三、SELECT 语句的执行顺序四、SELECT 语句的执行过程分析及验证1、FROM 子句指定查询所使用的表&am…

MySQL讲义第 46 讲——select 查询之查询练习(四)

MySQL讲义第 46 讲——select 查询之查询练习&#xff08;四&#xff09; 文章目录 MySQL讲义第 46 讲——select 查询之查询练习&#xff08;四&#xff09;28、查询每门课程的平均分并按平均分降序排列&#xff0c;显示课程编号、课程名和平均分29、查询每门课程的最高分、课…

数据库系统原理与应用教程(041)—— MySQL 查询(三):设置查询条件

数据库系统原理与应用教程&#xff08;041&#xff09;—— MySQL 查询&#xff08;三&#xff09;&#xff1a;设置查询条件 目录 数据库系统原理与应用教程&#xff08;041&#xff09;—— MySQL 查询&#xff08;三&#xff09;&#xff1a;设置查询条件一、运算符1、比较运…

数据库系统原理与应用教程(049)—— MySQL 查询(十一):子查询

数据库系统原理与应用教程&#xff08;049&#xff09;—— MySQL 查询&#xff08;十一&#xff09;&#xff1a;子查询 目录 数据库系统原理与应用教程&#xff08;049&#xff09;—— MySQL 查询&#xff08;十一&#xff09;&#xff1a;子查询一、标量子查询二、列子查询…

数据库系统原理与应用教程(053)—— MySQL 查询(十五):字符型函数的用法

数据库系统原理与应用教程&#xff08;053&#xff09;—— MySQL 查询&#xff08;十五&#xff09;&#xff1a;字符型函数的用法 目录 数据库系统原理与应用教程&#xff08;053&#xff09;—— MySQL 查询&#xff08;十五&#xff09;&#xff1a;字符型函数的用法一、创…

数据库系统原理与应用教程(039)—— MySQL 查询(一):SELECT 命令的语法分析

数据库系统原理与应用教程&#xff08;039&#xff09;—— MySQL 查询&#xff08;一&#xff09;&#xff1a;SELECT 命令的语法分析 目录 数据库系统原理与应用教程&#xff08;039&#xff09;—— MySQL 查询&#xff08;一&#xff09;&#xff1a;SELECT 命令的语法分析…