MYSQL第三次作业

(1) 分别查询student表和score表的所有记录

复制代码
mysql> select * from student;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       |
| 903 | 张思维    | 女   |  2003 | 中文系       | 湖南省永州市       |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省阜新市       |
| 905 | 王翰      | 男   |  2004 | 英语系       | 福建省厦门市       |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
6 rows in set (0.01 sec)

mysql> select * from score;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  1 |    901 | 计算机    |    98 |
|  2 |    901 | 英语      |    80 |
|  3 |    902 | 计算机    |    65 |
|  4 |    902 | 中文      |    88 |
|  5 |    903 | 中文      |    95 |
|  6 |    904 | 计算机    |    70 |
|  7 |    904 | 英语      |    92 |
|  8 |    905 | 英语      |    94 |
|  9 |    906 | 计算机    |    49 |
| 10 |    906 | 英语      |    83 |
+----+--------+-----------+-------+
10 rows in set (0.00 sec)

(2) 查询student表的第2条到5条记录

复制代码
mysql> select * from student limit 1, 4;
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | birth | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 902 | 周全有    | 男   |  2000 | 中文系     | 北京市昌平区       |
| 903 | 张思维    | 女   |  2003 | 中文系     | 湖南省永州市       |
| 904 | 李广昌    | 男   |  1999 | 英语系     | 辽宁省阜新市       |
| 905 | 王翰      | 男   |  2004 | 英语系     | 福建省厦门市       |
+-----+-----------+------+-------+------------+--------------------+
4 rows in set (0.00 sec)

(3) 从student表中查询计算机系和英语系的学生的信息

复制代码
mysql> select * from student where department in ('计算机系', '英语系');
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省阜新市       |
| 905 | 王翰      | 男   |  2004 | 英语系       | 福建省厦门市       |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
4 rows in set (0.01 sec)

(4) 从student表中查询年龄小于22岁的学生信息

复制代码
mysql> select * from student where 2024 - birth < 22;
+-----+-----------+------+-------+------------+--------------------+
| id  | name      | sex  | birth | department | address            |
+-----+-----------+------+-------+------------+--------------------+
| 903 | 张思维    | 女   |  2003 | 中文系     | 湖南省永州市       |
| 905 | 王翰      | 男   |  2004 | 英语系     | 福建省厦门市       |
+-----+-----------+------+-------+------------+--------------------+
2 rows in set (0.01 sec)

(5) 从student表中查询每个院系有多少人

复制代码
mysql> select department, count(*) as num from student group by department;
+--------------+-----+
| department   | num |
+--------------+-----+
| 计算机系     |   2 |
| 中文系       |   2 |
| 英语系       |   2 |
+--------------+-----+
3 rows in set (0.01 sec)

(6) 从score表中查询每个科目的最高分

复制代码
mysql> select c_name, max(grade) as max_grade from score group by c_name;
+-----------+-----------+
| c_name    | max_grade |
+-----------+-----------+
| 计算机    |        98 |
| 英语      |        94 |
| 中文      |        95 |
+-----------+-----------+
3 rows in set (0.01 sec)

(7) 查询李广昌的考试科目(c_name)和考试成绩(grade)

s为score表的别名

st为student表的别名

复制代码
mysql> select s.c_name, s.grade from score s join student st on s.stu_id = st.id where st.name = '李广昌';
+-----------+-------+
| c_name    | grade |
+-----------+-------+
| 计算机    |    70 |
| 英语      |    92 |
+-----------+-------+
2 rows in set (0.00 sec)

(8) 用连接的方式查询所有学生的信息和考试信息

复制代码
mysql> select st.*, s.c_name, s.grade from student st left join score s on st.id = s.stu_id;
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| id  | name      | sex  | birth | department   | address            | c_name    | grade |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       | 英语      |    80 |
| 901 | 张三丰    | 男   |  2002 | 计算机系     | 北京市海淀区       | 计算机    |    98 |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       | 中文      |    88 |
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       | 计算机    |    65 |
| 903 | 张思维    | 女   |  2003 | 中文系       | 湖南省永州市       | 中文      |    95 |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省阜新市       | 英语      |    92 |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省阜新市       | 计算机    |    70 |
| 905 | 王翰      | 男   |  2004 | 英语系       | 福建省厦门市       | 英语      |    94 |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       | 英语      |    83 |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       | 计算机    |    49 |
+-----+-----------+------+-------+--------------+--------------------+-----------+-------+
10 rows in set (0.01 sec)

(9) 计算每个学生的总成绩

复制代码
mysql> select st.id, st.name, sum(s.grade) as total_grade from student st left join score s on st.id = s.stu_id group by st.id, st.name;
+-----+-----------+-------------+
| id  | name      | total_grade |
+-----+-----------+-------------+
| 901 | 张三丰    |         178 |
| 902 | 周全有    |         153 |
| 903 | 张思维    |          95 |
| 904 | 李广昌    |         162 |
| 905 | 王翰      |          94 |
| 906 | 王心凌    |         132 |
+-----+-----------+-------------+
6 rows in set (0.00 sec)

(10) 计算每个考试科目的平均成绩

复制代码
mysql> select c_name, avg(grade) as avg_grade from score group by c_name;
+-----------+-----------+
| c_name    | avg_grade |
+-----------+-----------+
| 计算机    |   70.5000 |
| 英语      |   87.2500 |
| 中文      |   91.5000 |
+-----------+-----------+
3 rows in set (0.00 sec)

(11) 查询计算机成绩低于95的学生信息

复制代码
mysql> select st.* from student st join score s on st.id = s.stu_id where s.c_name = '计算机' and s.grade < 95;
+-----+-----------+------+-------+--------------+--------------------+
| id  | name      | sex  | birth | department   | address            |
+-----+-----------+------+-------+--------------+--------------------+
| 902 | 周全有    | 男   |  2000 | 中文系       | 北京市昌平区       |
| 904 | 李广昌    | 男   |  1999 | 英语系       | 辽宁省阜新市       |
| 906 | 王心凌    | 女   |  1998 | 计算机系     | 湖南省衡阳市       |
+-----+-----------+------+-------+--------------+--------------------+
3 rows in set (0.00 sec)

(12) 将计算机考试成绩按从高到低进行排序

复制代码
mysql> select * from score where c_name = '计算机' order by grade desc;
+----+--------+-----------+-------+
| id | stu_id | c_name    | grade |
+----+--------+-----------+-------+
|  1 |    901 | 计算机    |    98 |
|  6 |    904 | 计算机    |    70 |
|  3 |    902 | 计算机    |    65 |
|  9 |    906 | 计算机    |    49 |
+----+--------+-----------+-------+
4 rows in set (0.00 sec)

(13) 从student表和score表中查询出学生的学号,然后合并查询结果

复制代码
mysql> select id from student union select stu_id from score;
+-----+
| id  |
+-----+
| 901 |
| 902 |
| 903 |
| 904 |
| 905 |
| 906 |
+-----+
6 rows in set (0.01 sec)

(14) 查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

复制代码
mysql> select st.name, st.department, s.c_name, s.grade from student st join score s on st.id = s.stu_id where st.name like '张%' or st.name like '王%';
+-----------+--------------+-----------+-------+
| name      | department   | c_name    | grade |
+-----------+--------------+-----------+-------+
| 张三丰    | 计算机系     | 计算机    |    98 |
| 张三丰    | 计算机系     | 英语      |    80 |
| 张思维    | 中文系       | 中文      |    95 |
| 王翰      | 英语系       | 英语      |    94 |
| 王心凌    | 计算机系     | 计算机    |    49 |
| 王心凌    | 计算机系     | 英语      |    83 |
+-----------+--------------+-----------+-------+
6 rows in set (0.00 sec)

(15) 查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

复制代码
mysql> select st.name, 2024 - st.birth as age, st.department, s.c_name, s.grade from student st join score s on st.id = s.stu_id where st.address like '湖南省%';
+-----------+------+--------------+-----------+-------+
| name      | age  | department   | c_name    | grade |
+-----------+------+--------------+-----------+-------+
| 张思维    |   21 | 中文系       | 中文      |    95 |
| 王心凌    |   26 | 计算机系     | 计算机    |    49 |
| 王心凌    |   26 | 计算机系     | 英语      |    83 |
+-----------+------+--------------+-----------+-------+
3 rows in set (0.00 sec)
相关推荐
jiunian_cn8 分钟前
【Redis】渐进式遍历
数据库·redis·缓存
橙露23 分钟前
Spring Boot 核心原理:自动配置机制与自定义 Starter 开发
java·数据库·spring boot
冰暮流星24 分钟前
sql语言之分组语句group by
java·数据库·sql
符哥200825 分钟前
Ubuntu 常用指令集大全(附实操实例)
数据库·ubuntu·postgresql
筵陌32 分钟前
MySQL索引及其底层原理(上)
mysql
C++ 老炮儿的技术栈42 分钟前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
怣501 小时前
MySQL子查询零基础入门教程:从小白到上手(零基础入门版)
数据库·mysql
码界调试侠1 小时前
MongoDB 常用查询语法
数据库·mongodb
静听山水1 小时前
StarRocks导入数据【Stream Load】
数据库
藦卡机器人1 小时前
国产机械臂做的比较好的品牌有哪些?
大数据·数据库·人工智能