练手系列——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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,948评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,371评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,490评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,521评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,627评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,842评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,997评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,741评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,203评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,534评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,673评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,339评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,955评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,770评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,000评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,394评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,562评论 2 349

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,598评论 18 399
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,227评论 0 7
  • 写作打卡第25天,现在是北京时间23:54,今天的写作任务没有按时完成。 我最近总说让我静静,我是真的特别想静静。...
    小米屋阅读 441评论 1 50
  • 在很早很早以前,刚开始听说星座的时候,以为是按照阴历的生日来计算的,所以当了很长一段时间的巨蟹座,然后又听说巨蟹座...
    晓雪姑娘阅读 726评论 0 5
  • 碧洗空如海,浮飘霭若烟。 应苍穹处舞经幡。 一路览观松翠,穷目瞭奇山。 素雪皑依旧,瑰石俊莞然。 似祎仙女倚阑干。...
    小薇_阅读 337评论 19 18