数据库-mysql查询练习


title: 数据库-mysql查询练习
date: 2021-11-21 20:39:00
tags:


数据库-mysql查询练习

数据准备

新建一个查询用的数据库:selectTest
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'学生所在的班级'
);


课程表:
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)
);

报错:Failed to open the referenced table 'teacher'
我们先创建teacher表


教师表
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'教师所在的部门'
);


成绩表
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)
);
查看创建的表以及架构
SHOW TABLES;
+----------------------+
| Tables_in_selecttest |
+----------------------+
| course               |
| score                |
| student              |
| teacher              |
+----------------------+

查看student表结构    DESCRIBE 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    |       |
+------------+-------------+------+-----+---------+-------+

查看teacher表结构    DESCRIBE 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    |       |
+------------+-------------+------+-----+---------+-------+

查看course表结构    DESCRIBE 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    |       |
+--------+-------------+------+-----+---------+-------+

查看score表结构    DESCRIBE score;
+-----------+---------------+------+-----+---------+-------+
| Field     | Type          | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| s_no      | varchar(20)   | NO   | PRI | NULL    |       |
| c_no      | varchar(20)   | NO   | MUL | NULL    |       |
| sc_degree | decimal(10,0) | YES  |     | NULL    |       |
+-----------+---------------+------+-----+---------+-------+
向表中添加数据

--学生表数据
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');

--教师表数据
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','助教','电子工程系');

--添加课程表
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');

--添加成绩表
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');

查询

1.查询student表中所有的记录
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表中所有记录的s_name,s_sex和s_class列
select s_name, s_sex, s_class from student;

±-------±------±--------+
| s_name | s_sex | s_class |
±-------±------±--------+
| 曾华 | 男 | 95033 |
| 匡明 | 男 | 95031 |
| 王丽 | 女 | 95033 |
| 李军 | 男 | 95033 |
| 王芳 | 女 | 95031 |
| 陆军 | 男 | 95031 |
| 王尼玛 | 男 | 95033 |
| 张全蛋 | 男 | 95031 |
| 赵铁柱 | 男 | 95031 |
±-------±------±--------+

3.查询教师所有的单位但是不重复的t_depart列
distinct排除重复

select t_depart from teacher;
±-----------+
| t_depart |
±-----------+
| 计算机系 |
| 计算机系 |
| 电子工程系 |
| 电子工程系 |
±-----------+

select distinct t_depart from teacher;

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

4.查询score表中成绩在60-80之间所有的记录(sc_degree)
注意:BETWEEN… ADN… 是包含边界的
select * from score where sc_degree between 60 and 80;

或者

select * from score where sc_degree > 60 and sc_degree < 80;

±-----±------±----------+
| 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的记录(sc_degree)
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’班或者性别为’女’的同学记录
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表中所有的记录
升序asc、降序desc,默认就是升序的,所以升序可以不用写asc
select * from student order by s_class desc;

±-----±-------±------±--------------------±--------+
| 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 |
±-----±-------±------±--------------------±--------+

select * from student order by s_class;

或者

select * from student order by s_class asc;

±-----±-------±------±--------------------±--------+
| 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 |
±-----±-------±------±--------------------±--------+

8.以c_no升序.sc_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(*) from student where s_class = ‘95031’;

或者

select count(s_no) from student where s_class = ‘95031’;

±---------+
| count(*) |
±---------+
| 5 |
±---------+

10.查询score表中的最高分数的学生号和课程号.(子查询或者排序)
select s_no, c_no from score where sc_degree = (select max(sc_degree) from score);

拆解:

select max(sc_degree) from score //找到最高分

select s_no, c_no from score where sc_degree = (select max(sc_degree) from score);
±-----±------+
| s_no | c_no |
±-----±------+
| 103 | 3-105 |
±-----±------+
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 | |
±----------±--------------±-----±----±--------±------+

或者用排序,但是排序的话有两个或多个最高分会不能全部找到
limit x,y (x:表示从X条数据开始 y:需要查出多少条)

select s_no, c_no, sc_degree from score order by sc_degree;

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

select s_no, c_no, sc_degree from score order by sc_degree limit 0, 1;

±-----±------±----------+
| s_no | c_no | sc_degree |
±-----±------±----------+
| 109 | 3-245 | 68 |
±-----±------±----------+
select s_no, c_no, sc_degree from score order by sc_degree limit 0, 2;
±-----±------±----------+
| s_no | c_no | sc_degree |
±-----±------±----------+
| 109 | 3-245 | 68 |
| 105 | 3-245 | 75 |
±-----±------±----------+

11.查询每门课的平均成绩
group by //按…进行分组

select * from course;
±------±-----------±-----+
| c_no | c_name | t_no |
±------±-----------±-----+
| 3-105 | 计算机导论 | 825 |
| 3-245 | 操作系统 | 804 |
| 6-166 | 数字电路 | 856 |
| 9-888 | 高等数学 | 831 |
±------±-----------±-----+

select avg(sc_degree) from score where c_no=‘3-105’; //这是算一条特定的

±---------------+
| avg(sc_degree) |
±---------------+
| 85.3333 |
±---------------+

select avg(sc_degree) from score group by c_no; //一起算

±---------------+
| avg(sc_degree) |
±---------------+
| 85.3333 |
| 76.3333 |
| 81.6667 |
±---------------+

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开头的课程的平均分
like //模糊查询
'3% //以3开头,%是通配符,可以匹配任意的字符
select c_no from score group by c_no having count(c_no) >= 2;

±------+
| c_no |
±------+
| 3-105 |
| 3-245 |
| 6-166 |
±------+

select c_no from score group by c_no having count(c_no) >= 2 and c_no like ‘3%’;

±------+
| c_no |
±------+
| 3-105 |
| 3-245 |
±------+

select c_no, avg(sc_degree) from score group by c_no having count(c_no) >= 2 and c_no like ‘3%’;

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

select c_no, avg(sc_degree), count(*) from score group by c_no having count(c_no) >= 2 and c_no like ‘3%’;

±------±---------------±---------+
| c_no | avg(sc_degree) | count(*) |
±------±---------------±---------+
| 3-105 | 85.3333 | 3 |
| 3-245 | 76.3333 | 3 |
±------±---------------±---------+

13.查询分数大于70但是小于90的s_no列:
select s_no, sc_degree from score where sc_degree > 70 and sc_degree < 90;

或者

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 71 AND 89;
±-------±----------±-----------+
| s_name | sc_degree | c_name |
±-------±----------±-----------+
| 王丽 | 86 | 操作系统 |
| 王丽 | 85 | 数字电路 |
| 王芳 | 88 | 计算机导论 |
| 王芳 | 75 | 操作系统 |
| 王芳 | 79 | 数字电路 |
| 赵铁柱 | 76 | 计算机导论 |
| 赵铁柱 | 81 | 数字电路 |
±-------±----------±-----------+

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

select s_no, c_no, sc_degree 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 |
±-----±------±----------+

select s_no, s_name from student;
±-----±-------+
| s_no | s_name |
±-----±-------+
| 101 | 曾华 |
| 102 | 匡明 |
| 103 | 王丽 |
| 104 | 李军 |
| 105 | 王芳 |
| 106 | 陆军 |
| 107 | 王尼玛 |
| 108 | 张全蛋 |
| 109 | 赵铁柱 |
±-----±-------+

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列
多表查询

select c_no, c_name from course;
±------±-----------+
| c_no | c_name |
±------±-----------+
| 3-105 | 计算机导论 |
| 3-245 | 操作系统 |
| 6-166 | 数字电路 |
| 9-888 | 高等数学 |
±------±-----------+

select c_no, s_no, sc_degree from score;
±------±-----±----------+
| c_no | s_no | sc_degree |
±------±-----±----------+
| 3-105 | 103 | 92 |
| 3-245 | 103 | 86 |
| 6-166 | 103 | 85 |
| 3-105 | 105 | 88 |
| 3-245 | 105 | 75 |
| 6-166 | 105 | 79 |
| 3-105 | 109 | 76 |
| 3-245 | 109 | 68 |
| 6-166 | 109 | 81 |
±------±-----±----------+

select s_no, c_name, sc_degree from course, score where course.c_no=score.c_no;

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

16.查询所有的学生 s_name , c_name, sc_degree列
多表查询
select s_name, c_name, sc_degree from student, course, score where student.s_no = score.s_no and course.c_no = score.c_no;

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

select s_name, c_name, sc_degree, student.s_no, course.c_no from student, course, score where student.s_no = score.s_no and course.c_no = score.c_no;
//因为多张表都含有s_no和c_no,所以在使用时需要明确哪张表的,否则就会报错

±-------±-----------±----------±-----±------+
| s_name | c_name | sc_degree | s_no | c_no |
±-------±-----------±----------±-----±------+
| 王丽 | 计算机导论 | 92 | 103 | 3-105 |
| 王丽 | 操作系统 | 86 | 103 | 3-245 |
| 王丽 | 数字电路 | 85 | 103 | 6-166 |
| 王芳 | 计算机导论 | 88 | 105 | 3-105 |
| 王芳 | 操作系统 | 75 | 105 | 3-245 |
| 王芳 | 数字电路 | 79 | 105 | 6-166 |
| 赵铁柱 | 计算机导论 | 76 | 109 | 3-105 |
| 赵铁柱 | 操作系统 | 68 | 109 | 3-245 |
| 赵铁柱 | 数字电路 | 81 | 109 | 6-166 |
±-------±-----------±----------±-----±------+

select s_name, c_name, sc_degree, student.s_no as stu_sno, course.c_no as cou_cno from student, course, score where student.s_no = score.s_no and course.c_no = score.c_no;
//取别名student.s_no as stu_sno, course.c_no as cou_cno

±-------±-----------±----------±--------±--------+
| s_name | c_name | sc_degree | stu_sno | cou_cno |
±-------±-----------±----------±--------±--------+
| 王丽 | 计算机导论 | 92 | 103 | 3-105 |
| 王丽 | 操作系统 | 86 | 103 | 3-245 |
| 王丽 | 数字电路 | 85 | 103 | 6-166 |
| 王芳 | 计算机导论 | 88 | 105 | 3-105 |
| 王芳 | 操作系统 | 75 | 105 | 3-245 |
| 王芳 | 数字电路 | 79 | 105 | 6-166 |
| 赵铁柱 | 计算机导论 | 76 | 109 | 3-105 |
| 赵铁柱 | 操作系统 | 68 | 109 | 3-245 |
| 赵铁柱 | 数字电路 | 81 | 109 | 6-166 |
±-------±-----------±----------±--------±--------+

select s_name, c_name, sc_degree, student.s_no as stu_sno, score.s_no, course.c_no as cou_cno, score.c_no from student, course, score where student.s_no = score.s_no and course.c_no = score.c_no;
//可以看出他们之间的联系就是共同字段之间相等来联系

±-------±-----------±----------±--------±-----±--------±------+
| s_name | c_name | sc_degree | stu_sno | s_no | cou_cno | c_no |
±-------±-----------±----------±--------±-----±--------±------+
| 王丽 | 计算机导论 | 92 | 103 | 103 | 3-105 | 3-105 |
| 王丽 | 操作系统 | 86 | 103 | 103 | 3-245 | 3-245 |
| 王丽 | 数字电路 | 85 | 103 | 103 | 6-166 | 6-166 |
| 王芳 | 计算机导论 | 88 | 105 | 105 | 3-105 | 3-105 |
| 王芳 | 操作系统 | 75 | 105 | 105 | 3-245 | 3-245 |
| 王芳 | 数字电路 | 79 | 105 | 105 | 6-166 | 6-166 |
| 赵铁柱 | 计算机导论 | 76 | 109 | 109 | 3-105 | 3-105 |
| 赵铁柱 | 操作系统 | 68 | 109 | 109 | 3-245 | 3-245 |
| 赵铁柱 | 数字电路 | 81 | 109 | 109 | 6-166 | 6-166 |
±-------±-----------±----------±--------±-----±--------±------+

17.查询班级是’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’;

//用于下面复合语句

±-----+
| s_no |
±-----+
| 102 |
| 105 |
| 106 |
| 108 |
| 109 |
±-----+

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;

//group by c_no根据它来分组

±------±---------------+
| c_no | avg(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’成绩 的所有同学的记录

select sc_degree from score where s_no=‘109’ and c_no=‘3-105’;
±----------+
| sc_degree |
±----------+
| 76 |
±----------+

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’的成绩的所有记录
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 |
±-----±------±----------+

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;

±-----±-------±------±--------------------±--------±-----±------±----------+
| s_no | s_name | s_sex | s_birthday | s_class | s_no | c_no | sc_degree |
±-----±-------±------±--------------------±--------±-----±------±----------+
| 103 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 | 103 | 3-105 | 92 |
| 103 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 | 103 | 3-245 | 86 |
| 103 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 | 103 | 6-166 | 85 |
| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | 105 | 3-105 | 88 |
| 105 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 | 105 | 6-166 | 79 |
| 109 | 赵铁柱 | 男 | 1974-06-03 00:00:00 | 95031 | 109 | 6-166 | 81 |
±-----±-------±------±--------------------±--------±-----±------±----------+

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

select * 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 |
| 108 | 张全蛋 | 男 | 1975-02-10 00:00:00 | 95031 |
±-----±-------±------±--------------------±--------+

select 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.查询 张旭 教师任课的学生的成绩

select * from teacher where t_name = ‘张旭’;
±-----±-------±------±--------------------±------±-----------+
| t_no | t_name | t_sex | t_birthday | t_rof | t_depart |
±-----±-------±------±--------------------±------±-----------+
| 856 | 张旭 | 男 | 1969-03-12 00:00:00 | 讲师 | 电子工程系 |
±-----±-------±------±--------------------±------±-----------+

select t_no from teacher where t_name = ‘张旭’;
±-----+
| t_no |
±-----+
| 856 |
±-----+

select * from course where t_no = (select t_no from teacher where t_name = ‘张旭’);
±------±---------±-----+
| c_no | c_name | t_no |
±------±---------±-----+
| 6-166 | 数字电路 | 856 |
±------±---------±-----+

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’);

select c_no from score group by c_no;
±------+
| c_no |
±------+
| 3-105 |
| 3-245 |
| 6-166 |
±------+

select c_no from score group by c_no having count(*) > 5;
±------+
| c_no |
±------+
| 3-105 |
±------+

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 | 讲师 | 电子工程系 |
±-----±-------±------±--------------------±-------±-----------+

select * from course;
±------±-----------±-----+
| c_no | c_name | t_no |
±------±-----------±-----+
| 3-105 | 计算机导论 | 825 |
| 3-245 | 操作系统 | 804 |
| 6-166 | 数字电路 | 856 |
| 9-888 | 高等数学 | 831 |
±------±-----------±-----+

select t_no from course where c_no = (select c_no from score group by c_no having count(*) > 5);
±-----+
| t_no |
±-----+
| 825 |
±-----+

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

±-------+
| t_name |
±-------+
| 王萍 |
±-------+

注意:视频中用 ‘=’ 是不严谨的,实际中你根本不知道有多少条件是符合的,要用IN
SELECT t_name 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_name |
±-------+
| 王萍 |
±-------+

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

由于视频中就只有这两个班,所以要插入数据:

INSERT INTO student VALUES(‘110’,‘张飞’,‘男’,‘1974-06-03’,‘95038’);

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 |
| 110 | 张飞 | 男 | 1974-06-03 00:00:00 | 95038 |
±-----±-------±------±--------------------±--------+

select * from student where s_class in (‘95031’, ‘95033’);

±-----±-------±------±--------------------±--------+
| 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 |
±-----±-------±------±--------------------±--------+

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

select * from score;
±-----±------±----------+
| s_no | c_no | sc_degree |
±-----±------±----------+
| 101 | 3-105 | 90 |
| 102 | 3-105 | 91 |
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 104 | 3-105 | 89 |
| 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 from score where sc_degree > 85;

±------+
| c_no |
±------+
| 3-105 |
| 3-105 |
| 3-105 |
| 3-245 |
| 3-105 |
| 3-105 |
±------+

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

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 | 助教 | 计算机系 |
±-----±-------±------±--------------------±-------±---------+

select * from course where t_no in ( select t_no from teacher where t_depart = ‘计算机系’);
±------±-----------±-----+
| c_no | c_name | t_no |
±------±-----------±-----+
| 3-245 | 操作系统 | 804 |
| 3-105 | 计算机导论 | 825 |
±------±-----------±-----+

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 |
±-----±------±----------+
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 101 | 3-105 | 90 |
| 102 | 3-105 | 91 |
| 103 | 3-105 | 92 |
| 104 | 3-105 | 89 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
±-----±------±----------+

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

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 | 讲师 | 电子工程系 |
±-----±-------±------±--------------------±-------±-----------+

//所以按照题意只是找副教授和讲师,找计算机系中有但是电子工程系没有的教师的name和rof

select t_rof from teacher where t_depart = ‘电子工程系’;
±------+
| t_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-245’;
±-----±------±----------+
| s_no | c_no | sc_degree |
±-----±------±----------+
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
±-----±------±----------+

select * from score where 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 |
| 109 | 3-105 | 76 |
±-----±------±----------+

至少,说明大于其中至少一个,用any语法

select * from score where c_no = ‘3-105’ and sc_degree > any(select sc_degree from score where c_no = ‘3-245’);
±-----±------±----------+
| 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 |
| 109 | 3-105 | 76 |
±-----±------±----------+

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 c_no = ‘3-105’ and sc_degree > all(select sc_degree from score where c_no = ‘3-245’);

±-----±------±----------+
| 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 |
±-----±------±----------+

29. 查询所有教师和同学的 name ,sex, birthday
select t_name, t_sex, t_birthday from teacher union select s_name, s_sex, s_birthday from student;
用union

±-------±------±--------------------+
| t_name | t_sex | t_birthday |
±-------±------±--------------------+
| 李诚 | 男 | 1958-12-02 00:00:00 |
| 王萍 | 女 | 1972-05-05 00:00:00 |
| 刘冰 | 女 | 1977-08-14 00:00:00 |
| 张旭 | 男 | 1969-03-12 00:00:00 |
| 曾华 | 男 | 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 |
±-------±------±--------------------+

select t_name as name, t_sex as sex, t_birthday as birthday from teacher union select s_name, s_sex, s_birthday from student;
别名,用as,第二排默认和第一排使用一样的名字

±-------±----±--------------------+
| name | sex | birthday |
±-------±----±--------------------+
| 李诚 | 男 | 1958-12-02 00:00:00 |
| 王萍 | 女 | 1972-05-05 00:00:00 |
| 刘冰 | 女 | 1977-08-14 00:00:00 |
| 张旭 | 男 | 1969-03-12 00:00:00 |
| 曾华 | 男 | 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 |
±-------±----±--------------------+

30.查询所有’女’教师和’女’学生的name,sex,birthday
select t_name as name, t_sex as sex, t_birthday as birthday from teacher where t_sex = ‘女’ union select s_name, s_sex, s_birthday from student where s_sex = ‘女’;

±-----±----±--------------------+
| name | sex | birthday |
±-----±----±--------------------+
| 王萍 | 女 | 1972-05-05 00:00:00 |
| 刘冰 | 女 | 1977-08-14 00:00:00 |
| 王丽 | 女 | 1976-01-23 00:00:00 |
| 王芳 | 女 | 1975-02-10 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;
±-----±------±----------+
| s_no | c_no | sc_degree |
±-----±------±----------+
| 101 | 3-105 | 90 |
| 102 | 3-105 | 91 |
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 104 | 3-105 | 89 |
| 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 * from score a where sc_degree < (select avg(sc_degree) from score b where a.c_no = b.c_no);

//相当于复制了一个表,在两个表中进行操作,第一个表取别名叫做a,第二个表叫做b

±-----±------±----------+
| 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 |
±-----±------±----------+

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

//可能有的教师在但是没有任课的情况,所以需要根据课程表进行查看一下

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 | 讲师 | 电子工程系 |
±-----±-------±------±--------------------±-------±-----------+

select * from course;
±------±-----------±-----+
| c_no | c_name | t_no |
±------±-----------±-----+
| 3-105 | 计算机导论 | 825 |
| 3-245 | 操作系统 | 804 |
| 6-166 | 数字电路 | 856 |
| 9-888 | 高等数学 | 831 |
±------±-----------±-----+

select * from teacher where t_no in (select t_no from course);
±-----±-------±------±--------------------±-------±-----------+
| 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 | 讲师 | 电子工程系 |
±-----±-------±------±--------------------±-------±-----------+

select t_name, t_depart from teacher where t_no in (select t_no from course);

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

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

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 |
| 110 | 张飞 | 男 | 1974-06-03 00:00:00 | 95038 |
±-----±-------±------±--------------------±--------+

select s_class from student where s_sex = ‘男’ group by s_class having count(*) > 1;

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

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 year(now());

±------------+
| year(now()) |
±------------+
| 2021 |
±------------+

select year(s_birthday) from student;

±-----------------+
| year(s_birthday) |
±-----------------+
| 1977 |
| 1975 |
| 1976 |
| 1976 |
| 1975 |
| 1974 |
| 1976 |
| 1975 |
| 1974 |
| 1974 |
±-----------------+

select s_name, year(now()) - year(s_birthday) as ‘年龄’ from student;

±-------±-----+
| s_name | 年龄 |
±-------±-----+
| 曾华 | 44 |
| 匡明 | 46 |
| 王丽 | 45 |
| 李军 | 45 |
| 王芳 | 46 |
| 陆军 | 47 |
| 王尼玛 | 45 |
| 张全蛋 | 46 |
| 赵铁柱 | 47 |
| 张飞 | 47 |
±-------±-----+

select s_name, year(now()) - year(s_birthday) as ‘年龄’ from student;
±-------±-----+
| s_name | 年龄 |
±-------±-----+
| 曾华 | 44 |
| 匡明 | 46 |
| 王丽 | 45 |
| 李军 | 45 |
| 王芳 | 46 |
| 陆军 | 47 |
| 王尼玛 | 45 |
| 张全蛋 | 46 |
| 赵铁柱 | 47 |
| 张飞 | 47 |
±-------±-----+

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

//max min

select s_birthday from student order by s_birthday;
±--------------------+
| s_birthday |
±--------------------+
| 1974-06-03 00:00:00 |
| 1974-06-03 00:00:00 |
| 1974-06-03 00:00:00 |
| 1975-02-10 00:00:00 |
| 1975-02-10 00:00:00 |
| 1975-10-02 00:00:00 |
| 1976-01-23 00:00:00 |
| 1976-02-20 00:00:00 |
| 1976-02-20 00:00:00 |
| 1977-09-01 00:00:00 |
±--------------------+

select max(s_birthday) as ‘最大’, min(s_birthday) as ‘最小’ from student;

//这个最大和最小是按照年份的大小值排的

±--------------------±--------------------+
| 最大 | 最小 |
±--------------------±--------------------+
| 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 |
| 104 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 107 | 王尼玛 | 男 | 1976-02-20 00:00:00 | 95033 |
| 101 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 106 | 陆军 | 男 | 1974-06-03 00:00:00 | 95031 |
| 109 | 赵铁柱 | 男 | 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 teacher where t_sex = ‘男’;
±-----±-------±------±--------------------±-------±-----------+
| 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 | 讲师 | 电子工程系 |
±-----±-------±------±--------------------±-------±-----------+

select * from course where t_no in (select t_no from teacher where t_sex = ‘男’);

±------±---------±-----+
| 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 max(sc_degree) from score;
±---------------+
| max(sc_degree) |
±---------------+
| 92 |
±---------------+

select * from score where sc_degree = (select max(sc_degree) from score);

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

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

select s_sex from student where s_name = ‘李军’;
±------+
| s_sex |
±------+
| 男 |
±------+

select s_name from student where s_sex = ( select s_sex from student where s_name = ‘李军’);

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

41.查询和"李军"同性别并且同班的所有同学的s_name
select s_name from student where s_sex = (select s_sex from student where s_name = ‘李军’) and s_class = (select s_class from student where s_name = ‘李军’);

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

42.查询所有选修’计算机导论’课程的’男’同学的成绩表
select * from student where s_sex = ‘男’;

±-----±-------±------±--------------------±--------+
| 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 |
| 107 | 王尼玛 | 男 | 1976-02-20 00:00:00 | 95033 |
| 108 | 张全蛋 | 男 | 1975-02-10 00:00:00 | 95031 |
| 109 | 赵铁柱 | 男 | 1974-06-03 00:00:00 | 95031 |
| 110 | 张飞 | 男 | 1974-06-03 00:00:00 | 95038 |
±-----±-------±------±--------------------±--------+

select * from course where c_name = ‘计算机导论’;
±------±-----------±-----+
| c_no | c_name | t_no |
±------±-----------±-----+
| 3-105 | 计算机导论 | 825 |
±------±-----------±-----+

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 |
±-----±------±----------+

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’);

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 |
±-----±------±------+

查询所有同学的s_no , c_no 和grade列

select * from grade;
±-----±-----±------+
| low | upp | grade |
±-----±-----±------+
| 90 | 100 | A |
| 80 | 89 | B |
| 70 | 79 | c |
| 60 | 69 | D |
| 0 | 59 | E |
±-----±-----±------+

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 |
±-----±-------±-----------±------±------+

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值