MySQL学习笔记—查询练习

1.首先建立几张表

学生表
Student:学号、姓名、性别、出生年月日、所在班级

create table Student(
	sno varchar(20) primary key,
	sname varchar(20) not null,
	ssex varchar(2) not null,
	sbirthday datetime,
	classno varchar(20)
);

课程表
Course:课程号、课程名称、教师编号
//教师编号是教师表的外键,必须先创建依赖

create table Couse(
	cno varchar(20) primary key,
	cname varchar(20) not null,
	tno varchar(20) not null,
	foreign key(tno) references Teacher(tno)		
);

成绩表
Score:学号、课程号、成绩
create table Score(
sno varchar(20),
cno varchar(20) not null,
degree decimal,
primary key (sno,cno),
foreign key (sno) references Student(sno),
foreign key (cno) references Course(cno)
);

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

create table Teacher(
	tno varchar(20) primary key,
	tname varchar(20) not null,
	tsex varchar(2) not null,
	tbirthday datetime,
	prof varchar(20) not null,
	department varchar(20) not null
);
  1. 向数据表中添加数据
    – 往数据表中添加数据
    #添加学生信息
    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’);
    mysql> select * from student;
    ±----±-------±-----±--------------------±--------+
    | sno | sname | ssex | sbirthday | classno |
    ±----±-------±-----±--------------------±--------+
    | 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 |
    ±----±-------±-----±--------------------±--------+
    9 rows in set (0.00 sec)

#添加教师表
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');
//修改主键
ALTER TABLE `test2` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `id` )
#添加成绩表
insert into score values('103','3-105','92');
insert into score values('103','3-245','86');
insert into score values('103','6-166','85');
insert into score values('105','3-105','88');
insert into score values('105','3-245','75');
insert into score values('105','6-166','79');
insert into score values('109','3-105','76');
insert into score values('109','3-245','68');
insert into score values('109','6-166','81');

查询练习

1、查询student表中的所有记录

mysql> select * from student;
±----±-------±-----±--------------------±--------+
| sno | sname | ssex | sbirthday | classno |
±----±-------±-----±--------------------±--------+
| 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 |
±----±-------±-----±--------------------±--------+
9 rows in set (0.00 sec)

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

mysql> select sname,ssex,classno from student;
±-------±-----±--------+
| sname | ssex | classno |
±-------±-----±--------+
| 曾华 | 男 | 95033 |
| 匡明 | 男 | 95031 |
| 王丽 | 女 | 95033 |
| 李军 | 男 | 95033 |
| 王芳 | 女 | 95031 |
| 陆君 | 男 | 95031 |
| 王尼玛 | 男 | 95033 |
| 张全蛋 | 男 | 95031 |
| 赵铁柱 | 男 | 95031 |
±-------±-----±--------+
9 rows in set (0.00 sec)

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

mysql> select department from teacher;
±-----------+
| department |
±-----------+
| 计算机系 |
| 计算机系 |
| 电子工程系 |
| 电子工程系 |
±-----------+
4 rows in set (0.00 sec)

那么如何排除重复呢?
distinct 排除重复~

mysql> select distinct department from teacher;
±-----------+
| department |
±-----------+
| 计算机系 |
| 电子工程系 |
±-----------+
2 rows in set (0.01 sec)

4、查询score表中成绩在60-80之间的记录。
–查询区间,between … and …

mysql> select * from score where degree between 60 and 80;
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
±----±------±-------+
4 rows in set (0.02 sec)

–直接使用运算符比较

mysql> select * from score where degree > 60 and degree < 80;
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
±----±------±-------+
4 rows in set (0.01 sec)

5、查询socre表中成绩为85,86或88的记录
–表示或者关系的查询 in

select * from score where degree in(85,86,88);
mysql> select * from score where degree in(85,86,88);
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
±----±------±-------+
3 rows in set (0.00 sec)

–直接使用运算符比较

mysql> select * from score where degree = 85 or degree =86 or degree = 88;
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
±----±------±-------+
3 rows in set (0.00 sec)

6、 查询student表中“95031”班中或性别为"女"的记录;
– or表示或者 同理 and表示并且

mysql> select * from student where classno = ‘95031’ or ssex = ‘女’;
±----±-------±-----±--------------------±--------+
| sno | sname | ssex | sbirthday | classno |
±----±-------±-----±--------------------±--------+
| 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 |
±----±-------±-----±--------------------±--------+
6 rows in set (0.00 sec)

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

mysql> select * from student order by classno desc; 降序
±----±-------±-----±--------------------±--------+
| sno | sname | ssex | sbirthday | classno |
±----±-------±-----±--------------------±--------+
| 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 |
±----±-------±-----±--------------------±--------+
9 rows in set (0.02 sec)
select * from student order by classno asc; 升序,也是默认的;

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

mysql> select * from score order by cno asc, degree desc;
±----±------±-------+
| sno | cno | 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 rows in set (0.00 sec)

9、查询95031班的人数
–统计 count

mysql> select count() from student where classno = ‘95031’;
±---------+
| count(
) |
±---------+
| 5 |
±---------+
1 row in set (0.02 sec)

10、查询score表中最高分的记录。
子查询

mysql> select * from score where degree = (select max(degree) from score);
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 103 | 3-105 | 92 |
±----±------±-------+
1 row in set (0.00 sec)

分解上述步骤:
(1)找到最高分

select max(degree) from score

(2)找最高分的记录
select * from score where degree = (select max(degree) from score);

排序做法 limit 指定区间的记录 一个缺陷是无法获取重复的数据,即如果最大得分不是一个记录的话,limit只限制了一条记录。 0表示从0开始,1表示查1条。
select * from score order by degree desc limit 0,1;

11、 查询每门课的平均成绩
–avg() 计算平均值
–group by 分组

mysql> select cno,avg(degree) from score group by cno;
±------±------------+
| cno | avg(degree) |
±------±------------+
| 3-105 | 85.3333 |
| 3-245 | 76.3333 |
| 6-166 | 81.6667 |
±------±------------+
3 rows in set (0.00 sec)

12、查询 score表中至少有3名学生选修的并以3开头的课程平均分数。
(1)查询复杂问题的时候 对其进行分解, 首先对cno进行分组 group by cno
(2)使用having,让分组的结果附加一个条件
(3)like 模糊查询

mysql> select cno, avg(degree) from score group by cno having count(cno) >= 3 and cno like ‘3%’;
±------±------------+
| cno | avg(degree) |
±------±------------+
| 3-105 | 85.3333 |
| 3-245 | 76.3333 |
±------±------------+
2 rows in set (0.00 sec)

13、查询分数大于70,小于90的sno列。
很简单的区间操作,between and / and

mysql> select sno,degree from score where degree > 70 and degree < 90;
±----±-------+
| sno | degree |
±----±-------+
| 103 | 86 |
| 103 | 85 |
| 105 | 88 |
| 105 | 75 |
| 105 | 79 |
| 109 | 76 |
| 109 | 81 |
±----±-------+
7 rows in set (0.00 sec)

14、查询所有学生的sname,cno和degree;
(1)sname 来自于学生表student
(2)cno和degree来自于score表
多表查询

mysql> select student.sname , cno , degree from student,score where score.sno = student.sno;
±-------±------±-------+
| sname | cno | 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 |
±-------±------±-------+
9 rows in set (0.00 sec)

15、查询所有学生的sno、cname和degree
(1)cname来自于course表
(2)sno和degree来自于score表

mysql> select score.sno, course.cname, score.degree from score,course where score.cno = course.cno;
±----±-----------±-------+
| sno | cname | degree |
±----±-----------±-------+
| 103 | 计算机导论 | 92 |
| 105 | 计算机导论 | 88 |
| 109 | 计算机导论 | 76 |
| 103 | 操作系统 | 86 |
| 105 | 操作系统 | 75 |
| 109 | 操作系统 | 68 |
| 103 | 数字电路 | 85 |
| 105 | 数字电路 | 79 |
| 109 | 数字电路 | 81 |
±----±-----------±-------+
9 rows in set (0.00 sec)

16、查询所有学生的sname、cname和degree
(1)sname来自于student表
(2)cname来自于course表
(3)degree来自于score表
(4)score其实是所有学生的选课记录,sno和cno是连接student和course表的桥梁;

select student.sname, course.cname, score.degree, student.sno as stu_sno, course.cno as cou_cno from student,course,score  where student.sno = score.sno and course.cno = score.cno;
+--------+------------+--------+---------+---------+
| sname  | cname      | 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   |
+--------+------------+--------+---------+---------+
9 rows in set (0.00 sec)

17、查询‘95031’班学生每门课的平均分
(1) select sno from student where classno = ‘95031’ 首先先查找到95031班有那些学号
(2) select * from score where sno in (select sno from student where classno = ‘95031’) 将学号是95031班的记录查找出来
(3) select cno,avg(degree) from score where sno in (select sno from student where classno = ‘95031’) group by cno; 把第二步查找出来的记录按照 cno分组,计算平均分即可。

mysql> select cno,avg(degree) from score where sno in (select sno from student where classno = ‘95031’) group by cno;
±------±------------+
| cno | avg(degree) |
±------±------------+
| 3-105 | 82.0000 |
| 3-245 | 71.5000 |
| 6-166 | 80.0000 |
±------±------------+
3 rows in set (0.02 sec)

18、查询选修“3-105”课程的成绩高于“109”号同学“3-105”成绩的所有同学的记录。
(1) select degree from score where sno = ‘109’ and cno = ‘3-105’; 首先查出109号同学的3-105课程成绩;
(2) select * from score where cno = ‘3-105’ and degree > (select degree from score where sno = ‘109’ and cno = ‘3-105’);
第二步,查询成绩大于第一步查询出的成绩的记录即可。

mysql> select * from score where cno = ‘3-105’ and degree > (select degree from score where sno = ‘109’ and cno = ‘3-105’);
±----±------±-------+
| sno | cno | degree |
±----±------±-------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
±----±------±-------+
2 rows in set (0.00 sec)

19、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
(1) select * from score where degree > (select degree from score where sno = ‘109’ and cno = ‘3-105’);

mysql> select * from score where degree > (select degree from score where sno = ‘109’ and cno = ‘3-105’);
±----±------±-------+
| sno | cno | 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 |
±----±------±-------+
6 rows in set (0.00 sec)

20、查询和学号为108、101的同学同年出生的所有学生的sno、sname和sbirthday列。
(1) select year(sbirthday) from student where sno in (108,101);

mysql> select year(sbirthday) from student where sno in (108,101);
±----------------+
| year(sbirthday) |
±----------------+
| 1977 |
| 1975 |
±----------------+
2 rows in set (0.00 sec)

(2) select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));

mysql> select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
±----±-------±--------------------+
| sno | sname | sbirthday |
±----±-------±--------------------+
| 101 | 曾华 | 1977-09-01 00:00:00 |
| 102 | 匡明 | 1975-10-02 00:00:00 |
| 105 | 王芳 | 1975-02-10 00:00:00 |
| 108 | 张全蛋 | 1975-02-10 00:00:00 |
±----±-------±--------------------+
4 rows in set (0.00 sec)

21、查询“张旭“教师任课的学生成绩。
(1) select tno from teacher where tname=‘张旭’;
(2) select cno from course where tno = ( select tno from teacher where tname=‘张旭’);
(3) select degree from score where cno = (select cno from course where tno = ( select tno from teacher where tname=‘张旭’));.

mysql> select degree from score where cno = (select cno from course where tno = ( select tno from teacher where tname=’ 张旭’));
±-------+
| degree |
±-------+
| 85 |
| 79 |
| 81 |
±-------+
3 rows in set (0.00 sec)

22、 查询选修某课程的同学人数多于5人的教师姓名。

select cno from score group by cno having count(*) > 5;

select tno from course where cno in (select cno from score group by cno having count(*) > 5);

select tname from teacher where tno in (select tno from course where cno in (select cno from score group by cno having count(*) > 5));

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

mysql> select * from student where classno in (‘95031’,‘95033’);
±----±-------±-----±--------------------±--------+
| sno | sname | ssex | sbirthday | classno |
±----±-------±-----±--------------------±--------+
| 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 |
±----±-------±-----±--------------------±--------+
9 rows in set (0.00 sec)

24、查询存在有85分以上成绩的课程Cno.

select cno from score where degree > 85;

mysql> select cno from score where degree > 85;
±------+
| cno |
±------+
| 3-105 |
| 3-105 |
| 3-105 |
| 3-245 |
| 3-105 |
| 3-105 |
±------+
6 rows in set (0.00 sec)

25、查询出“计算机系“教师所教课程的成绩表。
(1)

mysql> select tno from teacher where department = ‘计算机系’;
±----+
| tno |
±----+
| 804 |
| 825 |
±----+
2 rows in set (0.00 sec)

(2)

mysql> select * from course where tno in (select tno from teacher where department = ‘计算机系’);
±------±-----------±----+
| cno | cname | tno |
±------±-----------±----+
| 3-245 | 操作系统 | 804 |
| 3-105 | 计算机导论 | 825 |
±------±-----------±----+
2 rows in set (0.00 sec)

(3)

mysql> select * from score where cno in (select cno from course where tno in (select tno from teacher where department = ‘计算机系’))
-> ;
±----±------±-------+
| sno | cno | 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 |
±----±------±-------+
9 rows in set (0.00 sec)

26、查询“计算机系”与“电子工程系“不同职称的教师的tname和prof。

select * from teacher where department = ‘计算机系’ and prof not in (select prof from teacher where department = ‘电子工程系’);
union 拼接两个表
select * from teacher where department = ‘电子工程系’ and prof not in (select prof from teacher where department = ‘计算机系’);

mysql> select * from teacher where department = ‘计算机系’ and prof not in (select prof from teacher where department = ‘电子工程系’)
-> union
-> select * from teacher where department = ‘电子工程系’ and prof not in (select prof from teacher where department = ‘计算机系’);
±----±------±-----±--------------------±-------±-----------+
| tno | tname | tsex | tbirthday | prof | department |
±----±------±-----±--------------------±-------±-----------+
| 804 | 李诚 | 男 | 1958-12-02 00:00:00 | 副教授 | 计算机系 |
| 856 | 张旭 | 男 | 1969-03-12 00:00:00 | 讲师 | 电子工程系 |
±----±------±-----±--------------------±-------±-----------+
2 rows in set (0.00 sec)

你可能感兴趣的:(MySQL)