设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。
以下代码本人均实测,可能部分答案仍有优化空间,欢迎留言指正。
表(一)Student (学生表) :
属性名 | 数据类型 | 可否为空 | 含 义 |
---|---|---|---|
Sno | varchar (20) | 否 | 学号(主键) |
Sname | varchar (20) | 否 | 学生姓名 |
Ssex | varchar (20) | 否 | 学生性别 |
Sbirthday | datetime | 可 | 学生出生年月 |
Class | varchar (20) | 可 | 学生所在班级 |
表(二)Course(课程表):
属性名 | 数据类型 | 可否为空 | 含 义 |
---|---|---|---|
Cno | varchar(20) | 否 | 课程号(主键) |
Cname | varchar (20) | 否 | 课程名称 |
Tno | varchar (20) | 否 | 教工编号(外键) |
表(三)Score(成绩表):
属性名 | 数据类型 | 可否为空 | 含 义 |
---|---|---|---|
Sno | varchar (20) | 否 | 学号(外键) |
Cno | varchar (20) | 否 | 课程号(外键) |
Degree | Decimal(4,1) | 可 | 成绩 |
主键:Sno+ Cno |
表(四)Teacher(教师表):
属性名 | 数据类型 | 可否为空 | 含 义 |
---|---|---|---|
Tno | varchar (20) | 否 | 教工编号(主码) |
Tname | varchar (20) | 否 | 教工姓名 |
Tsex | varchar (20) | 否 | 教工性别 |
Tbirthday | datetime | 可 | 教工出生年月 |
Prof | varchar (20) | 可 | 职称 |
Depart | varchar (20) | 否 | 教工所在部门 |
4个表中对应的数据如下:
表(一)Student:
Sno | Sname | Ssex | Sbirthday | class |
---|---|---|---|---|
108 | 曾华 | 男 | 1977-09-01 | 95033 |
105 | 匡明 | 男 | 1975-10-02 | 95031 |
107 | 王丽 | 女 | 1976-01-23 | 95033 |
101 | 李军 | 男 | 1976-02-20 | 95033 |
109 | 王芳 | 女 | 1975-02-10 | 95031 |
103 | 陆君 | 男 | 1974-06-03 | 95031 |
表(二)Course:
Cno | Cname | Tno |
---|---|---|
3-105 | 计算机导论 | 825 |
3-245 | 操作系统 | 804 |
6-166 | 数字电路 | 856 |
9-888 | 高等数学 | 831 |
表(三)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 |
101 | 3-105 | 64 |
107 | 3-105 | 91 |
108 | 3-105 | 78 |
101 | 6-166 | 85 |
107 | 6-166 | 79 |
108 | 6-166 | 81 |
表(四)Teacher:
Tno | Tname | Tsex | Tbirthday | Prof | Depart |
---|---|---|---|---|---|
804 | 李诚 | 男 | 1958-12-02 | 副教授 | 计算机系 |
856 | 张旭 | 男 | 1969-03-12 | 讲师 | 电子工程系 |
825 | 王萍 | 女 | 1972-05-05 | 助教 | 计算机系 |
831 | 刘冰 | 女 | 1977-08-14 | 助教 | 电子工程系 |
#建学生信息表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 primary key,
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');
以下为练习题部分
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT
Sname,
Ssex,
Class
FROM
Student;
2、 查询教师所有的单位即不重复的Depart列。
SELECT DISTINCT
Depart
FROM
teacher;
3、 查询Student表的所有记录。
select * from student
4、 查询Score表中成绩在60到80之间的所有记录。
SELECT
*
FROM
score
WHERE
degree BETWEEN 60
AND 80;
5、 查询Score表中成绩为85,86或88的记录。
SELECT
*
FROM
score
WHERE
degree IN (85, 86, 88);
6、 查询Student表中“95031”班或性别为“女”的同学记录。
SELECT
*
FROM
student
WHERE
class = "95031"
OR ssex = "女";
7、 以Class降序查询Student表的所有记录。
SELECT
*
FROM
student
ORDER BY
class DESC;
8、 以Cno升序、Degree降序查询Score表的所有记录。
SELECT
*
FROM
score
ORDER BY
cno,
degree DESC;
9、 查询“95031”班的学生人数。
SELECT
COUNT(1)
FROM
student
WHERE
class = "95031";
10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
#方法1:排序
SELECT
sno,
cno
FROM
score
ORDER BY
degree DESC
LIMIT 0,
1;
#方法2:子查询
SELECT
sno,
cno
FROM
score
WHERE
degree = (SELECT MAX(degree) FROM score);
11、 查询每门课的平均成绩。
SELECT
cno,
AVG(degree)
FROM
score
GROUP BY
cno;
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT cno,COUNT(sno),avg(degree)
FROM score
GROUP BY
cno
HAVING
COUNT(sno) >= 5
AND cno LIKE "3%"
13、查询分数大于70,小于90的Sno列。
SELECT
sno
FROM
score
WHERE
degree > 70
AND degree < 90;
14、查询所有学生的Sname、Cno和Degree列。
#方法1:连接
SELECT
sname,
cno,
degree
FROM
student
LEFT JOIN score ON student.sno = score.sno
#方法2:子查询
SELECT
sname,
cno,
degree
FROM
student,
score
WHERE
student.sno = score.sno
15、查询“95031”班学生的平均分。
#方法1:连接+分组
SELECT
class,
avg(degree)
FROM
student
LEFT JOIN score ON student.sno = score.sno
GROUP BY
class
HAVING
class = "95031";
#方法2:
SELECT
class,
avg(degree)
FROM
student,
score
WHERE
student.sno = score.sno
AND class = "95031";
#方法3:子查询
SELECT
avg(degree)
FROM
score
WHERE
sno IN (
SELECT
sno
FROM
student
WHERE
class = "95031"
)
16、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
SELECT
*
FROM
student
WHERE
sno IN (
SELECT
sno
FROM
score
WHERE
cno = "3-105"
AND sno <> "109"
AND degree > ANY (
SELECT
degree
FROM
score
WHERE
sno = "109"
)
);
17、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
SELECT
*
FROM
score
WHERE
degree > ANY (
SELECT
degree
FROM
score
WHERE
sno = "109"
AND cno = "3-105"
)
AND sno <> "109";
18、查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT
sno,
sname,
sbirthday
FROM
student
WHERE
YEAR (sbirthday) IN (
SELECT
YEAR (sbirthday)
FROM
student
WHERE
sno IN (108, 101)
)
AND sno NOT IN (108, 101);
19、查询“张旭”教师任课的学生成绩。
#方法1:子查询
SELECT
*
FROM
score
WHERE
cno IN (
SELECT
cno
FROM
course
WHERE
tno IN (
SELECT
tno
FROM
teacher
WHERE
tname = "张旭"
)
);
#方法2:内连接
SELECT
score.*
FROM
score
INNER JOIN course ON score.cno = course.cno
INNER JOIN teacher ON course.tno = teacher.tno
WHERE
teacher.tname = "张旭";
20、查询选修某课程的同学人数多于5人的教师姓名。
#方法1:子查询
SELECT
tname
FROM
teacher
WHERE
tno IN (
SELECT
tno
FROM
course
WHERE
cno IN (
SELECT
cno
FROM
score
GROUP BY
cno
HAVING
count(1) >= 5
)
);
21、查询95033班和95031班全体学生的记录。
SELECT
*
FROM
student
WHERE
class IN (95033, 95031);
22、 查询存在有85分以上成绩的课程Cno。
SELECT
*
FROM
score
WHERE
degree > 85;
23、查询出“计算机系“教师所教课程的成绩表。
SELECT
*
FROM
score
WHERE
cno IN (
SELECT
cno
FROM
course
WHERE
tno IN (
SELECT
tno
FROM
teacher
WHERE
depart = "计算机系"
)
);
24、查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。
建议画图,利用集合运算
#方法1:交集的补集
SELECT
tname,
prof
FROM
teacher
WHERE
prof NOT IN (
SELECT
prof
FROM
teacher
WHERE
prof IN (
SELECT
prof
FROM
teacher
WHERE
depart = "计算机系"
)
AND depart = "电子工程系"
);
#方法2:A与B交集的补集=A的补集并上B的补集
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="计算机系");
#方法3:按职称分组。只出现一次的即为不同职称
SELECT
tname,
prof
FROM
teacher
WHERE
depart IN (
"计算机系",
"电子工程系"
)
GROUP BY
prof
HAVING
count(prof) = 1;
25、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
SELECT
*
FROM
score
WHERE
cno = "3-105"
AND degree > (
SELECT
min(degree)
FROM
score
WHERE
cno = "3-245"
)
ORDER BY
degree;
26、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree。
SELECT
*
FROM
score
WHERE
cno = "3-105"
AND degree > (
SELECT
max(degree)
FROM
score
WHERE
cno = "3-245"
);
27、 查询所有教师和同学的name、sex和birthday。
SELECT
sname AS NAME,
ssex AS sex,
sbirthday AS birthday
FROM
student
UNION
SELECT
tname,
tsex,
tbirthday
FROM
teacher;
28、查询所有“女”教师和“女”同学的name、sex和birthday。
SELECT
sname AS NAME,
ssex AS sex,
sbirthday AS birthday
FROM
student
WHERE
ssex = "女"
UNION
SELECT
tname,
tsex,
tbirthday
FROM
teacher
WHERE
tsex = "女"
29、 查询成绩比该课程平均成绩低的同学的成绩表。
SELECT
*
FROM
score AS x
WHERE
x.degree < (
SELECT
avg(degree)
FROM
score AS y
WHERE
x.cno = y.cno
);
这是一种特殊形式的父子表连接(自连接)SQL选择查询写法。对于这种特殊的写法,数据库引擎会以特殊的方式检索父查询表里的数据。如果搞不清楚这种特殊的检索方式,我们很难从该SQL语句的表面逻辑理出个中道理。
现在我们来分拆该SQL语句里的父查询和子查询
1)语句中的父查询
select * from score x where degree<"子查询获得的一个数据值"
2)语句中的子查询
select avg(degree) from score y where x.cno=y.cno
请注意这个子查询的from子句里只有一张表 y ,但是where子句里却出现了第二张表 x ,
如果单独运行这个子查询,因为子查询没有列出表x,系统会要求输入x.cno或者直接报错,反正无法顺利执行,但是表x可以在父查询里的from子句中找到,面对这种情况数据库引擎会采取逐条取主查询记录与子查询实施比对以确定是否检出该条记录,最后汇总各次检索的结果输出整个记录集。
这个特殊的SQL语句检索过程大致如下:
取出首条记录的x.cno用作过滤,子查询里以avg函数得到该课程的平均分,主查询以分数比对平均分,满足条件保留否则抛弃(degree小于平均分的留下);
跟着判断父查询表下一条记录,处理过程相同,最后合并各次判断结果从而的到最终结果。
这种特殊的写法可以规避输出包含非分组字段,而分组不得输出非分组字段的矛盾。
30、 查询所有任课教师的Tname和Depart。
#有分数的课程,则为有任课老师的课程
SELECT
tname,
depart
FROM
teacher
WHERE
tno IN (
SELECT
tno
FROM
course
WHERE
cno IN (
SELECT
cno
FROM
score
WHERE
degree <> 0
)
);
31 、查询所有未讲课的教师的Tname和Depart。
#与上个相反
SELECT
tname,
depart
FROM
teacher
WHERE
tno IN (
SELECT
tno
FROM
course
WHERE
cno NOT IN (
SELECT
cno
FROM
score
WHERE
degree <> 0
)
);
32、查询至少有2名男生的班号。
SELECT
class
FROM
student
WHERE
ssex = "男"
GROUP BY
class
HAVING
count(*) >= 2;
33、查询Student表中不姓“王”的同学记录。
SELECT
*
FROM
student
WHERE
sname NOT LIKE "王%";
34、查询Student表中每个学生的姓名和年龄。
SELECT
sname,
(YEAR (curdate()) - YEAR (sbirthday)) as birth
FROM
student;
35、查询Student表中最大和最小的Sbirthday日期值。
SELECT
date(max(sbirthday)),
date(min(sbirthday))
FROM
student;
36、以班号和年龄从大到小的顺序查询Student表中的全部记录。
SELECT
*
FROM
student
ORDER BY
class,
sbirthday;
37、查询“男”教师及其所上的课程。
SELECT
*
FROM
teacher
LEFT JOIN course ON teacher.tno = course.tno
WHERE
tsex = "男";
38、查询最高分同学的Sno、Cno和Degree列。
SELECT
*
FROM
score
ORDER BY
degree DESC
LIMIT 0,1;
39、查询和“李军”同性别的所有同学的Sname。
SELECT
sname
FROM
student
WHERE
ssex = (
SELECT
ssex
FROM
student
WHERE
sname = "李军"
);
40、查询和“李军”同性别并同班的同学Sname。
SELECT
sname
FROM
student
WHERE
ssex = (
SELECT
ssex
FROM
student
WHERE
sname = "李军"
)
AND class = (
SELECT
class
FROM
student
WHERE
sname = "李军"
);
41、查询所有选修“计算机导论”课程的“男”同学的成绩表。
方法1:子查询
SELECT
*
FROM
score
WHERE
sno IN (
SELECT
sno
FROM
student
WHERE
ssex = "男"
)
AND cno = (
SELECT
cno
FROM
course
WHERE
cname = "计算机导论"
);
方法2:连接
SELECT
*
FROM
score
LEFT JOIN student ON score.sno = student.sno
LEFT JOIN course ON score.cno = course.cno
WHERE
student.ssex = "男"
AND course.cname = "计算机导论";