练手系列——mysql

最近打算采用关系型数据库来理一下公司的运营数据,先拿点东西练手找感觉。下面是几个关于学生课业的表,需要建立一个数据库,含有以下四张表,在此基础上做练习。

表结构

第一行为表名,第二行为字段名
student
sno sname ssex sbirthday class

course
cno cname tno

score
sno cno degree

teacher
tno tname tbirthday depart

建立数据库

  CREATE DATABASE my_practicedb;

建立表

USE  my_practicedb;

CREATE TABLE STUDENT
(SNO VARCHAR(3) NOT NULL, 
SNAME VARCHAR(4) NOT NULL,
SSEX VARCHAR(2) NOT NULL, 
SBIRTHDAY DATETIME,
CLASS VARCHAR(5));

CREATE TABLE COURSE
(CNO VARCHAR(5) NOT NULL, 
CNAME VARCHAR(10) NOT NULL, 
TNO VARCHAR(10) NOT NULL);

CREATE TABLE SCORE 
(SNO VARCHAR(3) NOT NULL, 
CNO VARCHAR(5) NOT NULL, 
DEGREE NUMERIC(10, 1) NOT NULL); 

CREATE TABLE TEACHER 
(TNO VARCHAR(3) NOT NULL, 
TNAME VARCHAR(4) NOT NULL, TSEX VARCHAR(2) NOT NULL, 
TBIRTHDAY DATETIME NOT NULL, PROF VARCHAR(6), 
DEPART VARCHAR(10) NOT NULL);

插入数据

INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' 
,'男' ,2017-09-01,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' 
,'男' ,1975-10-02,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' 
,'女' ,1976-01-23,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' 
,'男' ,1976-02-20,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' 
,'女' ,1975-02-10,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' 
,'男' ,1974-06-03,95031);

INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825)
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);

INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);

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

查询

1、 查询Student表中的所有记录的Sname、Ssex和Class列。
mysql> SELECT sname,ssex,class FROM student;
+--------+------+-------+
| sname | ssex | class |
+--------+------+-------+
| 曾华 | 男 | 95033 |
| 匡明 | 男 | 95031 |
| 王丽 | 女 | 95033 |
| 李军 | 男 | 95033 |
| 王芳 | 女 | 95031 |
| 陆君 | 男 | 95031 |
+--------+------+-------+
6 rows in set (0.03 sec)
*SELECT基础用法
SELECT [字段1],[字段2],... FROM [TABLENAME] ;

2、 查询教师所有的单位即不重复的Depart列。
mysql> SELECT distinct depart FROM teacher;
+-----------------+
| depart |
+-----------------+
| 计算机系 |
| 电子工程系 |
+-----------------+
2 rows in set (0.06 sec)
mysql> SELECT depart FROM teacher;
+-----------------+
| depart |
+-----------------+
| 计算机系 |
| 电子工程系 |
| 计算机系 |
| 电子工程系 |
+-----------------+
4 rows in set (0.00 sec)
*去重, distinct

3、 查询Student表的所有记录。
mysql> SELECT * FROM student;
+-----+--------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+--------+------+------------+-------+
| 108 | 曾华 | 男 | 2017-12-11 | 95033 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
+-----+--------+------+------------+-------+
6 rows in set (0.00 sec)
SELECT基本用法,通配符

4、 查询Score表中成绩在60到80之间的所有记录。
mysql> SELECT * FROM score where (degree >=60 and degree<=80);
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 109 | 3-105 | 76.0 |
| 101 | 3-105 | 64.0 |
| 108 | 3-105 | 78.0 |
| 107 | 6-106 | 79.0 |
+-----+-------+--------+
6 rows in set (0.06 sec)
*条件查询 and

5、 查询Score表中成绩为85,86或88的记录。
mysql> SELECT * FROM score where (degree = 85 or degree = 86 or degree = 88);
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 103 | 3-245 | 86.0 |
| 105 | 3-105 | 88.0 |
| 101 | 6-166 | 85.0 |
+-----+-------+--------+
3 rows in set (0.00 sec)
*条件查询 or

6、 查询Student表中“95031”班或性别为“女”的同学记录。
mysql> SELECT * FROM student where (class='95031' or ssex='女');
+-----+--------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+--------+------+------------+-------+
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
+-----+--------+------+------------+-------+
4 rows in set (0.01 sec)
*条件查询 or

7、 以Class降序查询Student表的所有记录。
mysql> SELECT * FROM student ORDER BY class DESC;
+-----+--------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+--------+------+------------+-------+
| 108 | 曾华 | 男 | 2017-12-11 | 95033 |
| 107 | 王丽 | 女 | 1976-01-23 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 | 95033 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
+-----+--------+------+------------+-------+
6 rows in set (0.08 sec)
*排序, ORDER BY ... DESC

8、 以Cno升序、Degree降序查询Score表的所有记录。
mysql> SELECT * FROM score ORDER BY cno,degree DESC;
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 103 | 3-105 | 92.0 |
| 107 | 3-105 | 91.0 |
| 105 | 3-105 | 88.0 |
| 108 | 3-105 | 78.0 |
| 109 | 3-105 | 76.0 |
| 101 | 3-105 | 64.0 |
| 103 | 3-245 | 86.0 |
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 107 | 6-106 | 79.0 |
| 101 | 6-166 | 85.0 |
| 108 | 6-166 | 81.0 |
+-----+-------+--------+
12 rows in set (0.02 sec)
*排序时与列名的先后位置有关系。SELECT * FROM score ORDER BY cno,degree DESC;
前者默认升序排列

mysql> SELECT * FROM score ORDER BY degree DESC,cno;
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 103 | 3-105 | 92.0 |
| 107 | 3-105 | 91.0 |
| 105 | 3-105 | 88.0 |
| 103 | 3-245 | 86.0 |
| 101 | 6-166 | 85.0 |
| 108 | 6-166 | 81.0 |
| 107 | 6-106 | 79.0 |
| 108 | 3-105 | 78.0 |
| 109 | 3-105 | 76.0 |
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 101 | 3-105 | 64.0 |
+-----+-------+--------+
12 rows in set (0.00 sec)

9、 查询“95031”班的学生人数。
mysql> SELECT COUNT(sno) FROM student WHERE class='95031';
+------------+
| COUNT(sno) |
+------------+
| 3 |
+------------+
1 row in set (0.05 sec)
*计数,count

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.03 sec)
*max等函数的用法

11、查询‘3-105’号课程的平均分。
mysql> SELECT AVG(degree) FROM score WHERE cno='3-105';
+-------------+
| AVG(degree) |
+-------------+
| 81.50000 |
+-------------+
1 row in set (0.04 sec)
*avg等函数的用法

12、score表中至少有5名学生选修的并以3开头的课程的平均分数。
mysql> SELECT AVG(degree) FROM score WHERE cno =(select cno from score group by cno having count(*)>=5) and cno like '3%';
+-------------+
| AVG(degree) |
+-------------+
| 81.50000 |
+-------------+
1 row in set (0.03 sec)
*分组,group by,having count() 通配符 http://www.w3school.com.cn/sql/sql_groupby.asp

13、查询最低分大于70,最高分小于90的Sno列。
mysql> SELECT SNO FROM SCORE GROUP BY SNO HAVING MIN(DEGREE)>=70 AND MAX(DEGREE)<=90;
+-----+
| SNO |
+-----+
| 105 |
| 108 |
+-----+
2 rows in set (0.00 sec)
*group by,having

14、查询所有学生的Sname、Cno和Degree列。
mysql> SELECT SNAME,CNO,DEGREE FROM STUDENT,SCORE WHERE STUDENT.SNO=SCORE.SNO;
+--------+-------+--------+
| SNAME | CNO | DEGREE |
+--------+-------+--------+
| 陆君 | 3-245 | 86.0 |
| 匡明 | 3-245 | 75.0 |
| 王芳 | 3-245 | 68.0 |
| 陆君 | 3-105 | 92.0 |
| 匡明 | 3-105 | 88.0 |
| 王芳 | 3-105 | 76.0 |
| 李军 | 3-105 | 64.0 |
| 王丽 | 3-105 | 91.0 |
| 曾华 | 3-105 | 78.0 |
| 李军 | 6-166 | 85.0 |
| 王丽 | 6-106 | 79.0 |
| 曾华 | 6-166 | 81.0 |
+--------+-------+--------+
12 rows in set (0.05 sec)

15、查询所有学生的Sno、Cname和Degree列。
mysql> SELECT SNO,CNAME,DEGREE FROM COURSE,SCORE WHERE COURSE.CNO=SCORE.CNO;
+-----+-----------------+--------+
| SNO | CNAME | DEGREE |
+-----+-----------------+--------+
| 103 | 操作系统 | 86.0 |
| 105 | 操作系统 | 75.0 |
| 109 | 操作系统 | 68.0 |
| 103 | 计算机导论 | 92.0 |
| 105 | 计算机导论 | 88.0 |
| 109 | 计算机导论 | 76.0 |
| 101 | 计算机导论 | 64.0 |
| 107 | 计算机导论 | 91.0 |
| 108 | 计算机导论 | 78.0 |
| 101 | 数据电路 | 85.0 |
| 108 | 数据电路 | 81.0 |
+-----+-----------------+--------+
11 rows in set (0.01 sec)

16、查询所有学生的Sname、Cname和Degree列。
mysql> SELECT SNAME,CNAME,DEGREE FROM STUDENT,COURSE,SCORE WHERE STUDENT.SNO=SCORE.SNO AND COURSE.CNO=SCORE.CNO;
+--------+-----------------+--------+
| SNAME | CNAME | DEGREE |
+--------+-----------------+--------+
| 曾华 | 计算机导论 | 78.0 |
| 曾华 | 数据电路 | 81.0 |
| 匡明 | 操作系统 | 75.0 |
| 匡明 | 计算机导论 | 88.0 |
| 王丽 | 计算机导论 | 91.0 |
| 李军 | 计算机导论 | 64.0 |
| 李军 | 数据电路 | 85.0 |
| 王芳 | 操作系统 | 68.0 |
| 王芳 | 计算机导论 | 76.0 |
| 陆君 | 操作系统 | 86.0 |
| 陆君 | 计算机导论 | 92.0 |
+--------+-----------------+--------+
11 rows in set (0.00 sec)

17、查询“95033”班所选课程的平均分。
mysql> SELECT AVG(DEGREE) FROM STUDENT,SCORE WHERE STUDENT.SNO=SCORE.SNO AND CLASS LIKE '95033';
+-------------+
| AVG(DEGREE) |
+-------------+
| 79.66667 |
+-------------+
1 row in set (0.02 sec)

18、假设使用如下命令建立了一个grade表:
create table grade(low number(3,0) ,upp number(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’);
commit;
现查询所有同学的Sno、Cno和rank列。
mysql> SELECT SNO,CNO,RANK FROM score,GRADE where score.degree>=grade.low and score.degree<=grade.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 |
| 101 | 3-105 | D |
| 107 | 3-105 | A |
| 108 | 3-105 | C |
| 101 | 6-166 | B |
| 107 | 6-106 | C |
| 108 | 6-166 | B |
+-----+-------+------+
12 rows in set (0.01 sec)
*比较查询

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
mysql> SELECT * FROM STUDENT,SCORE WHERE STUDENT.SNO =SCORE.SNO AND CNO='3-105' AND DEGREE>(SELECT DEGREE FROM SCORE WHERE SCORE.SNO = '109'AND CNO='3-105');
+-----+--------+------+------------+-------+-----+-------+--------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS | SNO | CNO | DEGREE |
+-----+--------+------+------------+-------+-----+-------+--------+
| 108 | 曾华 | 男 | 2017-12-11 | 95033 | 108 | 3-105 | 78.0 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 | 105 | 3-105 | 88.0 |
| 107 | 王丽 | 女 | 1976-01-23 | 95033 | 107 | 3-105 | 91.0 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 | 103 | 3-105 | 92.0 |
+-----+--------+------+------------+-------+-----+-------+--------+
4 rows in set (0.00 sec)

20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
mysql> SELECT SNO,CNO,DEGREE FROM SCORE AS A WHERE A.DEGREE < (SELECT MAX(DEGREE) FROM SCORE AS B WHERE B.CNO=A.CNO GROUP BY B.CNO) AND A.SNO IN (SELECT C.SNO FROM SCORE AS C GROUP BY C.SNO HAVING COUNT(C.CNO)>=1);
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 105 | 3-105 | 88 |
| 109 | 3-105 | 76 |
| 101 | 3-105 | 64 |
| 107 | 3-105 | 91 |
| 108 | 3-105 | 78 |
| 108 | 6-166 | 81 |
+-----+-------+--------+
8 rows in set

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
mysql> SELECT * FROM SCORE WHERE DEGREE>(SELECT DEGREE FROM SCORE WHERE SNO='109'AND CNO='3-105');
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 103 | 3-245 | 86.0 |
| 103 | 3-105 | 92.0 |
| 105 | 3-105 | 88.0 |
| 107 | 3-105 | 91.0 |
| 108 | 3-105 | 78.0 |
| 101 | 6-166 | 85.0 |
| 107 | 6-106 | 79.0 |
| 108 | 6-166 | 81.0 |
+-----+-------+--------+
8 rows in set (0.01 sec)

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
mysql> SELECT SNO,SNAME,SBIRTHDAY FROM STUDENT WHERE YEAR(SBIRTHDAY)=(SELECT YEAR(SBIRTHDAY) FROM STUDENT WHERE SNO='108');
+-----+--------+------------+
| SNO | SNAME | SBIRTHDAY |
+-----+--------+------------+
| 108 | 曾华 | 2017-12-11 |
+-----+--------+------------+
1 row in set (0.05 sec)

mysql> SELECT * FROM STUDENT;
+-----+--------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+--------+------+------------+-------+
| 108 | 曾华 | 男 | 2017-12-11 | 95033 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
+-----+--------+------+------------+-------+
6 rows in set (0.00 sec)

本人除外:
mysql> SELECT SNO,SNAME,SBIRTHDAY FROM STUDENT WHERE YEAR(SBIRTHDAY)=(SELECT YEAR(SBIRTHDAY) FROM STUDENT WHERE SNO='108') AND SNO<>'108';
Empty set (0.01 sec)

23、查询“张旭“教师任课的学生成绩。
mysql> SELECT * FROM SCORE WHERE CNO=(SELECT CNO FROM COURSE WHERE TNO=(SELECT TNO FROM TEACHER WHERE TNAME='张旭'));
+-----+-------+--------+
| SNO | CNO | DEGREE |
+-----+-------+--------+
| 101 | 6-166 | 85.0 |
| 108 | 6-166 | 81.0 |
+-----+-------+--------+
2 rows in set (0.01 sec)

24、查询选修某课程的同学人数多于5人的教师姓名。
mysql> SELECT TNAME FROM TEACHER,COURSE WHERE TEACHER.TNO=COURSE.TNO AND CNO IN (SELECT CNO FROM SCORE GROUP BY CNO HAVING COUNT(SNO)>=5);
+--------+
| TNAME |
+--------+
| 王萍 |
+--------+
1 row in set (0.02 sec)

25、查询95033班和95031班全体学生的记录。
mysql> SELECT * FROM STUDENT WHERE CLASS='95033' OR CLASS='95031';
+-----+--------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+--------+------+------------+-------+
| 108 | 曾华 | 男 | 2017-12-11 | 95033 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
+-----+--------+------+------------+-------+
6 rows in set (0.03 sec)

26、查询存在有85分以上成绩的课程Cno.
mysql> SELECT DISTINCT CNO FROM SCORE WHERE DEGREE >85;
+-------+
| CNO |
+-------+
| 3-245 |
| 3-105 |
+-------+
2 rows in set (0.00 sec)

27、查询出“计算机系“教师所教课程的成绩表。
mysql> SELECT SNAME,CNAME,DEGREE,TNAME FROM STUDENT,COURSE,SCORE,TEACHER WHERE STUDENT.SNO=SCORE.SNO AND COURSE.CNO=SCORE.CNO AND TEACHER.TNO=COURSE.TNO AND DEPART='计算机系';
+--------+-----------------+--------+--------+
| SNAME | CNAME | DEGREE | TNAME |
+--------+-----------------+--------+--------+
| 曾华 | 计算机导论 | 78.0 | 王萍 |
| 匡明 | 操作系统 | 75.0 | 李诚 |
| 匡明 | 计算机导论 | 88.0 | 王萍 |
| 王丽 | 计算机导论 | 91.0 | 王萍 |
| 李军 | 计算机导论 | 64.0 | 王萍 |
| 王芳 | 操作系统 | 68.0 | 李诚 |
| 王芳 | 计算机导论 | 76.0 | 王萍 |
| 陆君 | 操作系统 | 86.0 | 李诚 |
| 陆君 | 计算机导论 | 92.0 | 王萍 |
+--------+-----------------+--------+--------+
9 rows in set (0.01 sec)

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
mysql> SELECT TNAME,PROF FROM TEACHER WHERE PROF NOT IN (SELECT PROF FROM TEACHER WHERE DEPART='计算机系' AND PROF IN (SELECT PROF FROM TEACHER WHERE DEPART='电子工程系'));
+--------+-----------+
| TNAME | PROF |
+--------+-----------+
| 李诚 | 副教授 |
| 张旭 | 讲师 |
+--------+-----------+
2 rows in set (0.00 sec)

29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
mysql> SELECT CNO,SNO,DEGREE FROM SCORE WHERE CNO='3-105' AND DEGREE>=(SELECT MAX(DEGREE) FROM SCORE WHERE CNO='3-245') ORDER BY DEGREE DESC;
+-------+-----+--------+
| CNO | SNO | DEGREE |
+-------+-----+--------+
| 3-105 | 103 | 92.0 |
| 3-105 | 107 | 91.0 |
| 3-105 | 105 | 88.0 |
+-------+-----+--------+
3 rows in set (0.03 sec)

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
mysql> SELECT CNO,SNO,DEGREE FROM SCORE WHERE CNO='3-105' AND DEGREE>(SELECT MAX(DEGREE) FROM SCORE WHERE CNO='3-245') ;
+-------+-----+--------+
| CNO | SNO | DEGREE |
+-------+-----+--------+
| 3-105 | 103 | 92.0 |
| 3-105 | 105 | 88.0 |
| 3-105 | 107 | 91.0 |
+-------+-----+--------+
3 rows in set (0.00 sec)

31、查询所有教师和同学的name、sex和birthday.
mysql> SELECT SNAME,SSEX,SBIRTHDAY FROM STUDENT UNION SELECT TNAME,TSEX,TBIRTHDAY FROM TEACHER;
+--------+------+------------+
| SNAME | SSEX | SBIRTHDAY |
+--------+------+------------+
| 曾华 | 男 | 2017-12-11 |
| 匡明 | 男 | 1970-10-02 |
| 王丽 | 女 | 1976-01-23 |
| 李军 | 男 | 1976-02-20 |
| 王芳 | 女 | 1975-02-10 |
| 陆君 | 男 | 1974-06-03 |
| 李诚 | 男 | 1958-12-02 |
| 张旭 | 男 | 1969-03-12 |
| 王萍 | 女 | 1972-05-05 |
| 刘冰 | 女 | 1977-08-14 |
+--------+------+------------+
10 rows in set (0.03 sec)

32、查询所有“女”教师和“女”同学的name、sex和birthday.
mysql> SELECT SNAME AS NAME,SSEX AS SEX,SBIRTHDAY AS BIRTHDAY FROM STUDENT where SSEX='女' UNION SELECT TNAME ,TSEX,TBIRTHDAY FROM TEACHER WHERE TSEX='女';
+--------+-----+------------+
| NAME | SEX | BIRTHDAY |
+--------+-----+------------+
| 王丽 | 女 | 1976-01-23 |
| 王芳 | 女 | 1975-02-10 |
| 王萍 | 女 | 1972-05-05 |
| 刘冰 | 女 | 1977-08-14 |
+--------+-----+------------+
4 rows in set (0.69 sec)

33、查询成绩比该课程平均成绩低的同学的成绩表。
mysql> SELECT SNAME,CNAME,A.DEGREE FROM SCORE AS A,STUDENT,COURSE WHERE STUDENT.SNO=A.SNO AND COURSE.CNO=A.CNO AND A.DEGREE < (SELECT AVG(B.DEGREE) FROM SCORE AS B WHERE B.CNO=A.CNO GROUP BY B.CNO);
+--------+-----------------+--------+
| SNAME | CNAME | DEGREE |
+--------+-----------------+--------+
| 曾华 | 计算机导论 | 78.0 |
| 曾华 | 数据电路 | 81.0 |
| 匡明 | 操作系统 | 75.0 |
| 李军 | 计算机导论 | 64.0 |
| 王芳 | 操作系统 | 68.0 |
| 王芳 | 计算机导论 | 76.0 |
+--------+-----------------+--------+
6 rows in set (0.01 sec)

34、查询所有任课教师的Tname和Depart.
mysql> SELECT TNAME,DEPART FROM TEACHER WHERE TNO IN (SELECT TNO FROM COURSE WHERE CNO IN (SELECT CNO FROM SCORE));
+--------+-----------------+
| TNAME | DEPART |
+--------+-----------------+
| 李诚 | 计算机系 |
| 张旭 | 电子工程系 |
| 王萍 | 计算机系 |
+--------+-----------------+
3 rows in set (0.03 sec)

mysql> SELECT DISTINCT TNAME,DEPART FROM TEACHER,COURSE,SCORE WHERE TEACHER.TNO=COURSE.TNO AND COURSE.CNO=SCORE.CNO;
+--------+-----------------+
| TNAME | DEPART |
+--------+-----------------+
| 李诚 | 计算机系 |
| 王萍 | 计算机系 |
| 张旭 | 电子工程系 |
+--------+-----------------+
3 rows in set (0.00 sec)

35 查询所有未讲课的教师的Tname和Depart.
mysql> SELECT TNAME,DEPART FROM TEACHER WHERE TNO NOT IN (SELECT TNO FROM COURSE WHERE CNO IN (SELECT CNO FROM SCORE));
+--------+-----------------+
| TNAME | DEPART |
+--------+-----------------+
| 刘冰 | 电子工程系 |
+--------+-----------------+
1 row in set (0.00 sec)

36、查询至少有2名男生的班号。
mysql> SELECT CLASS FROM STUDENT WHERE SSEX='男' union SELECT CLASS FROM STUDENT GROUP BY CLASS HAVING COUNT(*);
+-------+
| CLASS |
+-------+
| 95033 |
| 95031 |
+-------+
2 rows in set (0.01 sec)

37、查询Student表中不姓“王”的同学记录。
mysql> SELECT * FROM STUDENT WHERE SNAME NOT IN (SELECT SNAME FROM STUDENT WHERE SNAME LIKE '王%');
+-----+-------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+-------+------+------------+-------+
| 108 | 曾华 | 男 | 2017-12-11 | 95033 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 101 | 李军 | 男 | 1976-02-20 | 95033 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
+-----+-------+------+------------+-------+
4 rows in set

38、查询Student表中每个学生的姓名和年龄。
mysql> SELECT TIMESTAMPDIFF(YEAR,SBIRTHDAY, CURDATE())FROM STUDENT;
+------------------------------------------+
| TIMESTAMPDIFF(YEAR,SBIRTHDAY, CURDATE()) |
+------------------------------------------+
| 0 |
| 46 |
| 41 |
| 41 |
| 42 |
| 43 |
+------------------------------------------+
6 rows in set

39、查询Student表中最大和最小的Sbirthday日期值。
mysql> SELECT MAX(SBIRTHDAY),MIN(SBIRTHDAY)FROM STUDENT;
+----------------+----------------+
| MAX(SBIRTHDAY) | MIN(SBIRTHDAY) |
+----------------+----------------+
| 2017-12-11 | 1970-10-02 |
+----------------+----------------+
1 row in set

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
mysql> SELECT * FROM STUDENT ORDER BY CLASS DESC,SBIRTHDAY;
+-----+-------+------+------------+-------+
| SNO | SNAME | SSEX | SBIRTHDAY | CLASS |
+-----+-------+------+------------+-------+
| 107 | 王丽 | 女 | 1976-01-23 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 | 95033 |
| 108 | 曾华 | 男 | 2017-12-11 | 95033 |
| 105 | 匡明 | 男 | 1970-10-02 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 | 95031 |
+-----+-------+------+------------+-------+
6 rows in set

41、查询“男”教师及其所上的课程。
mysql> SELECT TNAME,CNAME FROM TEACHER,COURSE WHERE TEACHER.TNO=COURSE.TNO AND TEACHER.TSEX='男';
+-------+----------+
| TNAME | CNAME |
+-------+----------+
| 李诚 | 操作系统 |
| 张旭 | 数据电路 |
+-------+----------+
2 rows in set

42、查询最高分同学的Sno、Cno和Degree列。
mysql> SELECT SNO,CNO,MAX(DEGREE) FROM SCORE GROUP BY CNO;
+-----+-------+-------------+
| SNO | CNO | MAX(DEGREE) |
+-----+-------+-------------+
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 107 | 6-106 | 79 |
| 101 | 6-166 | 85 |
+-----+-------+-------------+
4 rows in set

43、查询和“李军”同性别的所有同学的Sname.
mysql> SELECT SNAME FROM STUDENT WHERE SSEX=(SELECT SSEX FROM STUDENT WHERE SNAME='李军');
+-------+
| SNAME |
+-------+
| 曾华 |
| 匡明 |
| 李军 |
| 陆君 |
+-------+
4 rows in set

44、查询和“李军”同性别并同班的同学Sname.
mysql> SELECT SNAME FROM STUDENT WHERE SSEX=(SELECT SSEX FROM STUDENT WHERE SNAME='李军') AND CLASS=(SELECT CLASS FROM STUDENT WHERE SNAME='李军');
+-------+
| SNAME |
+-------+
| 曾华 |
| 李军 |
+-------+
2 rows in set

45、查询所有选修“计算机导论”课程的“男”同学的成绩表
mysql> SELECT SNAME,CNAME,DEGREE FROM STUDENT,COURSE,SCORE WHERE STUDENT.SNO=SCORE.SNO AND COURSE.CNO=SCORE.CNO AND CNAME='计算机导论' AND SSEX='男';
+-------+------------+--------+
| SNAME | CNAME | DEGREE |
+-------+------------+--------+
| 陆君 | 计算机导论 | 92 |
| 匡明 | 计算机导论 | 88 |
| 李军 | 计算机导论 | 64 |
| 曾华 | 计算机导论 | 78 |
+-------+------------+--------+
4 rows in set

你可能感兴趣的:(练手系列——mysql)