MySQL数据库使用笔记(三):查询练习

本笔记来自bilibili:一天学会 MySQL 数据库
以及参考某位大佬的笔记
MySQL数据库使用笔记(一):终端使用与约束
MySQL数据库使用笔记(二):范式设计
MySQL数据库使用笔记(三):查询练习
MySQL数据库使用笔记(四):链接查询与事务

MySQL数据库使用笔记(三):查询

创建数据

首先创建一个database:create database selectTest;并选择:use selectTest;
学生表:Student(学号、姓名、性别、出生年月日、所在班级)

CREATE TABLE student(
    s_no VARCHAR(20) PRIMARY KEY COMMENT'学生学号',
    s_name VARCHAR(20) NOT NULL COMMENT'学生姓名 不能为空',
    s_sex VARCHAR(10) NOT NULL COMMENT'学生性别',
    s_birthday DATETIME COMMENT'学生生日',
    s_class VARCHAR(20) COMMENT'学生所在的班级'
);

教师表:Teacher(教师编号、教师名字、教师性别、出生年月日、职称、所在部门)

CREATE TABLE teacher(
    t_no VARCHAR(20) PRIMARY KEY COMMENT'教师编号',
    t_name VARCHAR(20) NOT NULL COMMENT'教师姓名',
    t_sex VARCHAR(20) NOT NULL COMMENT'教师性别',
    t_birthday DATETIME COMMENT'教师生日',
    t_rof VARCHAR(20) NOT NULL COMMENT'教师职称',
    t_depart VARCHAR(20) NOT NULL COMMENT'教师所在的部门'
);

课程表:Course(课程号、课程名称、教师编号)

CREATE TABLE course(
    c_no VARCHAR(20) PRIMARY KEY COMMENT'课程号',
    c_name VARCHAR(20) NOT NULL COMMENT'课程名称',
    t_no VARCHAR(20) NOT NULL COMMENT'教师编号 外键关联teacher表',
    FOREIGN KEY(t_no) references teacher(t_no)
);

成绩表:Score(学号、课程号、成绩)
注意:视频中原先只有一个主键s_no ,后来修改了

CREATE TABLE score (
    s_no VARCHAR(20) NOT NULL COMMENT'成绩表的编号 依赖学生学号',
        c_no VARCHAR(20)  NOT NULL COMMENT'课程号 依赖于课程表中的c_id',
    sc_degree decimal,
    foreign key(s_no) references student(s_no),
    foreign key(c_no) references course(c_no),
    PRIMARY KEY(s_no,c_no)
);

查看student表: desc student;

+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| s_no       | varchar(20) | NO   | PRI | NULL    |       |
| s_name     | varchar(20) | NO   |     | NULL    |       |
| s_sex      | varchar(10) | NO   |     | NULL    |       |
| s_birthday | datetime    | YES  |     | NULL    |       |
| s_class    | varchar(20) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

student表中插入学生数据:

insert into student values('101', '曾华', '男', '1977-09-01', '95033');
insert into student values('102', '匡明', '男', '1975-10-02', '95031');
insert into student values('103', '王丽', '女', '1976-01-23', '95033');
insert into student values('104', '李军', '男', '1976-02-20', '95033');
insert into student values('105', '王芳', '女', '1975-02-10', '95031');
insert into student values('106', '陆君', '男', '1974-06-03', '95031');
insert into student values('107', '王尼玛', '男', '1976-02-20', '95033');
insert into student values('108', '张全蛋', '男', '1975-02-10', '95031');
insert into student values('109', '赵铁柱', '男', '1974-06-03', '95031');

查看数据: select * from student;

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

查看teacher表:desc teacher;

+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| t_no       | varchar(20) | NO   | PRI | NULL    |       |
| t_name     | varchar(20) | NO   |     | NULL    |       |
| t_sex      | varchar(20) | NO   |     | NULL    |       |
| t_birthday | datetime    | YES  |     | NULL    |       |
| t_rof      | varchar(20) | NO   |     | NULL    |       |
| t_depart   | varchar(20) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

teacher表中插入教师信息:

INSERT INTO teacher VALUES('804','李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO teacher VALUES('856','张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO teacher VALUES('825','王萍','女','1972-05-05','助教','计算机系');
INSERT INTO teacher VALUES('831','刘冰','女','1977-08-14','助教','电子工程系');

查看数据:select * from teacher;

+------+--------+-------+---------------------+--------+------------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart   |
+------+--------+-------+---------------------+--------+------------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系   |
| 825  | 王萍   || 1972-05-05 00:00:00 | 助教   | 计算机系   |
| 831  | 刘冰   || 1977-08-14 00:00:00 | 助教   | 电子工程系 |
| 856  | 张旭   || 1969-03-12 00:00:00 | 讲师   | 电子工程系 |
+------+--------+-------+---------------------+--------+------------+

查看course表:desc course;

+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| c_no   | varchar(20) | NO   | PRI | NULL    |       |
| c_name | varchar(20) | NO   |     | NULL    |       |
| t_no   | varchar(20) | NO   | MUL | NULL    |       |
+--------+-------------+------+-----+---------+-------+

course表中添加课程信息:

INSERT INTO course VALUES('3-105','计算机导论','825');
INSERT INTO course VALUES('3-245','操作系统','804');
INSERT INTO course VALUES('6-166','数字电路','856');
INSERT INTO course VALUES('9-888','高等数学','831');

查看数据: select * from course;

+-------+------------+------+
| c_no  | c_name     | t_no |
+-------+------------+------+
| 3-105 | 计算机导论 | 825  |
| 3-245 | 操作系统   | 804  |
| 6-166 | 数字电路   | 856  |
+-------+------------+------+

查看score表:desc score;

+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| s_no      | varchar(20)   | NO   | PRI | NULL    |       |
| c_no      | varchar(20)   | NO   | PRI | NULL    |       |
| sc_degree | decimal(10,0) | YES  |     | NULL    |       |
+-----------+---------------+------+-----+---------+-------+

score表中添加成绩信息:

INSERT INTO score VALUES('103','3-245','86');
INSERT INTO score VALUES('105','3-245','75');
INSERT INTO score VALUES('109','3-245','68');
INSERT INTO score VALUES('103','3-105','92');
INSERT INTO score VALUES('105','3-105','88');
INSERT INTO score VALUES('109','3-105','76');
INSERT INTO score VALUES('103','6-166','85');
INSERT INTO score VALUES('105','6-166','79');
INSERT INTO score VALUES('109','6-166','81');

查看数据:select * from score;

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 103  | 3-245 |        86 |
| 103  | 6-166 |        85 |
| 105  | 3-105 |        88 |
| 105  | 3-245 |        75 |
| 105  | 6-166 |        79 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
| 109  | 6-166 |        81 |
+------+-------+-----------+

查询练习:

1、 查询s tudent表的所有记录。

输入:SELECT * FROM student;

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

2、 查询student表中的所有记录的sname、ssex和class列。

输入:SELECT s_no,s_name,s_class FROM student;

+------+--------+---------+
| s_no | s_name | s_class |
+------+--------+---------+
| 101  | 曾华   | 95033   |
| 102  | 匡明   | 95031   |
| 103  | 王丽   | 95033   |
| 104  | 李军   | 95033   |
| 105  | 王芳   | 95031   |
| 106  | 陆君   | 95031   |
| 107  | 王尼玛 | 95033   |
| 108  | 张全蛋 | 95031   |
| 109  | 赵铁柱 | 95031   |
+------+--------+---------+

3、 查询教师所有的单位即不重复的depart列。

输入:SELECT distinct (t_depart) FROM teacher;
distinct表示不重复、排除重复。

+------------+
| t_depart   |
+------------+
| 计算机系   |
| 电子工程系 |
+------------+

4、 查询score表中成绩在60到80之间的所有记录。

输入:
注意:BETWEEN… ADN… 是包含边界的

SELECT * FROM score WHERE sc_degree BETWEEN 60 AND 80;
SELECT * FROM score WHERE sc_degree < 80 AND sc_degree > 60 ; 
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 105  | 3-245 |        75 |
| 105  | 6-166 |        79 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
+------+-------+-----------+

5、 查询score表中成绩为85,86或88的记录。

这是表示一个的关系使用in,输入:SELECT * FROM score WHERE sc_degree IN(85, 86, 88);

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-245 |        86 |
| 103  | 6-166 |        85 |
| 105  | 3-105 |        88 |
+------+-------+-----------+

6、 查询student表中"95031"班或性别为“女”的同学记录。

使用or来表示或者(不同字段),输入:SELECT * FROM student WHERE s_class = '95031' OR s_sex = '女';

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

7、 以class降序查询student表的所有记录。

输入:SELECT * FROM student ORDER BY s_class desc;其中desc是降序,asc是升序。

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

8、 以cno升序、degree降序查询score表的所有记录。

先以c_no进行升序,若c_no相同,则以sc_degree降序。输入:SELECT * FROM score ORDER BY c_no ASC,sc_degree DESC;

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 105  | 3-105 |        88 |
| 109  | 3-105 |        76 |
| 103  | 3-245 |        86 |
| 105  | 3-245 |        75 |
| 109  | 3-245 |        68 |
| 103  | 6-166 |        85 |
| 109  | 6-166 |        81 |
| 105  | 6-166 |        79 |
+------+-------+-----------+

9、 查询“95031"班的学生人数。

统计使用count,输入:SELECT COUNT(s_no) FROM student WHERE s_class = '95031';

+-------------+
| COUNT(s_no) |
+-------------+
|           5 |
+-------------+

10、 查询score表中的最高分的学生学号和课程号。 (子查询或者排序)

输入:select s_no,c_no from score where sc_degree= (select max(sc_degree) from score);

+------+-------+
| s_no | c_no  |
+------+-------+
| 103  | 3-105 |
+------+-------+

补充:

  • 查询步骤

1.先找到最高分
最高分: SELECT MAX(sc_degree) FROM score;
2.通过我们找到的最高分的分数来从score中找到我们需要的学生号和课程号

SELECT c_no, s_no FROM score WHERE sc_degree = (最高分)
SELECT c_no, s_no FROM score WHERE sc_degree = (SELECT MAX(sc_degree) FROM score);
+-------+------+
| c_no  | s_no |
+-------+------+
| 3-105 | 103  |
+-------+------+
  • 排序步骤

排序 最高分有多个的情况下可能有数据问题
limit x,y (x:表示从X条数据开始 y:需要查出多少条)
SELECT c_no, s_no FROM score ORDER BY sc_degree DESC LIMIT 0,1;这个OK
但是我们再插入一条数据:

INSERT INTO score VALUES('101','9-888','92');

再用排序法去查得到:

+-------+------+
| c_no  | s_no |
+-------+------+
| 9-888 | 101  |
+-------+------+

有两条数据但是只显示一条,有问题
若我们用子查询的方法来查询的话会得到:

+-------+------+
| c_no  | s_no |
+-------+------+
| 9-888 | 101  |
| 3-105 | 103  |
+-------+------+

完全没有问题.
最后为了和视频数据一致删除刚刚插入的数据:

DELETE FROM score WHERE c_no = '9-888' AND s_no = '101';

11.查询每门课的平均成绩

使用avg(),比如:select avg(sc_degree) from score where c_no='3-105';
使用group by进行分组,就可以求得多组数据, 输入:SELECT c_no,AVG(sc_degree) FROM SCORE GROUP BY c_no;

+-------+----------------+
| c_no  | AVG(sc_degree) |
+-------+----------------+
| 3-105 |        85.3333 |
| 3-245 |        76.3333 |
| 6-166 |        81.6667 |
+-------+----------------+

12.查询score表中至少有2名学生选修的,并且以3开头的课程的平均分

HAVING COUNT 用来计算数量,like 来模糊查询(匹配格式)。
输入:SELECT AVG(sc_degree),c_no from score GROUP BY c_no HAVING COUNT(c_no) >= 2 AND c_no like '3%' ;

+----------------+-------+
| AVG(sc_degree) | c_no  |
+----------------+-------+
|        85.3333 | 3-105 |
|        76.3333 | 3-245 |
+----------------+-------+

13.查询分数大于70但是小于90的s_no列

输入:SELECT s_no,sc_degree FROM score WHERE sc_degree BETWEEN 70 AND 90;

+------+-----------+
| s_no | sc_degree |
+------+-----------+
| 103  |        86 |
| 103  |        85 |
| 105  |        88 |
| 105  |        75 |
| 105  |        79 |
| 109  |        76 |
| 109  |        81 |
+------+-----------+

进阶:显示s_name,c_name
输入:

select s_name,sc_degree,c_name 
FROM score,student,course 
WHERE score.s_no = student.s_no AND score.c_no = course.c_no AND sc_degree BETWEEN 70 AND 90;
+--------+-----------+------------+
| s_name | sc_degree | c_name     |
+--------+-----------+------------+
| 王丽   |        86 | 操作系统   |
| 王丽   |        85 | 数字电路   |
| 王芳   |        88 | 计算机导论 |
| 王芳   |        75 | 操作系统   |
| 王芳   |        79 | 数字电路   |
| 赵铁柱 |        76 | 计算机导论 |
| 赵铁柱 |        81 | 数字电路   |
+--------+-----------+------------+

14.查询所有的学生 s_name , c_no, sc_degree列

WHERE student.s_no = score.s_no;是加入匹配条件,输入:

SELECT s_name, c_no, sc_degree FROM student,score 
WHERE student.s_no = score.s_no;
+--------+-------+-----------+
| s_name | c_no  | sc_degree |
+--------+-------+-----------+
| 王丽   | 3-105 |        92 |
| 王丽   | 3-245 |        86 |
| 王丽   | 6-166 |        85 |
| 王芳   | 3-105 |        88 |
| 王芳   | 3-245 |        75 |
| 王芳   | 6-166 |        79 |
| 赵铁柱 | 3-105 |        76 |
| 赵铁柱 | 3-245 |        68 |
| 赵铁柱 | 6-166 |        81 |
+--------+-------+-----------+

15.查询所有学生的s_no, c_name, sc_degree列

加入条件:WHERE student.s_no = score.s_no AND score.c_no = course.c_no,输入:SELECT student.s_no, c_name, sc_degree FROM student, course,score WHERE student.s_no = score.s_no AND score.c_no = course.c_no ;

+------+------------+-----------+
| s_no | c_name     | sc_degree |
+------+------------+-----------+
| 103  | 计算机导论 |        92 |
| 103  | 操作系统   |        86 |
| 103  | 数字电路   |        85 |
| 105  | 计算机导论 |        88 |
| 105  | 操作系统   |        75 |
| 105  | 数字电路   |        79 |
| 109  | 计算机导论 |        76 |
| 109  | 操作系统   |        68 |
| 109  | 数字电路   |        81 |
+------+------------+-----------+

16.查询所有的学生 s_name , c_name, sc_degree列

s_namestudent中,c_namecourse中 ,sc_degreescore中。

将上面的c_no 的值换位c_name
引入条件:WHERE student.s_no = score.s_no AND score.c_no = course.c_no,输入:

SELECT s_name, c_name, sc_degree FROM student, course, score 
WHERE student.s_no = score.s_no AND score.c_no = course.c_no;

使用as可以重命名

+--------+------------+-----------+
| s_name | c_name     | sc_degree |
+--------+------------+-----------+
| 王丽   | 计算机导论 |        92 |
| 王丽   | 操作系统   |        86 |
| 王丽   | 数字电路   |        85 |
| 王芳   | 计算机导论 |        88 |
| 王芳   | 操作系统   |        75 |
| 王芳   | 数字电路   |        79 |
| 赵铁柱 | 计算机导论 |        76 |
| 赵铁柱 | 操作系统   |        68 |
| 赵铁柱 | 数字电路   |        81 |
+--------+------------+-----------+

17.查询班级是’95031’班学生每门课的平均分

查看95031班的数据:select * from student where s_class='95031';

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

可以通过语句:select s_no from student where s_class='95031';获取student中的s_no
因此可以使用in来获取score表中的对应s_no的成绩:

select * from score where s_no 
in  (select s_no  from student where s_class='95031');
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 105  | 3-105 |        88 |
| 105  | 3-245 |        75 |
| 105  | 6-166 |        79 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
| 109  | 6-166 |        81 |
+------+-------+-----------+

按班级获得平均成绩:

select c_no, avg(sc_degree)
from score
where s_no in  (select s_no  from student where s_class='95031');
group by c_no;
+-------+----------------+
| c_no  | avg(sc_degree) |
+-------+----------------+
| 3-105 |        77.8333 |
+-------+----------------+

其他的查询方式:

select c_no,AVG(sc_degree) 
from score AS sc LEFT JOIN student AS s ON sc.s_no = s.s_no 
WHERE s.s_class = '95031' 
GROUP BY sc.c_no;

SELECT sc.c_no,AVG(sc.sc_degree) 
FROM student AS s, score AS SC  
WHERE s.s_class = '95031' AND s.s_no = sc.s_no 
GROUP BY sc.c_no ;
+-------+-------------------+
| c_no  | AVG(sc.sc_degree) |
+-------+-------------------+
| 3-105 |           82.0000 |
| 3-245 |           71.5000 |
| 6-166 |           80.0000 |
+-------+-------------------+

进阶**,加入课程名称**:

SELECT sc.c_no, c.c_name, AVG(sc.sc_degree) 
FROM student AS s, score AS SC, course AS c 
WHERE s.s_class = '95031' AND s.s_no = sc.s_no AND c.c_no = sc.c_no 
GROUP BY sc.c_no ;
+-------+------------+-------------------+
| c_no  | c_name     | AVG(sc.sc_degree) |
+-------+------------+-------------------+
| 3-105 | 计算机导论 |           82.0000 |
| 3-245 | 操作系统   |           71.5000 |
| 6-166 | 数字电路   |           80.0000 |
+-------+------------+-------------------+

18.查询选修"3-105"课程的成绩高于’109’号同学’3-105’成绩 的所有同学的记录

在大家都在选修3-105的背景下 查询 所有 分数 比 学号为"109"还要高的学生信息
首先选择’109’号同学’3-105’成绩:select sc_degree from score where s_no='109' and c_no='3-105';

+-----------+
| sc_degree |
+-----------+
|        76 |
+-----------+

接着查询成绩比他高的同学:

select * from score
where sc_degree > (select sc_degree from score where s_no='109' and c_no='3-105');
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 103  | 3-245 |        86 |
| 103  | 6-166 |        85 |
| 105  | 3-105 |        88 |
| 105  | 6-166 |        79 |
| 109  | 6-166 |        81 |
+------+-------+-----------+

再加上限定条件where c_no='3-105'

select * from score
where c_no='3-105' and sc_degree > (select sc_degree from score where s_no='109' and c_no='3-105');
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 105  | 3-105 |        88 |
+------+-------+-----------+

19.查询成绩高于学号为’109’,课程号为’3-105’的成绩的所有记录

和18中的一步操作一致:

select * from score
where sc_degree > (select sc_degree from score where s_no='109' and c_no='3-105');

不过视频中仅仅查出来了score记录,但是并没有学生的信息,按照上面的来修改:

SELECT * FROM student AS s, score AS sc 
WHERE sc.sc_degree > (SELECT sc_degree FROM score WHERE s_no = '109' AND c_no = '3-105') AND s.s_no = sc.s_no;

还可以再进一步:

SELECT s.s_no AS'学生学号', s.s_name AS'学生姓名', s_sex AS'性别', s_class AS'班级', c.c_no AS'课程编号', c.c_name AS'课程名称' ,sc.sc_degree AS'分数' 
FROM student AS s, score AS sc ,course AS c 
WHERE sc.sc_degree > (SELECT sc_degree FROM score WHERE s_no = '109' AND c_no = '3-105') AND s.s_no = sc.s_no AND sc.c_no = c.c_no;
+----------+----------+------+-------+----------+------------+------+
| 学生学号 | 学生姓名 | 性别 | 班级  | 课程编号 | 课程名称   | 分数 |
+----------+----------+------+-------+----------+------------+------+
| 103      | 王丽     || 95033 | 3-105    | 计算机导论 |   92 |
| 103      | 王丽     || 95033 | 3-245    | 操作系统   |   86 |
| 103      | 王丽     || 95033 | 6-166    | 数字电路   |   85 |
| 105      | 王芳     || 95031 | 3-105    | 计算机导论 |   88 |
| 105      | 王芳     || 95031 | 6-166    | 数字电路   |   79 |
| 109      | 赵铁柱   || 95031 | 6-166    | 数字电路   |   81 |
+----------+----------+------+-------+----------+------------+------+

20.查询所有学号为108.101的同学同年出生的所有学生的s_no,s_name和s_birthday

student表中查询108,101的s_birthdayselect year(s_birthday) from student where s_no in (108, 101);

+------------------+
| year(s_birthday) |
+------------------+
|             1977 |
|             1975 |
+------------------+

接下来输入:

SELECT * FROM student 
WHERE YEAR(s_birthday) 
IN (SELECT YEAR(s_birthday) FROM student WHERE s_no IN('108','101'));
+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

21.查询 张旭 教师任课的学生的成绩

首先查询张旭的教师id:select t_no from teacher where t_name = '张旭';

+------+
| t_no |
+------+
| 856  |
+------+

获取张旭老师教课程的id:

select c_no from course 
where t_no=(select t_no from teacher where t_name = '张旭');
+-------+
| c_no  |
+-------+
| 6-166 |
+-------+

接下来获取这门课程所有学生的成绩:

select * from score
where c_no=(select c_no from course 
where t_no=(select t_no from teacher where t_name = '张旭'));
+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 6-166 |        85 |
| 105  | 6-166 |        79 |
| 109  | 6-166 |        81 |
+------+-------+-----------+

22.查询选修课程的同学人数多余 5 人的教师姓名

为了效果,添加数据:

INSERT INTO score VALUES('101','3-105','90');
INSERT INTO score VALUES('102','3-105','91');
INSERT INTO score VALUES('104','3-105','89');

首先查询人数多余5人的课程id:select c_no from score group by c_no having count(*)>5;

+-------+
| c_no  |
+-------+
| 3-105 |
+-------+

接下来查询教这门课程的老师id:

select t_no from course 
where c_no=(select c_no from score group by c_no having count(*)>5);

+------+
| t_no |
+------+
| 825  |
+------+

再根据老师id获取老师名字:

SELECT * FROM teacher 
WHERE t_no 
IN(SELECT t_no FROM course WHERE c_no IN (SELECT c_no FROM score GROUP BY c_no HAVING COUNT(s_no) > 5));

+------+--------+-------+---------------------+-------+----------+
| t_no | t_name | t_sex | t_birthday          | t_rof | t_depart |
+------+--------+-------+---------------------+-------+----------+
| 825  | 王萍   || 1972-05-05 00:00:00 | 助教  | 计算机系 |
+------+--------+-------+---------------------+-------+----------+

注意:视频中用 = 是不严谨的,实际中你根本不知道有多少条件是符合的,要用IN

23.查询95033班和95031班全体学生的记录

由于视频中就只有这两个班,所以要插入数据:INSERT INTO student VALUES('110','张飞','男','1974-06-03','95038');
输入:

SELECT * FROM student WHERE s_class IN('95031','95033') ORDER BY s_class ;

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
+------+--------+-------+---------------------+---------+

进阶:查询95033班和95031班全体学生每门课的成绩以及负责该课程的老师,最后以class来排序

SELECT s.s_no, s.s_name,s.s_birthday,s.s_class, c.c_no, c.c_name, sc.sc_degree , t.t_name 
FROM student AS s, course AS c, score AS sc,teacher AS t 
WHERE  s.s_class IN('95031','95033') AND s.s_no = sc.s_no AND sc.c_no = c.c_no AND c.t_no = t.t_no;

+------+--------+---------------------+---------+-------+------------+-----------+--------+
| s_no | s_name | s_birthday          | s_class | c_no  | c_name     | sc_degree | t_name |
+------+--------+---------------------+---------+-------+------------+-----------+--------+
| 101  | 曾华   | 1977-09-01 00:00:00 | 95033   | 3-105 | 计算机导论 |        90 | 王萍   |
| 102  | 匡明   | 1975-10-02 00:00:00 | 95031   | 3-105 | 计算机导论 |        91 | 王萍   |
| 103  | 王丽   | 1976-01-23 00:00:00 | 95033   | 3-105 | 计算机导论 |        92 | 王萍   |
| 104  | 李军   | 1976-02-20 00:00:00 | 95033   | 3-105 | 计算机导论 |        89 | 王萍   |
| 105  | 王芳   | 1975-02-10 00:00:00 | 95031   | 3-105 | 计算机导论 |        88 | 王萍   |
| 109  | 赵铁柱 | 1974-06-03 00:00:00 | 95031   | 3-105 | 计算机导论 |        76 | 王萍   |
| 103  | 王丽   | 1976-01-23 00:00:00 | 95033   | 3-245 | 操作系统   |        86 | 李诚   |
| 105  | 王芳   | 1975-02-10 00:00:00 | 95031   | 3-245 | 操作系统   |        75 | 李诚   |
| 109  | 赵铁柱 | 1974-06-03 00:00:00 | 95031   | 3-245 | 操作系统   |        68 | 李诚   |
| 103  | 王丽   | 1976-01-23 00:00:00 | 95033   | 6-166 | 数字电路   |        85 | 张旭   |
| 105  | 王芳   | 1975-02-10 00:00:00 | 95031   | 6-166 | 数字电路   |        79 | 张旭   |
| 109  | 赵铁柱 | 1974-06-03 00:00:00 | 95031   | 6-166 | 数字电路   |        81 | 张旭   |
+------+--------+---------------------+---------+-------+------------+-----------+--------+

24.查询存在85分以上成绩的课程c_no

输入:

SELECT * FROM score where sc_degree > 85;

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 101  | 3-105 |        90 |
| 102  | 3-105 |        91 |
| 103  | 3-105 |        92 |
| 103  | 3-245 |        86 |
| 104  | 3-105 |        89 |
| 105  | 3-105 |        88 |
+------+-------+-----------+

进阶:知道c_no还不行,还需要看到c_name 以及对应的老师

SELECT sc.c_no,c.c_name, t.t_name 
FROM score AS sc, course AS c, teacher AS t 
WHERE sc.c_no IN(SELECT c_no FROM score where sc_degree > 85) AND sc.c_no = c.c_no AND c.t_no = t.t_no 
GROUP BY c.c_name;

+-------+------------+--------+
| c_no  | c_name     | t_name |
+-------+------------+--------+
| 3-105 | 计算机导论 | 王萍   |
| 3-245 | 操作系统   | 李诚   |
+-------+------------+--------+
--这两位老师教出来的学生都有85分以上的

25.查出所有’计算机系’ 教师所教课程的成绩表

首先查看教计算机系的老师id:

select * from teacher where t_depart='计算机系';

+------+--------+-------+---------------------+--------+----------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart |
+------+--------+-------+---------------------+--------+----------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系 |
| 825  | 王萍   || 1972-05-05 00:00:00 | 助教   | 计算机系 |
+------+--------+-------+---------------------+--------+----------+

根据老师id可以获得班级id:

select * from course 
where t_no in (select t_no from teacher where t_depart='计算机系');

+-------+------------+------+
| c_no  | c_name     | t_no |
+-------+------------+------+
| 3-105 | 计算机导论 | 825  |
| 3-245 | 操作系统   | 804  |
+-------+------------+------+

根据班级id获得成绩:

SELECT * FROM score 
WHERE c_no 
IN (SELECT c_no FROM course WHERE t_no IN (SELECT t_no FROM teacher WHERE t_depart = '计算机系'));

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 101  | 3-105 |        90 |
| 102  | 3-105 |        91 |
| 103  | 3-105 |        92 |
| 103  | 3-245 |        86 |
| 104  | 3-105 |        89 |
| 105  | 3-105 |        88 |
| 105  | 3-245 |        75 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
+------+-------+-----------+

进阶:

SELECT  t.t_name,t.t_depart,c.c_name,s.s_name,s_class,sc.sc_degree 
FROM course AS c, score AS sc, teacher AS t,student AS s 
WHERE c.t_no 
IN (select t_no FROM teacher WHERE t_depart = '计算机系') AND c.t_no = t.t_no AND c.c_no = sc.c_no AND sc.s_no = s.s_no ;

+--------+----------+------------+--------+---------+-----------+
| t_name | t_depart | c_name     | s_name | s_class | sc_degree |
+--------+----------+------------+--------+---------+-----------+
| 王萍   | 计算机系 | 计算机导论 | 曾华   | 95033   |        90 |
| 王萍   | 计算机系 | 计算机导论 | 匡明   | 95031   |        91 |
| 王萍   | 计算机系 | 计算机导论 | 王丽   | 95033   |        92 |
| 王萍   | 计算机系 | 计算机导论 | 李军   | 95033   |        89 |
| 王萍   | 计算机系 | 计算机导论 | 王芳   | 95031   |        88 |
| 王萍   | 计算机系 | 计算机导论 | 赵铁柱 | 95031   |        76 |
| 李诚   | 计算机系 | 操作系统   | 王丽   | 95033   |        86 |
| 李诚   | 计算机系 | 操作系统   | 王芳   | 95031   |        75 |
| 李诚   | 计算机系 | 操作系统   | 赵铁柱 | 95031   |        68 |
+--------+----------+------------+--------+---------+-----------+

26.查询’计算机系’与’电子工程系’ 不同职称的教师的name和rof

查询计算机系中的职称,且该职称电子工程系没有

select * from teacher 
where t_depart='计算机系' and t_rof not in (select t_rof from teacher where t_depart='电子工程系');

+------+--------+-------+---------------------+--------+----------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart |
+------+--------+-------+---------------------+--------+----------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系 |
+------+--------+-------+---------------------+--------+----------+

同理,查询电子工程系中的职称,且该职称计算机系没有

select * from teacher 
where t_depart='电子工程系' and t_rof not in (select t_rof from teacher where t_depart='计算机系');

+------+--------+-------+---------------------+-------+------------+
| t_no | t_name | t_sex | t_birthday          | t_rof | t_depart   |
+------+--------+-------+---------------------+-------+------------+
| 856  | 张旭   || 1969-03-12 00:00:00 | 讲师  | 电子工程系 |
+------+--------+-------+---------------------+-------+------------+

使用union,拼接这两个结果:

select * from teacher 
where t_depart='计算机系' and t_rof not in (select t_rof from teacher where t_depart='电子工程系')
union
select * from teacher 
where t_depart='电子工程系' and t_rof not in (select t_rof from teacher where t_depart='计算机系');

+------+--------+-------+---------------------+--------+------------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart   |
+------+--------+-------+---------------------+--------+------------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系   |
| 856  | 张旭   || 1969-03-12 00:00:00 | 讲师   | 电子工程系 |
+------+--------+-------+---------------------+--------+------------+

27, 查询选修编号为"3-105"课程且成绩至少高于选修编号为’3-245’同学的c_no,s_no和sc_degree,并且按照sc_degree从高到地次序排序

select * from score 
where c_no = '3-105' 
AND sc_degree > ANY(SELECT sc_degree FROM score WHERE c_no = '3-245' ) 
ORDER BY sc_degree desc ;

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
| 102  | 3-105 |        91 |
| 101  | 3-105 |        90 |
| 104  | 3-105 |        89 |
| 105  | 3-105 |        88 |
| 109  | 3-105 |        76 |
+------+-------+-----------+

28.查询选修编号为"3-105"且成绩高于选修编号为"3-245"课程的同学c_no.s_no和sc_degree

all表示所有。

SELECT * FROM score 
WHERE sc_degree > ALL (select sc_degree from score WHERE c_no = '3-245') 
AND c_no = '3-105';

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 101  | 3-105 |        90 |
| 102  | 3-105 |        91 |
| 103  | 3-105 |        92 |
| 104  | 3-105 |        89 |
| 105  | 3-105 |        88 |
+------+-------+-----------+

进阶:查出学生的信息,课程名称,分数(s_name c_name,sc_degree)

SELECT s.s_name , c.c_name ,sc.sc_degree 
FROM score AS sc, student AS s,course AS c 
WHERE sc_degree > ALL (select sc_degree from score WHERE c.c_no = '3-245') 
AND c.c_no = '3-105' AND sc.s_no = s.s_no AND sc.c_no = c.c_no ;

+--------+------------+-----------+
| s_name | c_name     | sc_degree |
+--------+------------+-----------+
| 曾华   | 计算机导论 |        90 |
| 匡明   | 计算机导论 |        91 |
| 王丽   | 计算机导论 |        92 |
| 李军   | 计算机导论 |        89 |
| 王芳   | 计算机导论 |        88 |
| 赵铁柱 | 计算机导论 |        76 |
+--------+------------+-----------+

总结: ANYALL

  • ANY:表示任何一个就行了,如;数组A中的值比数组B中任何一个都要大,那么只要A和B中最小的比较就行了.
  • ALL:表示所有都要比较,如:数组A中的值比数组B中所有的数都要大,那么A要和B中最大的值比较才行

29. 查询所有教师和同学的 name ,sex, birthday

SELECT s_name AS name, s_sex AS sex, s_birthday AS birthday  FROM student
UNION
SELECT t_name , t_sex , t_birthday  FROM teacher; --第二排会默认的匹配第一排

+--------+-----+---------------------+
| name   | sex | birthday            |
+--------+-----+---------------------+
| 曾华   || 1977-09-01 00:00:00 |
| 匡明   || 1975-10-02 00:00:00 |
| 王丽   || 1976-01-23 00:00:00 |
| 李军   || 1976-02-20 00:00:00 |
| 王芳   || 1975-02-10 00:00:00 |
| 陆君   || 1974-06-03 00:00:00 |
| 王尼玛 || 1976-02-20 00:00:00 |
| 张全蛋 || 1975-02-10 00:00:00 |
| 赵铁柱 || 1974-06-03 00:00:00 |
| 张飞   || 1974-06-03 00:00:00 |
| 李诚   || 1958-12-02 00:00:00 |
| 王萍   || 1972-05-05 00:00:00 |
| 刘冰   || 1977-08-14 00:00:00 |
| 张旭   || 1969-03-12 00:00:00 |
+--------+-----+---------------------+

30.查询所有’女’教师和’女’学生的name,sex,birthday

在上一部分加入限制条件where即可

SELECT s_name AS name, s_sex AS sex, s_birthday AS birthday  FROM student WHERE s_sex = '女'
UNION
SELECT t_name AS name, t_sex AS sex, t_birthday AS birthday FROM teacher WHERE t_sex = '女';

+------+-----+---------------------+
| name | sex | birthday            |
+------+-----+---------------------+
| 王丽 || 1976-01-23 00:00:00 |
| 王芳 || 1975-02-10 00:00:00 |
| 王萍 || 1972-05-05 00:00:00 |
| 刘冰 || 1977-08-14 00:00:00 |
+------+-----+---------------------+

31.查询成绩比该课程平均成绩低的同学的成绩表

首先查看各班级的平均成绩:

select c_no, avg(sc_degree) from score
group by c_no;

+-------+----------------+
| c_no  | avg(sc_degree) |
+-------+----------------+
| 3-105 |        87.6667 |
| 3-245 |        76.3333 |
| 6-166 |        81.6667 |
+-------+----------------+

拆分来看,需要把成绩表拆分成两份,一份是原始成绩,一份是计算的平均成绩,然后以平均成绩作为筛选条件,在原始成绩中找到比平均成绩低的同学。

SELECT * FROM score AS sc1 /*原始成绩表*/
WHERE sc1.sc_degree <  /*在获取原始成绩表中找到比平均成绩低的id*/
(SELECT  AVG(sc_degree) FROM score AS sc2 /*平均成绩表*/
WHERE sc1.c_no = sc2.c_no);/*id一致的同学*/

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 105  | 3-245 |        75 |
| 105  | 6-166 |        79 |
| 109  | 3-105 |        76 |
| 109  | 3-245 |        68 |
| 109  | 6-166 |        81 |
+------+-------+-----------+

进阶:显示出学生name,课程name以及分数

SELECT s.s_name ,sc1.c_no,c.c_name, sc1.sc_degree 
FROM score AS sc1,student AS s,course AS c 
WHERE sc1.sc_degree < 
(SELECT  AVG(sc_degree) FROM score AS sc2 WHERE sc1.c_no = sc2.c_no) 
AND sc1.s_no = s.s_no AND sc1.c_no = c.c_no ;

+--------+-------+------------+-----------+
| s_name | c_no  | c_name     | sc_degree |
+--------+-------+------------+-----------+
| 王芳   | 3-245 | 操作系统   |        75 |
| 王芳   | 6-166 | 数字电路   |        79 |
| 赵铁柱 | 3-105 | 计算机导论 |        76 |
| 赵铁柱 | 3-245 | 操作系统   |        68 |
| 赵铁柱 | 6-166 | 数字电路   |        81 |
+--------+-------+------------+-----------+

32.查询所有任课教师的t_name 和 t_depart(要在分数表中可以查得到)

SELECT t_name,t_depart FROM teacher /*根据课程表老师的id获取老师的信息*/
WHERE t_no IN(SELECT t_no FROM course);/*在课程表中获取老师的id*/

+--------+------------+
| t_name | t_depart   |
+--------+------------+
| 李诚   | 计算机系   |
| 王萍   | 计算机系   |
| 刘冰   | 电子工程系 |
| 张旭   | 电子工程系 |
+--------+------------+

如果考虑有课程成绩才算任课的话:从score表中查出有过考试成绩的课程,再用该课程查出教师的信息

SELECT * FROM teacher
WHERE t_no 
IN(SELECT t_no FROM course WHERE c_no IN (SELECT c_no from score));

+------+--------+-------+---------------------+--------+------------+
| t_no | t_name | t_sex | t_birthday          | t_rof  | t_depart   |
+------+--------+-------+---------------------+--------+------------+
| 804  | 李诚   || 1958-12-02 00:00:00 | 副教授 | 计算机系   |
| 825  | 王萍   || 1972-05-05 00:00:00 | 助教   | 计算机系   |
| 856  | 张旭   || 1969-03-12 00:00:00 | 讲师   | 电子工程系 |
+------+--------+-------+---------------------+--------+------------+

33.查出至少有2名男生的班号

SELECT s_class FROM student 
WHERE s_sex = '男' 
GROUP BY s_class HAVING COUNT(s_no) > 1; 

+---------+
| s_class |
+---------+
| 95031   |
| 95033   |
+---------+

34.查询student 表中 不姓"王"的同学的记录

SELECT * FROM student 
WHERE s_name NOT LIKE '王%';

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
| 110  | 张飞   || 1974-06-03 00:00:00 | 95038   |
+------+--------+-------+---------------------+---------+

35. 查询student 中每个学生的姓名和年龄(当前时间 - 出生年份)

SELECT s_name, YEAR(NOW()) - YEAR(s_birthday) AS age FROM student;

+--------+------+
| s_name | age  |
+--------+------+
| 曾华   |   43 |
| 匡明   |   45 |
| 王丽   |   44 |
| 李军   |   44 |
| 王芳   |   45 |
| 陆君   |   46 |
| 王尼玛 |   44 |
| 张全蛋 |   45 |
| 赵铁柱 |   46 |
| 张飞   |   46 |
+--------+------+

36. 查询student中最大和最小的 s_birthday的值

SELECT MAX(s_birthday),MIN(s_birthday) FROM student;

+---------------------+---------------------+
| MAX(s_birthday)     | MIN(s_birthday)     |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |
+---------------------+---------------------+

37.以班级号和年龄从大到小的顺序查询student表中的全部记录

SELECt * FROM student ORDER BY s_class DESC, s_birthday;

+------+--------+-------+---------------------+---------+
| s_no | s_name | s_sex | s_birthday          | s_class |
+------+--------+-------+---------------------+---------+
| 110  | 张飞   || 1974-06-03 00:00:00 | 95038   |
| 103  | 王丽   || 1976-01-23 00:00:00 | 95033   |
| 107  | 王尼玛 || 1976-02-20 00:00:00 | 95033   |
| 104  | 李军   || 1976-02-20 00:00:00 | 95033   |
| 101  | 曾华   || 1977-09-01 00:00:00 | 95033   |
| 109  | 赵铁柱 || 1974-06-03 00:00:00 | 95031   |
| 106  | 陆君   || 1974-06-03 00:00:00 | 95031   |
| 105  | 王芳   || 1975-02-10 00:00:00 | 95031   |
| 108  | 张全蛋 || 1975-02-10 00:00:00 | 95031   |
| 102  | 匡明   || 1975-10-02 00:00:00 | 95031   |
+------+--------+-------+---------------------+---------+

38.查询"男"教师 及其所上的课

SELECT * FROM course 
WHERE t_no IN 
(SELECT t_no FROM teacher WHERE t_sex = '男');/*在teacher表查询老师的id*/

+-------+----------+------+
| c_no  | c_name   | t_no |
+-------+----------+------+
| 3-245 | 操作系统 | 804  |
| 6-166 | 数字电路 | 856  |
+-------+----------+------+

进阶:查出教师名称,教师性别, 课程名字

SELECT t.t_name,t.t_sex ,c.c_name 
FROM teacher t ,course c 
WHERE t_sex = '男' AND t.t_no = c.t_no;

+--------+-------+----------+
| t_name | t_sex | c_name   |
+--------+-------+----------+
| 李诚   || 操作系统 |
| 张旭   || 数字电路 |
+--------+-------+----------+

39.查询最高分同学的s_no c_no 和 sc_degree

SELECT * FROM score 
WHERE sc_degree = (select MAX(sc_degree) AS sc_degree FROM score);

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 103  | 3-105 |        92 |
+------+-------+-----------+

40. 查询和"李军"同性别的所有同学的s_name

SELECT s_name, s_sex FROM student 
WHERE s_SEX = (SELECT s_sex FROM student WHERE s_name = '李军');

+--------+-------+
| s_name | s_sex |
+--------+-------+
| 曾华   ||
| 匡明   ||
| 李军   ||
| 陆君   ||
| 王尼玛 ||
| 张全蛋 ||
| 赵铁柱 ||
| 张飞   ||
+--------+-------+

41.查询和"李军"同性别并且同班的所有同学的s_name

SELECT s_name, s_sex FROM student 
WHERE s_sex = (SELECT s_sex FROM student WHERE s_name = '李军') 
AND s_class = (SELECT s_class FROM student WHERE s_name = '李军');
/*或者*/
SELECT s_name, s_sex FROM student s1 
WHERE s_sex = (SELECT s_sex FROM student s2 WHERE s_name = '李军' 
               AND s1.s_class = s2.s_class);

+--------+-------+
| s_name | s_sex |
+--------+-------+
| 曾华   ||
| 李军   ||
| 王尼玛 ||
+--------+-------+

42. 查询所有选修’计算机导论’课程的’男’同学的成绩表

SELECT * FROM score 
WHERE c_no = (SELECT c_no FROM course WHERE c_name = '计算机导论' ) 
AND s_no IN(SELECT s_no FROM student WHERE s_sex = '男');

+------+-------+-----------+
| s_no | c_no  | sc_degree |
+------+-------+-----------+
| 101  | 3-105 |        90 |
| 102  | 3-105 |        91 |
| 104  | 3-105 |        89 |
| 109  | 3-105 |        76 |
+------+-------+-----------+

进阶:显示出s_name,c_name 替代之前的 s_noc_no

SELECT s_name,c_name,sc_degree 
FROM score,student,course 
WHERE score.c_no = (SELECT c_no FROM course WHERE c_name = '计算机导论' ) 
AND score.s_no IN(SELECT s_no FROM student WHERE s_sex = '男') 
AND score.s_no = student.s_no AND score.c_no = course.c_no ;

+--------+------------+-----------+
| s_name | c_name     | sc_degree |
+--------+------------+-----------+
| 曾华   | 计算机导论 |        90 |
| 匡明   | 计算机导论 |        91 |
| 李军   | 计算机导论 |        89 |
| 赵铁柱 | 计算机导论 |        76 |
+--------+------------+-----------+

43. 假设使用了以下命令建立了一个grade表

CREATE TABLE grade(
    low INT(3),
    upp INT(3),
    grade CHAR(1)
);
INSERT INTO grade VALUES(90,100,'A');
INSERT INTO grade VALUES(80,89,'B');
INSERT INTO grade VALUES(70,79,'c');
INSERT INTO grade VALUES(60,69,'D');
INSERT INTO grade VALUES(0,59,'E');
  • 查询所有同学的s_no , c_no 和grade列
SELECT s_no, c_no , grade FROM score, grade 
WHERE sc_degree BETWEEN low and upp; 

+------+-------+-------+
| s_no | c_no  | grade |
+------+-------+-------+
| 101  | 3-105 | A     |
| 102  | 3-105 | A     |
| 103  | 3-105 | A     |
| 103  | 3-245 | B     |
| 103  | 6-166 | B     |
| 104  | 3-105 | B     |
| 105  | 3-105 | B     |
| 105  | 3-245 | c     |
| 105  | 6-166 | c     |
| 109  | 3-105 | c     |
| 109  | 3-245 | D     |
| 109  | 6-166 | B     |
+------+-------+-------+

进阶:显示学生名字和课程名称

SELECT  s.s_no, s.s_name, c.c_name ,c.c_no , grade 
FROM student s, course c ,score sc, grade 
WHERE sc_degree BETWEEN low and upp AND sc.s_no = s.s_no AND sc.c_no = c.c_no;

+------+--------+------------+-------+-------+
| s_no | s_name | c_name     | c_no  | grade |
+------+--------+------------+-------+-------+
| 101  | 曾华   | 计算机导论 | 3-105 | A     |
| 102  | 匡明   | 计算机导论 | 3-105 | A     |
| 103  | 王丽   | 计算机导论 | 3-105 | A     |
| 103  | 王丽   | 操作系统   | 3-245 | B     |
| 103  | 王丽   | 数字电路   | 6-166 | B     |
| 104  | 李军   | 计算机导论 | 3-105 | B     |
| 105  | 王芳   | 计算机导论 | 3-105 | B     |
| 105  | 王芳   | 操作系统   | 3-245 | c     |
| 105  | 王芳   | 数字电路   | 6-166 | c     |
| 109  | 赵铁柱 | 计算机导论 | 3-105 | c     |
| 109  | 赵铁柱 | 操作系统   | 3-245 | D     |
| 109  | 赵铁柱 | 数字电路   | 6-166 | B     |
+------+--------+------------+-------+-------+

你可能感兴趣的:(数据库)