MYSQL基础例题结果

本文介绍了如何使用SQL在MySQL中创建`student`和`score`表,以及如何向这些表中插入数据。通过示例查询了`student`表的所有记录、指定范围的记录,以及按照特定条件筛选数据,如院系、年龄等。还展示了如何查询每个院系的学生人数,`score`表中每个科目的最高分,以及计算学生总成绩、平均成绩等。此外,文章还涵盖了联查、分组聚合等高级查询技巧。
摘要由CSDN通过智能技术生成

1.写出创建 student 和 score 表的 SQL 语句,并在自己数据库上执行

create table student(id int(10) primary key not null unique auto_increment, name varchar(20) not null,sex varchar(4),birth year,department varchar(20) not null,address varchar(50));

create table score (id int(10) primary key not null unique auto_increment,stu_id int(10) not null,c_name varchar(20),grade int(10));

2.为 student 表和 score 表增加记录,写出 SQL 语句以及在自己的数据库上执行

向 student 表插入记录的 INSERT 语句如下:
‘张老大’, ‘男’,1985,‘计算机系’, ‘北京市海淀区’
‘张老二’, ‘男’,1986,‘中文系’, ‘北京市昌平区’
‘张三’, ‘女’,1990,‘中文系’, ‘湖南省永州市’);
李四’, ‘男’,1990,‘英语系’, ‘辽宁省阜新市’);
王五’, ‘女’,1991,‘英语系’, ‘福建省厦门市’);
王六’, ‘男’,1988,‘计算机系’, ‘湖南省衡阳市’);

insert into student(id,name,sex,birth,department,address)values(‘801’,‘张老大’, ‘男’,1985,‘计算机系’, ‘北京市海淀区’),
(‘802’,‘张老二’, ‘男’,1986,‘中文系’, ‘北京市昌平区’),
(‘803’,‘张三’, ‘女’,1990,‘中文系’, ‘湖南省永州市’),
(‘804’,‘李四’, ‘男’,1990,‘英语系’, ‘辽宁省阜新市’),
(‘805’,‘王五’, ‘女’,1991,‘英语系’, ‘福建省厦门市’),
(‘806’,‘王六’, ‘男’,1988,‘计算机系’, ‘湖南省衡阳市’);

向 score 表插入记录的 INSERT 语句如下:
张老大,‘计算机’,98);
张老大,‘英语’, 80);
张老二, ‘计算机’,65);
张老二, ‘中文’,88);
张三, ‘中文’,95);
李四, ‘计算机’,70);
李四, ‘英语’,92);
王五, ‘英语’,94);
王五, ‘计算机’,90);
王六, ‘英语’,85);

insert into score (stu_id,c_name,grade)values(801,‘计算机’,98),
(801,‘英语’, 80),
(802, ‘计算机’,65),
(802, ‘中文’,88),
(803, ‘中文’,95),
(804, ‘计算机’,70),
(804, ‘英语’,92),
(805, ‘英语’,94),
(805, ‘计算机’,90),
(806, ‘英语’,85);

3.查询 student 表的所有记录

MariaDB [exp]> SELECT * FROM student;
±----±----------±-----±------±-------------±-------------------+
| id | name | sex | birth | department | address |
±----±----------±-----±------±-------------±-------------------+
| 801 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 802 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 803 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 806 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
±----±----------±-----±------±-------------±-------------------+
6 rows in set (0.001 sec)

4.查询 student 表的第 2 条到 4 条记录

MariaDB [exp]> SELECT * FROM student LIMIT 1,3;
±----±----------±-----±------±-----------±-------------------+
| id | name | sex | birth | department | address |
±----±----------±-----±------±-----------±-------------------+
| 802 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 803 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
±----±----------±-----±------±-----------±-------------------+
3 rows in set (0.000 sec)

5.从 student 表查询所有学生的学号(id)、姓名(name)和院系(department)的信息

MariaDB [exp]> SELECT id,name,department FROM student;
±----±----------±-------------+
| id | name | department |
±----±----------±-------------+
| 801 | 张老大 | 计算机系 |
| 802 | 张老二 | 中文系 |
| 803 | 张三 | 中文系 |
| 804 | 李四 | 英语系 |
| 805 | 王五 | 英语系 |
| 806 | 王六 | 计算机系 |
±----±----------±-------------+
6 rows in set (0.001 sec)

6.从 student 表中查询计算机系和英语系的学生的信息

MariaDB [exp]> SELECT * FROM student WHERE department=‘计算机系’ or department=‘英语系’;
±----±----------±-----±------±-------------±-------------------+
| id | name | sex | birth | department | address |
±----±----------±-----±------±-------------±-------------------+
| 801 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 806 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
±----±----------±-----±------±-------------±-------------------+
4 rows in set (0.001 sec)

7.从 student 表中查询年龄 28~32 岁的学生信息

MariaDB [exp]> SELECT * FROM student WHERE birth>1987 and birth<1993;
±----±-------±-----±------±-------------±-------------------+
| id | name | sex | birth | department | address |
±----±-------±-----±------±-------------±-------------------+
| 803 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 806 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
±----±-------±-----±------±-------------±-------------------+
4 rows in set (0.001 sec)

8.从 student 表中查询每个院系有多少人

MariaDB [exp]> SELECT
-> sum(case when department=‘中文系’ then 1 else 0 end) as 中文系人数,
-> sum(case when department=‘英语系’ then 1 else 0 end) as 英语系人数,
-> sum(case when department=‘计算机系’ then 1 else 0 end) as 计算机系人数
-> FROM student;
±----------------±----------------±-------------------+
| 中文系人数 | 英语系人数 | 计算机系人数 |
±----------------±----------------±-------------------+
| 2 | 2 | 2 |
±----------------±----------------±-------------------+
1 row in set (0.001 sec)

巩固练习:查询score表学习每个课程的人数

MariaDB [exp]> SELECT
-> sum(case when c_name=‘中文’ then 1 else 0 end) as 中文人数,
-> sum(case when c_name=‘英语’ then 1 else 0 end) as 英语人数,
-> sum(case when c_name=‘计算机’ then 1 else 0 end) as 计算机人数
-> from score;
±-------------±-------------±----------------+
| 中文人数 | 英语人数 | 计算机人数 |
±-------------±-------------±----------------+
| 2 | 4 | 4 |
±-------------±-------------±----------------+
1 row in set (0.001 sec)

9.从 score 表中查询每个科目的最高分

MariaDB [exp]> SELECT max(grade) as 中文最高分 FROM score WHERE c_name=‘中文’;
±----------------+
| 中文最高分 |
±----------------+
| 95 |
±----------------+
1 row in set (0.001 sec)

MariaDB [exp]> SELECT max(grade) as 英语最高分 FROM score WHERE c_name=‘英语’;
±----------------+
| 英语最高分 |
±----------------+
| 94 |
±----------------+
1 row in set (0.000 sec)

MariaDB [exp]> SELECT max(grade) as 计算机最高分 FROM score WHERE c_name=‘计算机’;
±-------------------+
| 计算机最高分 |
±-------------------+
| 98 |
±-------------------+
1 row in set (0.000 sec)

10.查询李四的考试科目(c_name)和考试成绩(grade)

MariaDB [exp]> SELECT c_name as 考试科目, grade as 考试成绩 FROM score WHERE stu_id=(SELECT id FROM student WHERE name=‘李四’);
±-------------±-------------+
| 考试科目 | 考试成绩 |
±-------------±-------------+
| 计算机 | 70 |
| 英语 | 92 |
±-------------±-------------+
2 rows in set (0.001 sec)

11.用连接的方式查询所有学生的信息和考试信息

MariaDB [exp]> select * from student,score where student.id=score.stu_id;
±----±----------±-----±------±-------------±-------------------±—±-------±----------±------+
| id | name | sex | birth | department | address | id | stu_id | c_name | grade |
±----±----------±-----±------±-------------±-------------------±—±-------±----------±------+
| 801 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 1 | 801 | 计算机 | 98 |
| 801 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 | 2 | 801 | 英语 | 80 |
| 802 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 3 | 802 | 计算机 | 65 |
| 802 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 | 4 | 802 | 中文 | 88 |
| 803 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 | 5 | 803 | 中文 | 95 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 6 | 804 | 计算机 | 70 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 | 7 | 804 | 英语 | 92 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 8 | 805 | 英语 | 94 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 | 9 | 805 | 计算机 | 90 |
| 806 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 | 10 | 806 | 英语 | 85 |
±----±----------±-----±------±-------------±-------------------±—±-------±----------±------+
10 rows in set (0.001 sec)

12.计算每个学生的总成绩

MariaDB [exp]> select student.name,sum(score.grade) as 总成绩 from student,score where student.id=score.stu_id group by score.stu_id;
±----------±----------+
| name | 总成绩 |
±----------±----------+
| 张老大 | 178 |
| 张老二 | 153 |
| 张三 | 95 |
| 李四 | 162 |
| 王五 | 184 |
| 王六 | 85 |
±----------±----------+
6 rows in set (0.001 sec)

13.计算每个考试科目的平均成绩

MariaDB [exp]> select c_name as 考试科目, avg(grade) as 平均成绩 from score group by c_name;
±-------------±-------------+
| 考试科目 | 平均成绩 |
±-------------±-------------+
| 中文 | 91.5000 |
| 英语 | 87.7500 |
| 计算机 | 80.7500 |
±-------------±-------------+
3 rows in set (0.001 sec)

14.查询计算机成绩低于 95 的学生信息

MariaDB [exp]> select * from student where id in (select stu_id from score where grade<95 and c_name=‘计算机’);
±----±----------±-----±------±-----------±-------------------+
| id | name | sex | birth | department | address |
±----±----------±-----±------±-----------±-------------------+
| 802 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
±----±----------±-----±------±-----------±-------------------+
3 rows in set (0.000 sec)

15.查询同时参加计算机和英语考试的学生的信息

MariaDB [exp]> select * from student where id in(select stu_id from score where c_name=‘计算机’) and id in(select stu_id from score where c_name=‘英语’);
±----±----------±-----±------±-------------±-------------------+
| id | name | sex | birth | department | address |
±----±----------±-----±------±-------------±-------------------+
| 801 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 804 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 805 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
±----±----------±-----±------±-------------±-------------------+
3 rows in set (0.001 sec)

16.将计算机考试成绩按从高到低进行排序

MariaDB [exp]> select * from score where c_name=‘计算机’ order by grade desc;
±—±-------±----------±------+
| id | stu_id | c_name | grade |
±—±-------±----------±------+
| 1 | 801 | 计算机 | 98 |
| 9 | 805 | 计算机 | 90 |
| 6 | 804 | 计算机 | 70 |
| 3 | 802 | 计算机 | 65 |
±—±-------±----------±------+
4 rows in set (0.000 sec)

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

MariaDB [exp]> select id from student union select stu_id from score;
±----+
| id |
±----+
| 801 |
| 802 |
| 803 |
| 804 |
| 805 |
| 806 |
±----+
6 rows in set (0.000 sec)

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

MariaDB [exp]> select
-> student.name as 姓名,
-> student.department as 院系,
-> score.c_name as 考试科目,
-> score.grade as 成绩
-> from student,score
-> where
-> (student.name like ‘张%’
-> or student.name like ‘王%’)
-> and student.id=score.stu_id;
±----------±-------------±-------------±-------+
| 姓名 | 院系 | 考试科目 | 成绩 |
±----------±-------------±-------------±-------+
| 张老大 | 计算机系 | 计算机 | 98 |
| 张老大 | 计算机系 | 英语 | 80 |
| 张老二 | 中文系 | 计算机 | 65 |
| 张老二 | 中文系 | 中文 | 88 |
| 张三 | 中文系 | 中文 | 95 |
| 王五 | 英语系 | 英语 | 94 |
| 王五 | 英语系 | 计算机 | 90 |
| 王六 | 计算机系 | 英语 | 85 |
±----------±-------------±-------------±-------+
8 rows in set (0.000 sec)

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

MariaDB [exp]> select
-> student.name as 姓名,
-> ((select DATE_FORMAT(NOW(), ‘%Y’)) - student.birth) as 年龄,
-> student.department as 院系,
-> score.c_name as 考试科目,
-> score.grade as 成绩
-> from student,score
-> where
-> student.address like ‘湖南%’
-> and student.id=score.stu_id;
±-------±-------±-------------±-------------±-------+
| 姓名 | 年龄 | 院系 | 考试科目 | 成绩 |
±-------±-------±-------------±-------------±-------+
| 张三 | 30 | 中文系 | 中文 | 95 |
| 王六 | 32 | 计算机系 | 英语 | 85 |
±-------±-------±-------------±-------------±-------+
2 rows in set (0.001 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值