SQL练习
已知某数据库中存在如下四张表:
1
2
3
4department(dNo,dName,officeRoom,homepage)
student(sNo,sName,sex,age,dNo)
course(cNo,cName,cPNo,credit,dNo)
sc(sNo,cNo,score,recordDate)
Query
单表
1.查询所有年龄大于等于20岁的学生学号、姓名; 1
2
3select sNo, sName
from student
where age >= 20;1
2
3select sNo, sName, 2018 - age
from student
where sName like '钱%' and sex = '男';1
2
3select cName
from course
where credit > 3;1
2
3select sName
from student
where dNo is null;1
2
3select dName
from department
where homepage is null;
聚集
1.查询各个学院的平均年龄;
1
2
3select dNo, avg(age)
from student
group by dNo;1
2
3select department.dNo, department.dName, avg(age)
from student left join department on (student.dNo = department.dNo)
group by department.dNo, department.dName;1
2
3
4select sNo, avg(score)
from sc
where score is not null
group by sNo; 1
2
3
4select student.sNo, student.sName, avg(score)
from student, sc
where student.sNo = sc.sNo and sc.score is not null
group by student.sNo, student.sName;
1
2
3
4select cNo,avg(score)
from sc
where score is not null
group by cNo; 1
2
3
4select course.cNo, course.cName, avg(score)
from course, sc
where course.cNo = sc.cNo and sc.score is not null
group by course.cNo, course.cName;1
2
3select dNo, count(cNo)
from course
group by dNo; 1
2
3select department.dNo, department.dName, count(course.cNo)
from department join course using(dNo)
group by department.dNo, department.dName;1
2
3select cNo, count(sNo)
from sc
group by cNo; 1
2
3select course.cNo, course.cName, count(sc.sNo)
from sc join course using (cNo)
group by course.cNo, course.cName;
多表
1.查询“信息学院”所有学生学号与姓名; 1
2
3
4
5
6select sNo, sName
from student
where dNo in
(select dNo
from department
where dName = '信息学院');1
2
3select sNo, sName
from student, department
where student.dNo = department.dNo and department.dName = '信息学院';1
2
3select student.sNo, student.sName
from student join department using (dNo)
where department.dName = '信息学院';1
2
3select student.sNo, student.sName
from student join department on (student.dNo = department.dNo)
where department.dName = '信息学院';1
2
3
4
5
6select cNo, cName
from course
where dNo in
(select dNo
from department
where dName = '软件学院');1
2
3
4
5
6select sNo, sName
from student
where dNo in
(select dNo
from student
where sName = '陈丽');1
2
3
4
5
6select sNo, sName
from student
where age =
(select age
from student
where sName = '张三');1
2
3
4
5
6
7
8
9
10select sNo, sName
from student
where age =
(select age
from student
where sName = '张三')
and dNo <>
(select dNo
from student
where sName = '张三');1
2
3
4
5
6select cName
from course
where credit >
(select credit
from course
where cName = '离散数学');1
2
3
4
5
6
7
8
9select count(sNo)
from student
where sNo in
(select sNo
from sc
where cNo in
(select cNo
from course
where cName = '组合数学'));1
2
3
4
5
6
7
8
9select sName
from student
where not exists
(select *
from sc
where sNo = student.sNo and cNo =
(select cNo
from course
where cName = '离散数学'));1
2
3
4
5
6
7
8
9
10select cName
from course as c1
where (c1.credit <>
(select credit
from course
where cName = '算法设计与分析')
and c1.credit <>
(select credit
from course
where cName = '移动计算'));1
2
3
4
5
6
7
8select cName
from course
where cNo in
(select cNo
from sc
where score is not null
group by cNo
having avg(score) >= 90);1
2
3
4
5
6select student.sName, sc.score
from student, sc
where student.sNo = sc.sNo and sc.cNo =
(select cNo
from course
where cName = '离散数学');1
2
3select student.sName, sc.score
from (student join sc using (sNo)) join course using (cNo)
where course.cName = '离散数学';1
2
3
4
5
6select course.cName, sc.score
from course natural join sc
where sc.sNo in
(select sNo
from student
where sName = '王兵'); 1
2
3
4
5
6select course.cName, sc.score
from course join sc on course.cNo = sc.cNo
where sc.sNo in
(select sNo
from student
where sName = '王兵');1
2
3select student.sName, course.cName, sc.score
from student, course, sc
where student.sNo = sc.sNo and course.cNo = sc.cNo and sc.score < 60;1
2
3
4
5
6select s1.sName
from student s1
where s1.sNo in
(select distinct s2.sNo
from student s2, sc, department, course
where department.dName = '文学院' and course.dNo =department.dNo and course.cNo = sc.cNo and s2.sNo = sc.sNo);1
2
3select student.sName, course.cName
from student, course, department, sc
where department.dName = '信息学院' and student.dNo = department.dNo and course.dNo = department.dNo and sc.sNo = student.sNo and sc.cNo = course.cNo;1
2
3
4//包含信息学院中未选信息学院课程的学生
select student.sName, course.cName
from (student join department on (student.dNo = department.dNo)) left join (sc join course on (sc.cNo = course.cNo)) on (student.sNo = sc.sNo)
where department.dName = '信息学院' and (course.dNo = department.dNo or course.dNo is null );
1.查询所有学生及其选课信息(包括没有选课的学生); 1
2select student.*, sc.*
from student left outer join sc on (student.sNo = sc.sNo);1
2
3
4
5
6select cName
from course
where cNo in
(select cPNo
from course
where cName = '形式语言与自动机');1
2
3
4
5
6
7
8select cName
from course
where cNo in
(select cPNo
from course
where cNo in (select cPNo
from course
where cName = '形式语言与自动机'));1
2
3
4
5
6select cName
from course
where cPNo in
(select cNo
from course
where cName = '编译原理');1
2
3
4
5
6
7
8
9select cName
from course
where cPNo in
(select cNo
from course
where cPNo in
(select cNo
from course
where cName = '离散数学'));1
2
3select cName
from course
where cPNo is null;1
2
3
4
5
6
7
8
9select sName
from student
where not exists
(select *
from sc
where sNo = student.sNo and cNo =
(select cNo
from course
where cName = '形式语言与自动机'));1
2
3
4
5
6
7
8
9
10select student1.sName
from student student1, sc sc1, course course1
where course1.cName = '形式语言与自动机' and student1.sNo = sc1.sNo and course1.cNo = sc1.cNo
except
select student2.sName
from student student2, sc sc2, course course2
where course2.cNo =
(select cPNo
from course
where cName = '形式语言与自动机') and student2.sNo = sc2.sNo and sc2.cNo = course2.cNo;1
2
3
4select student.sName, sum(credit)
from (student join sc on (student.sNo = sc.sNo)) join course on (course.cNo = sc.cNo)
group by student.sNo
having sum(credit) >= 28;1
2
3
4
5
6select student.sNo, student.sName
from student join sc on (student.sNo = sc.sNo)
--将会把有一门低于85的但有三门大于85的人选进来
where sc.score is not null and sc.score > 85
group by student.sNo
having count(sc.cNo) > 3;1
2
3
4
5select student.sNo, student.sName
from student join sc on (student.sNo = sc.sNo)
where sc.score is not null
group by student.sNo
having count(sc.cNo) > 3 and min(sc.score) > 85;1
2
3
4
5
6select student.sNo, student.sName
from student join sc on (student.sNo = sc.sNo)
--出现问题如上一题所示
where sc.score is not null and sc.score >= 60
group by student.sNo
having count(sc.cNo) = 3;1
2
3
4
5select student.sNo, student.sName
from student join sc on (student.sNo = sc.sNo)
where sc.score is not null
group by student.sNo
having count(sc.cNo) = 3 and min(sc.score) >= 60;1
2
3
4select department.dName, count(student.*)
from department join student on (department.dNo = student.dNo)
group by department.dNo
having count(student.*) > 6;1
2
3
4
5
6
7
8select student1.sNo, student1.sName
from student student1 join sc sc1 on (student1.sNo = sc1.sNo)
where sc1.score is not null
group by student1.sNo
having (avg(sc1.score) >
(select avg(sc2.score)
from student student2 join sc sc2 on (student2.sNo = sc2.sNo)
where sc2.score is not null and student2.sName = '王兵'));1
2
3
4
5
6
7
8
9
10select student1.sNo, student1.sName
from student student1 join sc sc1 on (student1.sNo = sc1.sNo)
where sc1.score is not null
group by student1.sNo, student1.sName
having (avg(sc1.score) > (select max(avgScore)
from (select student2.sNo, student2.sName, avg(sc2.score)
from student student2 join sc sc2 on student2.sNo = sc2.sNo
where sc2.score is not null
group by student2.sNo, student2.sName) as s_avg(sNo, sName, avgScore)
where s_avg.sName = '王兵'));1
2
3
4
5
6
7
8
9
10select student1.sName
from student student1, sc sc1, course course1
where course1.cName = '离散数学' and student1.sNo = sc1.sNo and course1.cNo = sc1.cNo
intersect
select student2.sName
from student student2, sc sc2, course course2
where course2.cNo =
(select cPNo
from course
where cName = '编译原理') and student2.sNo = sc2.sNo and sc2.cNo = course2.cNo;1
2
3select avg(score)
from sc, course, student, department
where department.dName = '软件学院' and student.dNo = department.dNo and course.cName = '离散数学' and sc.cNo = course.cNo and student.sNo = sc.sNo and sc.score is not null;1
2
3
4
5select student1.sName, student1.age, department1.dName
from student student1 left join department department1 on (student1.dNo = department1.dNo)
where student1.age not in (select student2.age
from student student2 left join department department2 on (student2.dNo = department2.dNo)
where department2.dName = '软件学院' and student2.age is not null);1
2
3
4
5
6
7select department1.dName, course1.cName, other.counts
from department department1, course course1, (select student2.dNo, sc2.cNo, count(student2.sNo)
from student student2, sc sc2
where student2.sNo = sc2.sNo
group by student2.dNo, sc2.cNo
having count(student2.sNo) > 4) as other(dNo, cNo, counts)
where department1.dNo = other.dNo and course1.cNo = other.cNo;1
2
3
4
5
6
7select student1.sNo, student1.sName, department1.dName
from (student student1 join sc sc1 on (student1.sNo = sc1.sNo)) left join department department1 on (student1.dNo = department1.dNo)
where sc1.sNo not in (select sc2.sNo
from sc sc2
where sc2.cNo <> (select course1.cNo
from course course1
where course1.cName = '高等数学'));1
2
3
4
5select student.sName
from (student join sc on (student.sNo = sc.sNo)) join course on (sc.cNo = course.cNo)
where sc.score is not null
group by student.sNo
having (sum(sc.score * course.credit) / sum(course.credit)) < 70;1
2
3
4
5select student.sName
from (student join sc on (student.sNo = sc.sNo)) join course on (sc.cNo = course.cNo)
where sc.score is not null
group by student.sNo
having CAST(sum(CAST(sc.score * course.credit AS NUMERIC)) / sum(course.credit) AS DECIMAL(5,2)) < 70.00; 1
2
3
4SELECT student.sName FROM student WHERE
NOT EXISTS( SELECT course.cNo FROM course
WHERE NOT EXISTS(SELECT sc.cNo FROM sc WHERE sc.sNo=student.sNo AND sc.cNo=course.cNo )
AND course.dNo=(SELECT department.dNo FROM department WHERE department.dName='信息学院') );1
2
3
4
5
6
7
8
9
10
11
12select sName
from student s
where not exists
(select *
from course c
where c.dNo in
(select dNo
from department
where dName = '信息学院') and not exists
(select *
from sc
where sNo = s.sNo and cNo = c.cNo));1
2
3
4
5
6
7
8
9
10
11
12select sname
from student s
where not exists
(select *
from sc sc2
where sc2.sno=
(select sno from student where sname='杨佳伟')
and not exists
(select *
from sc sc3
where s.sno=sc3.sno and sc2.cno=sc3.cno))
and s.sname<>'杨佳伟'; 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15select sName
from student
where sNo in
(select distinct sNo
from sc scx
where not exists
(select *
from sc scy
where scy.sNo =
(select sNo
from student
where sName = '杨佳伟') and not exists
(select *
from sc scz
where scz.sNo = scx.sNo and scz.cNo = scy.cNo)));
DDL练习
1.创建2张表,信息如下:
图书(编号,书名,作者,ISBN,出版社编号,版本,出版日期)。主码为编号,ISBN唯一。
出版社编号为外码,参照出版社编号。
出版社(编号,名称,地址,电话)。主码为编号。
要求:
(1)创建表的同时创建约束;
(2)删除所创建的表;
(3)重新创建表,在表创建之后增加约束。 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42--drop table book cascade
--drop table publisher cascade
create table publisher(
pNo varchar(20) not null,
pName varchar(200),
address varchar(200),
TEL varchar(200),
primary key(pNo)
);
create table book (
bNo char(12) not null unique,
bName varchar(200) not null,
author varchar(200) not null,
ISBN varchar(200) not null unique,
pNo varchar(20),
bVersion char(6),
bDate date,
primary key(bNo),
foreign key(pNo) references publisher(pNo)
);
create table publisher(
pNo varchar(20) not null,
pName varchar(200),
address varchar(200),
TEL varchar(200)
);
create table book (
bNo char(12) not null unique,
bName varchar(200) not null,
author varchar(200) not null,
ISBN varchar(200) not null unique,
pNo varchar(20),
bVersion char(6),
bDate date,
primary key(bNo)
);
alter table publisher add primary key(pNo);
alter table book add foreign key(pNo) references publisher(pNo);
2.分别向两张表中各插入2行数据。 1
2
3
4
5insert into publisher values('01', '机械工业出版社', '北京', '110');
insert into publisher values('02', '高等教育出版社', '上海', '120');
insert into book values('10086', '算法导论', 'Thomas', '9787040370683', '01', '1.0', to_date('2017-01-08','yyyy-mm-dd'));
insert into book values('10000', '离散数学', 'Pony', '9787040370688', '02', '16.3', to_date('2018-01-08','yyyy-mm-dd'));1
2
3update publisher
set address = '陕西'
where pNo = '01';1
2
3
4
5delete from
book cascade;
delete
from publisher cascade; 1
2
3
4
5
6--drop view other cascade;
create view other
as
select student.sNo, student.sName
from student join sc on student.sNo = sc.sNo join course on sc.cNo = course.cNo join department on student.dNo = department.dNo
where department.dName = '软件学院' and course.cName = '离散数学';1
2
3
4
5
6
7
8
9
10
11
12
13
14
15drop view other cascade;
create view other
as
SELECT student.sNo, student.sName
FROM student
WHERE student.dNo in
(SELECT department.dNo
FROM department
WHERE department.dName = '软件学院') and student.sNo in
(SELECT sc.sNo
FROM sc
where sc.sNo = student.sNo and sc.cNo in
(SELECT course.cNo
FROM course
WHERE course.cName = '离散数学'));1
2
3insert
into other
values('170288', '李天一');1
2
3
4
5
6drop view AVERAGE;
create view AVERAGE as
select sc.cNo, course.cName, avg(sc.score)
from course join sc on course.cNo = sc.cNo
where sc.score is not null
group by sc.cNo, course.cName;1
2
3
4
5
6--drop view s_score;
create view s_score as
select student.sNo, student.sName, avg(sc.score)
from student left join sc on student.sNo = sc.sNo
where sc.score is not null
group by student.sNo, student.sName1
2select *
from s_score;
尝试将多条SQL语句组成一个事务执行,体验提交和回滚操作。
1
2
3
4
5
6begin transaction;
select *
from sc;
select *
from student;
commit;