设有一个数据库(sql_exercise02),包括四个表:学生表(Student),课程表(Course),成绩表(Score),以及教师信息表(Teacher),设计如下针对四张表完成如下信息
创表语句 以及表数据的添加在文章底部
表结构
#学生表
mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Sno | varchar(20) | NO | PRI | NULL | |
| Sname | varchar(20) | NO | | NULL | |
| Ssex | varchar(20) | NO | | NULL | |
| Sbirthday | datetime | YES | | NULL | |
| Class | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
#教师表
mysql> desc teacher;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Tno | varchar(20) | NO | PRI | NULL | |
| Tname | varchar(20) | NO | | NULL | |
| Tsex | varchar(20) | NO | | NULL | |
| Tbirthday | datetime | YES | | NULL | |
| Prof | varchar(20) | YES | | NULL | |
| Depart | varchar(20) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> #课程表
mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Cno | varchar(20) | NO | PRI | NULL | |
| Cname | varchar(20) | NO | | NULL | |
| Tno | varchar(20) | NO | MUL | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> #成绩表
mysql> desc score;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| Sno | varchar(20) | NO | MUL | NULL | |
| Cno | varchar(20) | NO | MUL | NULL | |
| Degree | decimal(10,0) | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
表数据
mysql> #学生表
mysql> select *from student;
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
mysql> #课程表
mysql> select *from course;
+-------+-----------------+-----+
| Cno | Cname | Tno |
+-------+-----------------+-----+
| 3-105 | 计算机导论 | 825 |
| 3-245 | 操作系统 | 804 |
| 6-166 | 数字电路 | 856 |
| 9-888 | 高等数学 | 831 |
+-------+-----------------+-----+
4 rows in set (0.00 sec)
mysql> #成绩表
mysql> select *from score;
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 105 | 3-105 | 91 |
| 109 | 3-105 | 78 |
| 103 | 6-166 | 85 |
| 105 | 6-166 | 79 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
12 rows in set (0.00 sec)
mysql> #教师表
mysql> select *from teacher;
+-----+--------+------+---------------------+-----------+-----------------+
| Tno | Tname | Tsex | Tbirthday | Prof | 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 | 讲师 | 电子工程系 |
+-----+--------+------+---------------------+-----------+-----------------+
4 rows in set (0.00 sec)
1. 查询Student表中的所有记录的Sname , Ssex 和 Class列
select Sname ,Ssex,Class from student;
--->最终结果:
mysql> select Sname ,Ssex,Class from student;
+--------+------+-------+
| Sname | Ssex | Class |
+--------+------+-------+
| 李军 | 男 | 95033 |
| 陆君 | 男 | 95031 |
| 匡明 | 男 | 95031 |
| 王丽 | 女 | 95033 |
| 曾华 | 男 | 95033 |
| 王芳 | 女 | 95031 |
+--------+------+-------+
6 rows in set (0.00 sec)
2. 查询教师所有的单位即不重复的Depart列
select distinct Depart from teacher;
---> 最终结果显示:
mysql> select Depart from teacher ;
+-----------------+
| Depart |
+-----------------+
| 计算机系 |
| 计算机系 |
| 电子工程系 |
| 电子工程系 |
+-----------------+
4 rows in set (0.00 sec)
mysql> select distinct Depart from teacher;
+-----------------+
| Depart |
+-----------------+
| 计算机系 |
| 电子工程系 |
+-----------------+
3.查询Student表的所有记录
select *from student;
4. 查询Score表中成绩在60到80之间的所有记录
select * from score where Degree between 60 and 80;
--->最终结果显示:
mysql> select * from score where Degree between 60 and 80;
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 109 | 3-105 | 78 |
| 105 | 6-166 | 79 |
+-----+-------+--------+
6 rows in set (0.00 sec)
5.查询Score表中成绩为85 86 或88 的记录
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 |
| 105 | 3-105 | 88 |
| 103 | 6-166 | 85 |
+-----+-------+--------+
3 rows in set (0.00 sec)
6. 查询Student表中'95031'班或性别为 '女'的同学记录
select *from student where Class='95031' or Ssex='女';
---> 最终结果显示:
mysql> select *from student where Class='95031' or Ssex='女';
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
4 rows in set (0.00 sec)
7. 以Class降序查询Student表的所有记录
select *from student order by Class desc;
--->最终结果显示:
mysql> select *from student order by Class desc ;
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
8. 以Cno升序 Degree降序查询Score表的所有记录
select *from score order by Cno asc , Degree desc ;
--->最终结果显示:
mysql> select *from score order by Cno asc, Degree desc;
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 91 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 78 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 103 | 6-166 | 85 |
| 109 | 6-166 | 81 |
| 105 | 6-166 | 79 |
+-----+-------+--------+
12 rows in set (0.00 sec)
9. 查询'95031'班的学生人数
select count(1) from student where Class='95031';
---> 最终结果显示:
mysql> select count(1) from student where Class='95031';
+----------+
| count(1) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
10. 查询Score表中的最高分的学生学号和课程号
--->解法一:
mysql> select Sno,Cno from Score where Degree=(select max(Degree) from Score);
+-----+-------+
| Sno | Cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
--->解法二:
mysql> select Sno,Cno from Score order by Degree desc limit 0,1;
+-----+-------+
| Sno | Cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
--->解法三:
select Sname, Sno,Cno
from student,course where (Sno,Cno) = (select Sno,Cno from score where Degree = (select max(Degree) from score));
--->最终结果:
mysql> select Sname, Sno,Cno
-> from student,course where (Sno,Cno) = (select Sno,Cno from score where Degree = (select max(Degree) from score));
+--------+-----+-------+
| Sname | Sno | Cno |
+--------+-----+-------+
| 陆君 | 103 | 3-105 |
+--------+-----+-------+
1 row in set (0.00 sec)
11.查询每门课的平均成绩
select Cno, AVG(Degree) from score group by Cno;
--->最终结果:
mysql> select Cno, AVG(Degree) from score group by Cno;
+-------+-------------+
| Cno | AVG(Degree) |
+-------+-------------+
| 3-105 | 81.5000 |
| 3-245 | 76.3333 |
| 6-166 | 81.6667 |
+-------+-------------+
3 rows in set (0.00 sec)
12.查询Score表中至少有5名学生选修的并以3开头的课程的平均数
select Cno, AVG(Degree)
From score where Cno like '3%' group by Cno
having count(1) >=5;
--->最终结果显示:
mysql> select Cno, AVG(Degree)
-> From score where Cno like '3%' group by Cno
-> having count(1) >=5;
+-------+-------------+
| Cno | AVG(Degree) |
+-------+-------------+
| 3-105 | 81.5000 |
+-------+-------------+
13.查询分数大于70,小于90的Sno列
select Sno from score where Degree between 70 and 90;
--->结果显示:
mysql> select Sno from score where Degree between 70 and 90;
+-----+
| Sno |
+-----+
| 103 |
| 105 |
| 105 |
| 109 |
| 109 |
| 103 |
| 105 |
| 109 |
+-----+
8 rows in set (0.00 sec)
14.查询所有学生的Sname, Cno和Degree列
select Sname,Cno,Degree
from student,score
where student.Sno = score.Sno;
--->最终结果:
mysql> select Sname,Cno,Degree
-> from student,score
-> where student.Sno = score.Sno;
+--------+-------+--------+
| Sname | Cno | Degree |
+--------+-------+--------+
| 陆君 | 3-245 | 86 |
| 匡明 | 3-245 | 75 |
| 王芳 | 3-245 | 68 |
| 陆君 | 3-105 | 92 |
| 匡明 | 3-105 | 88 |
| 王芳 | 3-105 | 76 |
| 陆君 | 3-105 | 64 |
| 匡明 | 3-105 | 91 |
| 王芳 | 3-105 | 78 |
| 陆君 | 6-166 | 85 |
| 匡明 | 6-166 | 79 |
| 王芳 | 6-166 | 81 |
+--------+-------+--------+
12 rows in set (0.00 sec)
15.查询所有学生的Sno, Cname和Degree列
select Sno , Cname , Degree
from course , score
where course.Cno = score.Cno;
--->最终结果显示:
mysql> select Sno , Cname , Degree
-> from course , score
-> where course.Cno = score.Cno;
+-----+-----------------+--------+
| Sno | Cname | Degree |
+-----+-----------------+--------+
| 103 | 操作系统 | 86 |
| 105 | 操作系统 | 75 |
| 109 | 操作系统 | 68 |
| 103 | 计算机导论 | 92 |
| 105 | 计算机导论 | 88 |
| 109 | 计算机导论 | 76 |
| 103 | 计算机导论 | 64 |
| 105 | 计算机导论 | 91 |
| 109 | 计算机导论 | 78 |
| 103 | 数字电路 | 85 |
| 105 | 数字电路 | 79 |
| 109 | 数字电路 | 81 |
+-----+-----------------+--------+
12 rows in set (0.00 sec)
16.查询所有学生的Sname, Cname和Degree列
select Sname , Cname ,Degree
from student,course,score
where student.Sno = score.Sno AND course.Cno = score.Cno;
--->最终结果显示:
mysql> select Sname , Cname ,Degree
-> from student,course,score
-> where student.Sno = score.Sno AND course.Cno = score.Cno;
+--------+-----------------+--------+
| Sname | Cname | Degree |
+--------+-----------------+--------+
| 陆君 | 操作系统 | 86 |
| 匡明 | 操作系统 | 75 |
| 王芳 | 操作系统 | 68 |
| 陆君 | 计算机导论 | 92 |
| 匡明 | 计算机导论 | 88 |
| 王芳 | 计算机导论 | 76 |
| 陆君 | 计算机导论 | 64 |
| 匡明 | 计算机导论 | 91 |
| 王芳 | 计算机导论 | 78 |
| 陆君 | 数字电路 | 85 |
| 匡明 | 数字电路 | 79 |
| 王芳 | 数字电路 | 81 |
+--------+-----------------+--------+
12 rows in set (0.00 sec)
17.查询'95033'班学生的平均分
select AVG(Degree)
from score
where Sno in (select Sno from student where Class='95033');
--->最终结果显示:
mysql> select AVG(Degree)
-> from score
-> where Sno in (select Sno from student where Class='95033');
+-------------+
| AVG(Degree) |
+-------------+
| NULL |
+-------------+
18.假设使用如下命令建立一个grdae表:
create table grade(low int(3),upp int(3),rank 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');
现查询所有同学的Sno、Cno和rank列。
--->解析学生的分值在90~100 之间为 'A' 80~89之间为'B'
select Sno,Cno,rank
from score,grade
where Degree between low and upp;
--->最终结果显示:
mysql> select Sno,Cno,rank
-> from score,grade
-> where Degree between low and upp;
+-----+-------+------+
| Sno | Cno | rank |
+-----+-------+------+
| 103 | 3-245 | B |
| 105 | 3-245 | C |
| 109 | 3-245 | D |
| 103 | 3-105 | A |
| 105 | 3-105 | B |
| 109 | 3-105 | C |
| 103 | 3-105 | D |
| 105 | 3-105 | A |
| 109 | 3-105 | C |
| 103 | 6-166 | B |
| 105 | 6-166 | C |
| 109 | 6-166 | B |
+-----+-------+------+
12 rows in set (0.00 sec)
--->解法二:
select Sno, Cno, (case when Degree between low and upp
then rank else NULL end) as rank
from score,grade
where Sno in (select Sno from student)
having rank is not NULL;
--->结果显示:
mysql> select Sno, Cno, (case when Degree between low and upp
-> then rank else NULL end) as rank
-> from score,grade
-> where Sno in (select Sno from student)
-> having rank is not NULL;
+-----+-------+------+
| Sno | Cno | rank |
+-----+-------+------+
| 103 | 3-245 | B |
| 105 | 3-245 | C |
| 109 | 3-245 | D |
| 103 | 3-105 | A |
| 105 | 3-105 | B |
| 109 | 3-105 | C |
| 103 | 3-105 | D |
| 105 | 3-105 | A |
| 109 | 3-105 | C |
| 103 | 6-166 | B |
| 105 | 6-166 | C |
| 109 | 6-166 | B |
+-----+-------+------+
12 rows in set, 12 warnings (0.01 sec)
19.查询选修'3-105'课程的成绩高于'109'号同学成绩的所有同学记录
---> 这个表显示是那个同学
select Sno, Sname
from student
where Sno in
(select Sno from score where Cno='3-105'
AND Degree > (select max(Degree) from score where Sno=109 ));
--->解法二:
select Sno, Cno
from score
where Cno='3-105' AND Degree >(select max(Degree) from score where Sno=109 );
20.查询'计算机系'和'电子工程系'相同同职称的教师的Tname和Prof
select Tname ,Prof from teacher where Prof = (select Prof from teacher where Depart='计算机系' or Depart='电子工程系' group by Prof having count(1) >1);
--->最终结果:
mysql> select Tname ,Prof from teacher where Prof = (select Prof from teacher where Depart='计算机系' or Depart='电子工程系' group by Prof having count(1) >1);
+--------+--------+
| Tname | Prof |
+--------+--------+
| 王萍 | 助教 |
| 刘冰 | 助教 |
+--------+--------+
2 rows in set (0.00 sec)
21.查询成绩高于学号为'109',课程号为'3-105'的成绩的所有记录
select *
from score
where Cno='3-105' AND Degree >(select max(Degree) from score where Sno=109 );
--->结果显示:
mysql> select *
-> from score
-> where Cno='3-105' AND Degree >(select max(Degree) from score where Sno=109 );
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 105 | 3-105 | 91 |
+-----+-------+--------+
22.查询和学号为108,101的同学同年出生的所有学生的Sno,Sname和Sbirthday列
select Sno,Sname,Sbirthday
from student
where year(Sbirthday) in
(select year(Sbirthday) from student where Sno=108 or Sno=101)
having Sno != 108 and Sno != 101;
--->最终结果显示:
mysql> select Sno,Sname,Sbirthday
-> from student
-> where year(Sbirthday) in
-> (select year(Sbirthday) from student where Sno=108 or Sno=101)
-> having Sno != 108 and Sno != 101;
+-----+--------+---------------------+
| Sno | Sname | Sbirthday |
+-----+--------+---------------------+
| 107 | 王丽 | 1976-01-23 00:00:00 |
+-----+--------+---------------------+
1 row in set (0.00 sec)
23 .查询'张旭'教师任课的学生成绩
select Sno, Degree
from score,course
where score.Cno = course.Cno AND Tno = (select Tno from teacher where Tname = '张旭');
--->最终结果:
mysql> select Sno, Degree
-> from score,course
-> where score.Cno = course.Cno AND Tno = (select Tno from teacher where Tname = '张旭');
+-----+--------+
| Sno | Degree |
+-----+--------+
| 103 | 85 |
| 105 | 79 |
| 109 | 81 |
+-----+--------+
3 rows in set (0.00 sec)
24.查询选修某课程的同学人数多于5人的教师姓名
select Tname
from teacher,course,score
where teacher.Tno = course.Tno AND course.cno = score.cno
group by score.Cno
having count(1) > 5;
--->结果显示:
mysql> select Tname ,count(1)
-> from teacher,course,score
-> where teacher.Tno = course.Tno AND course.cno = score.cno
-> group by score.Cno;
+--------+----------+
| Tname | count(1) |
+--------+----------+
| 王萍 | 6 |
| 李诚 | 3 |
| 张旭 | 3 |
+--------+----------+
3 rows in set (0.00 sec)
--->最终结果显示:
mysql> select Tname
-> from teacher,course,score
-> where teacher.Tno = course.Tno AND course.cno = score.cno
-> group by score.Cno
-> having count(1) > 5;
+--------+
| Tname |
+--------+
| 王萍 |
+--------+
25.查询95033班和95031班全体学生的记录
--->最终结果显示:
mysql> select * from student where class in ('95033','95031');
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
26.查询存在有85分以上成绩的课程Cno
select Cno
from score
where Degree >85;
--->最终结果显示:
mysql> select Cno
-> from score
-> where Degree >85;
+-------+
| Cno |
+-------+
| 3-245 |
| 3-105 |
| 3-105 |
| 3-105 |
+-------+
27.查询出'计算机系'教师所教课程的成绩表
select *
from score where Cno in
(select Cno from course where Tno in
(select Tno from teacher where Depart = '计算机系'));
--->最终结果显示:
mysql> select *
-> from score where Cno in
-> (select Cno from course where Tno in
-> (select Tno from teacher where Depart = '计算机系'));
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 103 | 3-105 | 92 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 105 | 3-105 | 91 |
| 109 | 3-105 | 78 |
+-----+-------+--------+
9 rows in set (0.00 sec)
28.查询'计算机系'和'电子工程系'不同职称的教师的Tname和Prof
select any_value(Tname) Tname,Prof
from teacher
where Depart='计算机系' or Depart='电子工程系'
group by Prof
having count(1) = 1;
--->最终结果
mysql> select any_value(Tname) Tname,Prof
-> from teacher
-> where Depart='计算机系' or Depart='电子工程系'
-> group by Prof
-> having count(1) = 1;
+--------+-----------+
| Tname | Prof |
+--------+-----------+
| 李诚 | 副教授 |
| 张旭 | 讲师 |
+--------+-----------+
--->解法二:
mysql> select Tname,Prof from Teacher where Depart ='计算机系' and Prof not in( select Prof from Teacher where Depart ='电子工程系') union select Tname,Prof from Teacher where Depart ='电子工程系' and Prof not in( select Prof from Teacher where Depart ='计算机系');
+--------+-----------+
| Tname | Prof |
+--------+-----------+
| 李诚 | 副教授 |
| 张旭 | 讲师 |
+--------+-----------+
29.查询选修编号为'3-105'课程且成绩至少高于选修编号为'3-245'的同学的Cno,Sname和Degree,并按照Degree降序排列
any:代表括号中任意一个成绩就可以
--->解析 至少高于 就是高于'3-245'成绩的最低分
select distinct score.Cno,Sname,Degree
from student, course,score
where score.Cno = '3-105' AND student.Sno = score.Sno AND
( Degree > (select min(Degree) from score where Cno='3-245'))
order by Degree desc;
--->最终结果显示:
mysql> select distinct score.Cno,Sname,Degree
-> from student, course,score
-> where score.Cno = '3-105' AND student.Sno = score.Sno AND
-> ( Degree > (select min(Degree) from score where Cno='3-245'))
-> order by Degree desc;
+-------+--------+--------+
| Cno | Sname | Degree |
+-------+--------+--------+
| 3-105 | 陆君 | 92 |
| 3-105 | 匡明 | 91 |
| 3-105 | 匡明 | 88 |
| 3-105 | 王芳 | 78 |
| 3-105 | 王芳 | 76 |
+-------+--------+--------+
5 rows in set (0.00 sec)
--->使用 any :相当于条件or
select distinct score.Cno,Sname,Degree
from student, course,score
where score.Cno = '3-105' AND student.Sno = score.Sno AND
( Degree > any(select Degree from score where Cno='3-245'))
order by Degree desc;
--->最终结果显示:
mysql> select distinct score.Cno,Sname,Degree
-> from student, course,score
-> where score.Cno = '3-105' AND student.Sno = score.Sno AND
-> ( Degree > any(select Degree from score where Cno='3-245'))
-> order by Degree desc;
+-------+--------+--------+
| Cno | Sname | Degree |
+-------+--------+--------+
| 3-105 | 陆君 | 92 |
| 3-105 | 匡明 | 91 |
| 3-105 | 匡明 | 88 |
| 3-105 | 王芳 | 78 |
| 3-105 | 王芳 | 76 |
+-------+--------+--------+
30.查询选修编号为'3-105'且成绩高于选修编号为'3-245'课程的同学的Cno,Sname和Drgree
all:代表括号中所有成绩
---->解析 高于'3-245'的成绩就是高于'3-245'成绩的最大值
----> all 相当于 and
select distinct score.Cno,Sname,Degree
from student, course,score
where score.Cno = '3-105' AND student.Sno = score.Sno AND
( Degree > (select max(Degree) from score where Cno='3-245'))
order by Degree desc;
# 上下两者类似
select distinct score.Cno,Sname,Degree
from student, course,score
where score.Cno = '3-105' AND student.Sno = score.Sno AND
( Degree > all(select Degree from score where Cno='3-245'))
order by Degree desc;
--->最终结果显示:
mysql> select distinct score.Cno,Sname,Degree
-> from student, course,score
-> where score.Cno = '3-105' AND student.Sno = score.Sno AND
-> ( Degree > all(select Degree from score where Cno='3-245'))
-> order by Degree desc;
+-------+--------+--------+
| Cno | Sname | Degree |
+-------+--------+--------+
| 3-105 | 陆君 | 92 |
| 3-105 | 匡明 | 91 |
| 3-105 | 匡明 | 88 |
+-------+--------+--------+
31.查询所有教师和同学的name,sex和birthday
--->解析 这里考察的是表的行拼接 需要使用 union
select Sname name,Ssex sex,Sbirthday birthday
from student
union
select Tname,Tsex,Tbirthday
from teacher;
--->最终结果显示:
mysql> select Sname name,Ssex sex,Sbirthday birthday
-> from student
-> union
-> select Tname,Tsex,Tbirthday
-> from teacher;
+-----------+-----+---------------------+
| name | sex | birthday |
+-----------+-----+---------------------+
| 李军 | 男 | 1976-02-20 00:00:00 |
| 陆君 | 男 | 1974-06-03 00:00:00 |
| 匡明 | 男 | 1975-10-02 00:00:00 |
| 王丽 | 女 | 1976-01-23 00:00:00 |
| 曾华 | 男 | 1977-09-01 00:00:00 |
| 王芳 | 女 | 1975-02-10 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 |
| 孙子衡 | 男 | 1990-05-24 00:00:00 |
+-----------+-----+---------------------+
11 rows in set (0.00 sec)
32.查询所有'女'教师和'女'同学的name,sex和birthday
select Sname name,Ssex sex,Sbirthday birthday
from student where Ssex='女'
union
select Tname,Tsex,Tbirthday
from teacher where Tsex='女';
--->最终结果显示:
mysql> select Sname name,Ssex sex,Sbirthday birthday
-> from student where Ssex='女'
-> union
-> select Tname,Tsex,Tbirthday
-> from teacher where Tsex='女';
+--------+-----+---------------------+
| 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 |
+--------+-----+---------------------+
33.查询成绩比该课程平均成绩低的同学的成绩表
--->解法一:
mysql> select * from score a where degree < (select avg(degree) from score b where b.cno=a.cno);
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 109 | 3-105 | 78 |
| 105 | 6-166 | 79 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
7 rows in set (0.00 sec)
--->解法二:
--->解析先把各个课程的平均成绩拿出
mysql> select Cno , AVG(Degree) Degree from score group by Cno;
+-------+---------+
| Cno | Degree |
+-------+---------+
| 3-105 | 81.5000 |
| 3-245 | 76.3333 |
| 6-166 | 81.6667 |
+-------+---------+
--->在让score表 和成绩表做连接 然后再对相同课程Degree 和平均成绩进行比较
select Sno,score.Cno Cno ,score.Degree from score join
(select Cno , AVG(Degree) Degree from score group by Cno) as s2
on score.Cno = s2.Cno AND score.Degree <s2.Degree;
--->最终结果显示:
mysql> select Sno,score.Cno Cno ,score.Degree from score join
-> (select Cno , AVG(Degree) Degree from score group by Cno) as s2
-> on score.Cno = s2.Cno AND score.Degree <s2.Degree;
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 109 | 3-105 | 76 |
| 103 | 3-105 | 64 |
| 109 | 3-105 | 78 |
| 105 | 6-166 | 79 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
34.查询所有任课教师的Tname和Depart
--->解析 就是有讲过课老师的Tname 和 Depart
select Tname,Depart
from teacher t,course c
where t.Tno = c.Tno AND c.Cno in (select Cno from score);
--->最终结果显示:
mysql> select Tname,Depart
-> from teacher t,course c
-> where t.Tno = c.Tno AND c.Cno in (select Cno from score);
+--------+-----------------+
| Tname | Depart |
+--------+-----------------+
| 李诚 | 计算机系 |
| 王萍 | 计算机系 |
| 张旭 | 电子工程系 |
+--------+-----------------+
35.查询所有未讲课教师的Tname和Depart
select Tname,Depart
from teacher t,course c
where t.Tno = c.Tno AND c.Cno not in (select Cno from score);
--->最终结果显示:
mysql> select Tname,Depart
-> from teacher t,course c
-> where t.Tno = c.Tno AND c.Cno not in (select Cno from score);
+--------+-----------------+
| Tname | Depart |
+--------+-----------------+
| 刘冰 | 电子工程系 |
+--------+-----------------+
1 row in set (0.00 sec)
36.查询至少有2名男生的班号
select Class
from student
where Ssex = '男' group by Class
having count(1) > 1;
--->最终结果显示:
mysql> select Class
-> from student
-> where Ssex = '男' group by Class
-> having count(1) > 1;
+-------+
| Class |
+-------+
| 95031 |
| 95033 |
+-------+
37.查询student表中不姓''王''的同学记录
select *
from student
where Sname not like '王%';
--->最终结果显示:
mysql> select *
-> from student
-> where Sname not like '王%';
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
+-----+--------+------+---------------------+-------+
4 rows in set (0.00 sec)
38.查询student表中每个学生的姓名和年龄
select Sname,
year(now()) - year(Sbirthday) as age
from student;
--->最终结果显示:
mysql> select Sname,
-> year(now()) - year(Sbirthday) as age
-> from student;
+--------+------+
| Sname | age |
+--------+------+
| 李军 | 44 |
| 陆君 | 46 |
| 匡明 | 45 |
| 王丽 | 44 |
| 曾华 | 43 |
| 王芳 | 45 |
+--------+------+
6 rows in set (0.00 sec)
39.查询student表中最大和最小的Sbirthday日期值
select max(Sbirthday),min(sbirthday) from student;
--->最终结果:
mysql> select max(Sbirthday),min(sbirthday) from student;
+---------------------+---------------------+
| max(Sbirthday) | min(sbirthday) |
+---------------------+---------------------+
| 1977-09-01 00:00:00 | 1974-06-03 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)
40.以班号和年龄从大到小的顺讯查询student表中的全部记录
select *
from student
order by Class desc , Sbirthday;
--->最终结果:
mysql> select *
-> from student
-> order by Class desc , Sbirthday;
+-----+--------+------+---------------------+-------+
| Sno | Sname | Ssex | Sbirthday | Class |
+-----+--------+------+---------------------+-------+
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
41.查询'男'教师及其所上的课程
select Tname,Cname
from teacher t,course c
where t.Tno = c.Tno;
--->最终结果:
mysql> select Tname,Cname
-> from teacher t,course c
-> where t.Tno = c.Tno;
+--------+-----------------+
| Tname | Cname |
+--------+-----------------+
| 王萍 | 计算机导论 |
| 李诚 | 操作系统 |
| 张旭 | 数字电路 |
| 刘冰 | 高等数学 |
+--------+-----------------+
4 rows in set (0.00 sec)
42.查询最高分同学的Sno,Cno和Degree列
select Sno,Cno,Degree
from score
having Degree = (select max(Degree) from score);
--->最终结果:
mysql> select Sno,Cno,Degree
-> from score
-> having Degree = (select max(Degree) from score);
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
+-----+-------+--------+
1 row in set (0.01 sec)
43.查询和'李军'同性别的所有同学的Sname
select Sname
from student
where Ssex=(select Ssex from student where Sname='李军');
--->最终结果:
mysql> select Sname
-> from student
-> where Ssex=(select Ssex from student where Sname='李军');
+--------+
| Sname |
+--------+
| 李军 |
| 陆君 |
| 匡明 |
| 曾华 |
+--------+
44.查询和'李军'同性别并同班的同学Sname
select Sname
from student
where (Class,Ssex) in(select Class,Ssex from student where Sname='李军');
--->结果显示:
mysql> select Sname
-> from student
-> where (Class,Ssex) in(select Class,Ssex from student where Sname='李军');
+--------+
| Sname |
+--------+
| 李军 |
| 曾华 |
+--------+
45.查询所有选修'计算机导论'课程的'男'同学的成绩表
select score.*
from course,score,student
where course.Cname = '计算机导论' AND course.Cno = score.Cno AND score.Sno = student.Sno AND student.Ssex = '男';
---> 最终结果:
mysql> select score.*
-> from course,score,student
-> where course.Cname = '计算机导论' AND course.Cno = score.Cno AND score.Sno = student.Sno AND student.Ssex = '男';
+-----+-------+--------+
| Sno | Cno | Degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 103 | 3-105 | 64 |
| 105 | 3-105 | 88 |
| 105 | 3-105 | 91 |
+-----+-------+--------+
4 rows in set (0.00 sec)
--->另一种解法:
mysql> select Sno,Cno,degree from score where Cno=( select Cno from course where Cname='计算机导论') and Sno in (select Sno from student where Ssex='男');
+-----+-------+--------+
| Sno | Cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 103 | 3-105 | 64 |
| 105 | 3-105 | 88 |
| 105 | 3-105 | 91 |
+-----+-------+--------+
4 rows in set (0.00 sec)
创表语句和插入语句
#创建数据库:
mysql> create database sql_exercise02 charset utf8;
Query OK, 1 row affected (0.00 sec)
mysql> use sql_exercise02;
Database changed
#建学生信息表student
create table student
(
Sno varchar(20) not null primary key,
Sname varchar(20) not null,
Ssex varchar(20) not null,
Sbirthday datetime,
Class varchar(20)
);
#建立教师表
create table teacher
(
Tno varchar(20) not null primary key,
Tname varchar(20) not null,
Tsex varchar(20) not null,
Tbirthday datetime,
Prof varchar(20),
Depart varchar(20) not null
);
#建立课程表course
create table course
(
Cno varchar(20) not null primary key,
Cname varchar(20) not null,
Tno varchar(20) not null,
foreign key(Tno) references teacher(Tno)
);
#建立成绩表
create table score
(
Sno varchar(20) not null ,
foreign key(Sno) references student(Sno),
Cno varchar(20) not null,
foreign key(Cno) references course(Cno),
Degree decimal
);
#添加学生信息
insert into student values('108','曾华','男','1977-09-01','95033');
insert into student values('105','匡明','男','1975-10-02','95031');
insert into student values('107','王丽','女','1976-01-23','95033');
insert into student values('101','李军','男','1976-02-20','95033');
insert into student values('109','王芳','女','1975-02-10','95031');
insert into student values('103','陆君','男','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','3-105','64');
insert into score values('105','3-105','91');
insert into score values('109','3-105','78');
insert into score values('103','6-166','85');
insert into score values('105','6-166','79');
insert into score values('109','6-166','81');
网友评论