SQL Exercises
Create Tables
--DROP TABLE department CASCADE;
--DROP TABLE student CASCADE;
--DROP TABLE course CASCADE;
--DROP TABLE sc CASCADE;
--department(dNo,dName,officeRoom,homepage)
--student(sNo,sName,sex,age,dNo)
--course(cNo,cName,cPNo,credit,dNo)
--sc(sNo,cNo,score,recordDate)
CREATE TABLE department(
dNo CHAR(2) NOT NULL UNIQUE,
dName VARCHAR(20),
officeRoom VARCHAR(40),
homePage VARCHAR(80),
PRIMARY KEY(dNo)
);
CREATE TABLE student(
sNo CHAR(6) NOT NULL UNIQUE,
sName VARCHAR(20) NOT NULL,
sex CHAR(2) CHECK (sex IN('男','女')),
age INT,
dNo CHAR(2),
PRIMARY KEY(sNo),
FOREIGN KEY (dNo) REFERENCES department(dNo)
);
CREATE TABLE course(
cNo CHAR(6) NOT NULL UNIQUE,
cName VARCHAR(20) NOT NULL,
cPNo CHAR(6),
credit INT,
dNo CHAR(2),
PRIMARY KEY(cNo),
FOREIGN KEY (cPNo) REFERENCES course(cNo),
FOREIGN KEY (dNo) REFERENCES department(dNo)
);
CREATE TABLE sc(
sNo CHAR(6) NOT NULL,
cNo CHAR(6) NOT NULL,
score INT,
recordDate date,
PRIMARY KEY(sNo,cNo),
FOREIGN KEY (sNo) REFERENCES student(sNo),
FOREIGN KEY (cNo) REFERENCES course(cNo)
);
Insert Values
INSERT INTO department VALUES('01','信息学院','行政楼409','www.xxx.edu.cn');
INSERT INTO department VALUES('02','软件学院',null,null);
INSERT INTO department VALUES('03','理学院',null,null);
INSERT INTO department VALUES('04','文学院',null,null);
INSERT INTO department VALUES('05','外国语学院',null,null);
INSERT INTO student VALUES('170101','宁灿', '女',19,'01');
INSERT INTO student VALUES('170102','尹江月','女',19,'01');
INSERT INTO student VALUES('170103','杨佳伟','男',null,null);
INSERT INTO student VALUES('170104','杨何宇','男',19,'01');
INSERT INTO student VALUES('170105','胡耀斌','男',19,null);
INSERT INTO student VALUES('170106','李杨阳','女',20,'01');
INSERT INTO student VALUES('170107','杜利俊','女',18,'01');
INSERT INTO student VALUES('170108','钱多多','女',17,'01');
INSERT INTO student VALUES('170109','李佳伟','女',null,'01');
INSERT INTO student VALUES('170110','吴莫愁','女',21,'01');
INSERT INTO student VALUES('170201','安相成','男',19,'02');
INSERT INTO student VALUES('170202','曹师好','男',null,'02');
INSERT INTO student VALUES('170203','雷霆', '男',18,'02');
INSERT INTO student VALUES('170204','刘书敏','男',20,'02');
INSERT INTO student VALUES('170205','王兵', '男',21,'02');
INSERT INTO student VALUES('170206','李佳成','男',19,null);
INSERT INTO student VALUES('170207','唐玉迎','女',17,'02');
INSERT INTO student VALUES('170208','杨曼婷','女',19,'02');
INSERT INTO student VALUES('170301','张望', '男',21,'03');
INSERT INTO student VALUES('170302','王芳', '女',18,'03');
INSERT INTO student VALUES('170303','赵四海','男',19,'03');
INSERT INTO student VALUES('170401','孙敏', '女',null,null);
INSERT INTO student VALUES('170402','李忠国','男',null,'04');
INSERT INTO student VALUES('170403','钱紧', '男',17,'04');
INSERT INTO student VALUES('170404','钱多多','女',20,'04');
INSERT INTO student VALUES('170405','管八方','男',21,'04');
INSERT INTO student VALUES('170406','王兵', '男',19,'04');
INSERT INTO student VALUES('170407','张三丰','男',100,null);
INSERT INTO course VALUES('030101','高等数学',null,2,'03');
INSERT INTO course VALUES('030102','线性代数',null,2,'03');
INSERT INTO course VALUES('030201','矩阵论','030102',3,'03');
INSERT INTO course VALUES('030202','概率论','030101',2,'03');
INSERT INTO course VALUES('030301','数理统计','030202',3,'03');
INSERT INTO course VALUES('010101','信号与系统',null,2,'01');
INSERT INTO course VALUES('010102','数字电路','010101',2,'01');
INSERT INTO course VALUES('010103','数字信号处理','030301',3,'01');
INSERT INTO course VALUES('010201','模式识别','010103',3,'01');
INSERT INTO course VALUES('020101','离散数学',null,2,'02');
INSERT INTO course VALUES('020102','程序设计基础',null,2,'02');
INSERT INTO course VALUES('020201','计算机组成原理','020101',3,'02');
INSERT INTO course VALUES('020202','面向对象程序设计','020102',2,'02');
INSERT INTO course VALUES('020203','编译原理','020101',2,'02');
INSERT INTO course VALUES('020301','数据结构','020202',3,'02');
INSERT INTO course VALUES('020302','操作系统','020201',3,'02');
INSERT INTO course VALUES('020401','数据库系统','020301',3,'02');
INSERT INTO course VALUES('020402','算法设计与分析','020301',3,'02');
INSERT INTO course VALUES('020403','形式语言与自动机','020203',3,'02');
INSERT INTO course VALUES('020501','移动计算','020102',2,'02');
INSERT INTO course VALUES('040101','中国传统文化',null,1,'04');
INSERT INTO course VALUES('040102','近代世界史',null,1,'04');
INSERT INTO course VALUES('040103','现代文学',null,1,'04');
INSERT INTO course VALUES('040201','古典文学欣赏','040101',2,'04');
INSERT INTO course VALUES('040202','世界文学','040103',2,'04');
INSERT INTO course VALUES('040301','中国诗词欣赏','040201',2,'04');
INSERT INTO sc VALUES ('170101','030101',91,to_date('2016-01-08','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170101','030102',83,to_date('2016-07-10','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170101','020101',88,to_date('2016-07-02','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170101','020102',92,to_date('2017-01-10','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170101','020201',70,to_date('2017-01-10','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170101','020202',80,to_date('2017-01-10','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170101','020203',null,null);
INSERT INTO sc VALUES ('170101','020301',null,null);
INSERT INTO sc VALUES ('170101','020302',null,null);
INSERT INTO sc VALUES ('170101','020401',null,null);
INSERT INTO sc VALUES ('170101','020402',null,null);
INSERT INTO sc VALUES ('170102','030101',88,to_date('2016-01-08','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170102','030102',86,to_date('2016-07-10','yyyy-mm-dd'));
INSERT INTO sc VALUES ('170102','030201',58,null);
INSERT INTO sc VALUES ('170102','030202',90,null);
INSERT INTO sc VALUES ('170102','030301',70,null);
INSERT INTO sc VALUES ('170102','010101',85,null);
INSERT INTO sc VALUES ('170102','010102',68,null);
INSERT INTO sc VALUES ('170102','010103',80,null);
INSERT INTO sc VALUES ('170102','010201',78,null);
INSERT INTO sc VALUES ('170103','030101',88,null);
INSERT INTO sc VALUES ('170103','030102',80,null);
INSERT INTO sc VALUES ('170103','030202',76,null);
INSERT INTO sc VALUES ('170103','010101',90,null);
INSERT INTO sc VALUES ('170103','010102',83,null);
INSERT INTO sc VALUES ('170103','010103',55,null);
INSERT INTO sc VALUES ('170103','010201',77,null);
INSERT INTO sc VALUES ('170104','030101',null,null);
INSERT INTO sc VALUES ('170105','030101',null,null);
INSERT INTO sc VALUES ('170108','030101',90,null);
INSERT INTO sc VALUES ('170108','030102',87,null);
INSERT INTO sc VALUES ('170108','030202',78,null);
INSERT INTO sc VALUES ('170108','010101',57,null);
INSERT INTO sc VALUES ('170108','010102',56,null);
INSERT INTO sc VALUES ('170108','010103',54,null);
INSERT INTO sc VALUES ('170108','010201',50,null);
INSERT INTO sc VALUES ('170110','030101',90,null);
INSERT INTO sc VALUES ('170110','030102',90,null);
INSERT INTO sc VALUES ('170110','010101',88,null);
INSERT INTO sc VALUES ('170110','010102',88,null);
INSERT INTO sc VALUES ('170110','010103',77,null);
INSERT INTO sc VALUES ('170201','030101',88,null);
INSERT INTO sc VALUES ('170201','030102',83,null);
INSERT INTO sc VALUES ('170201','020101',79,null);
INSERT INTO sc VALUES ('170201','020102',84,null);
INSERT INTO sc VALUES ('170201','020201',75,null);
INSERT INTO sc VALUES ('170201','020202',80,null);
INSERT INTO sc VALUES ('170201','020301',82,null);
INSERT INTO sc VALUES ('170201','020302',53,null);
INSERT INTO sc VALUES ('170201','020401',78,null);
INSERT INTO sc VALUES ('170201','020402',58,null);
INSERT INTO sc VALUES ('170202','030101',86,null);
INSERT INTO sc VALUES ('170202','030102',82,null);
INSERT INTO sc VALUES ('170202','020101',90,null);
INSERT INTO sc VALUES ('170202','020102',85,null);
INSERT INTO sc VALUES ('170202','020201',80,null);
INSERT INTO sc VALUES ('170202','020202',78,null);
INSERT INTO sc VALUES ('170202','020203',75,null);
INSERT INTO sc VALUES ('170202','020301',70,null);
INSERT INTO sc VALUES ('170202','020302',68,null);
INSERT INTO sc VALUES ('170202','020401',80,null);
INSERT INTO sc VALUES ('170202','020402',56,null);
INSERT INTO sc VALUES ('170203','030101',null,null);
INSERT INTO sc VALUES ('170203','030102',null,null);
INSERT INTO sc VALUES ('170205','030101',80,null);
INSERT INTO sc VALUES ('170205','030102',55,null);
INSERT INTO sc VALUES ('170205','020101',50,null);
INSERT INTO sc VALUES ('170205','020102',96,null);
INSERT INTO sc VALUES ('170205','020201',80,null);
INSERT INTO sc VALUES ('170205','020202',85,null);
INSERT INTO sc VALUES ('170205','020203',60,null);
INSERT INTO sc VALUES ('170205','020301',88,null);
INSERT INTO sc VALUES ('170205','020302',92,null);
INSERT INTO sc VALUES ('170205','020401',65,null);
INSERT INTO sc VALUES ('170205','020402',70,null);
INSERT INTO sc VALUES ('170205','020501',80,null);
INSERT INTO sc VALUES ('170207','030101',null,null);
INSERT INTO sc VALUES ('170207','030102',null,null);
INSERT INTO sc VALUES ('170208','030101',60,null);
INSERT INTO sc VALUES ('170208','030102',null,null);
INSERT INTO sc VALUES ('170208','020101',null,null);
INSERT INTO sc VALUES ('170208','020102',null,null);
INSERT INTO sc VALUES ('170208','020201',null,null);
INSERT INTO sc VALUES ('170208','020202',null,null);
INSERT INTO sc VALUES ('170208','020301',null,null);
INSERT INTO sc VALUES ('170208','020302',null,null);
INSERT INTO sc VALUES ('170208','020401',null,null);
INSERT INTO sc VALUES ('170208','020402',null,null);
INSERT INTO sc VALUES ('170208','020501',null,null);
INSERT INTO sc VALUES ('170303','030101',90,null);
INSERT INTO sc VALUES ('170303','030102',88,null);
INSERT INTO sc VALUES ('170303','030201',92,null);
INSERT INTO sc VALUES ('170303','030202',93,null);
INSERT INTO sc VALUES ('170303','030301',94,null);
INSERT INTO sc VALUES ('170401','040101',82,null);
INSERT INTO sc VALUES ('170401','040102',80,null);
INSERT INTO sc VALUES ('170401','040103',83,null);
INSERT INTO sc VALUES ('170401','040201',89,null);
INSERT INTO sc VALUES ('170401','040202',70,null);
INSERT INTO sc VALUES ('170401','040301',null,null);
INSERT INTO sc VALUES ('170403','040201',95,null);
INSERT INTO sc VALUES ('170403','040202',88,null);
INSERT INTO sc VALUES ('170403','040301',80,null);
INSERT INTO sc VALUES ('170404','040101',95,null);
INSERT INTO sc VALUES ('170404','040102',91,null);
INSERT INTO sc VALUES ('170404','040103',89,null);
INSERT INTO sc VALUES ('170404','040201',93,null);
INSERT INTO sc VALUES ('170404','040202',92,null);
INSERT INTO sc VALUES ('170404','040301',88,null);
INSERT INTO sc VALUES ('170405','040102',70,null);
INSERT INTO sc VALUES ('170405','040201',58,null);
INSERT INTO sc VALUES ('170405','040202',66,null);
INSERT INTO sc VALUES ('170405','040301',77,null);
INSERT INTO sc VALUES ('170406','040101',55,null);
INSERT INTO sc VALUES ('170406','040102',65,null);
INSERT INTO sc VALUES ('170406','040301',75,null);
exercise
/*一、单表*/
/*(1)查询所有年龄大于等于20岁的学生学号、姓名*/
select sNo,sName
from student
where age>=20;
/*(2)查询所有姓钱的男生学号、姓名、出生年份*/
select sNo,sName,2021-age
from student
where sName like '钱%';
/*(3)查询所有学分大于3的课程名称*/
select cName
from course
where credit>3;
/*(4)查询所有没有被分配到任何学院的学生姓名*/
select sName
from student
where dNo is null;
/*(5)查询所有尚未设置主页的学院名称*/
select dName
from department
where homepage is null;
/*二、聚集*/
/*(1)查询各个学院的平均年龄*/
select dNo,avg(age) as avg_age
from student group by dNo;
/*(2)查询每个学生选修课程的平均分*/
select sNo,avg(score) as avg_score
from sc group by sNo;
/*(3)查询各课程的平均分*/
select cNo,avg(score) as avg_score
from sc group by cNo;
/*(4)查询各学院开设的课程门数*/
select dNo,count(*) as num_cust
from course group by dNo;
/*(5)查询各门课程选修人数*/
select cNo,count(sNo) as num_cust
from sc group by cNo;
/*三、多表*/
/*(1)查询“信息学院”所有学生学号与姓名*/
select sNo,sName
from student,department
where student.dNo = department.dNo
and dName='信息学院';
/*(2)查询“软件学院”开设的所有课程号与课程名称*/
select cNo,cName
from course,department
where course.dNo=department.dNo
and dName='软件学院';
/*(3)查询与“陈丽”在同一个系的所有学生学号与姓名*/
select sNo,sName
from student
where dNo in (select dNo
from student
where sName='陈丽');
/*无结果*/
/*(4)查询与“张三”同岁的所有学生学号与姓名*/
select sNo,sName
from student
where age in (select age
from student
where sName='张三');
/*无结果*/
/*(5)查询与“张三”同岁且不与“张三”在同一个系的学生学号与姓名*/
select sNo,sName
from student
where age in (select age
from student
where sName='张三')
and dNo not in (select dNo
from student
where sName='张三');
/*无结果*/
/*(6)查询学分大于“离散数学”的所有课程名称*/
select cName
from course
where credit>(select credit
from course
where cName='离散数学');
/*(7)查询选修了课程名为“组合数学”的学生人数*/
select count(sNo)
from sc
where cNo in(select cNo
from course
where cName='组合数学');
/*(8)查询没有选修“离散数学”的学生姓名*/
select sName
from sc,student
where sc.sNo not in (select sNo
from sc
where cNo not in(select cNo
from course
where cName='离散数学'))
and sc.sNo=student.sNo;
/*(9)查询与“算法设计与分析”、“移动计算”学分不同的所有课程名称*/
select cName
from course
where credit not in (select credit
from course
where cName='算法设计与分析' or cName='移动计算');
/*(10)查询平均分大于等于90分的所有课程名称*/
select cName
from course
where cNo not in (select cNo
from sc
group by cNo
having avg(score)>=90);
/*(11)查询选修了“离散数学”课程的所有学生姓名与成绩*/
select sName,score
from student,sc
where sc.sNo=student.sNo
and sc.sNo in (select cNo
from sc
where cNo in (select cNo
from course
where cName='离散数学'));
/*(12)查询“王兵”所选修的所有课程名称及成绩*/
select cName,score
from course,sc
where course.cNo=sc.cNo
and sNo in (select sNo
from student
where sName='王兵');
/*(13)查询所有具有不及格课程的学生姓名、课程名与成绩*/
select sName,cName,score
from sc,student,course
where sc.sNo=student.sNo
and sc.cNo=course.cNo
and score<60;
/*(14)查询选修了“文学院”开设课程的所有学生姓名*/
select sName
from student,course,sc
where sc.sNo=student.sNo
and course.cNo=sc.cNo
and course.dNo=(select dNo
from department
where dName='文学院')
group by sName;
/*(15)查询“信息学院”所有学生姓名及其所选的“信息学院”开设的课程名称*/
select sName,cName
from student,sc,course
where student.sNo=sc.sNo
and sc.cNo=course.cNo
and course.dNo in (select dNo
from department
where dName='信息学院')
and student.dNo in (select dNo
from department
where dName='信息学院');
/*四、综合*/
/*(1)查询所有学生及其选课信息(包括没有选课的学生)*/
select *
from student,sc
where sc.sNo=student.sNo;
/*(2)查询“形式语言与自动机”先修课的课程名称*/
select cName
from course
where cNo =(select cPNO
from course
where cName='形式语言与自动机');
/*(3)查询“形式语言与自动机”间接先修课课程名称;*/
select cName
from course
where cNo in (select cPNo
from course
where cNo in (select cPNO
from course
where cName='形式语言与自动机'));
/*(4)查询先修课为编译原理数学的课程名称*/
select cName
from course
where cPNo = (select cNo
from course
where cName='编译原理');
/*(5)查询间接先修课为离散数学的课程名称*/
select cName
from course
where cPNo in (select cNo
from course
where cPNo in (select cNo
from course
where cName='离散数学'));
/*(6)查询所有没有先修课的课程名称*/
select cName
from course
where cPNO is null;
/*(7)查询所有没选修“形式语言与自动机”课程的学生姓名*/
select sName
from student
where sNo not in (select sNo
from sc
where cNo in (select cNo
from course
where cName = '形式语言与自动机'));
/*(8)查询所有选修了“形式语言与自动机”但没选修其先修课的学生姓名*/
select sName
from student
where sNo in (select sNo
from sc
where cNo = (select cNo
from course
where cName = '形式语言与自动机'))
and sNo not in (select sNo
from sc
where cNo = (select cNo
from course
where cNo = (select cPNo
from course
where cName = '形式语言与自动机')));
/*(9)查询选修课程总学分大于等于28的学生姓名及其选修课程总学分*/
select sName,sum(credit) as total_credit
from student,sc,course
where student.sNo=sc.sNo and sc.cNo=course.cNo
group by student.sNo
having sum(credit)>=28;
/*(10)查询选修了3门以上课程且成绩都大于85分的学生学号与姓名*/
select sNo,sName
from student
where sNo in (select sNo
from sc
group by sNo
having count(cNo)>3
and min(score)>85);
/*(11)查询恰好选修了3门课并且都及格的学生姓名*/
select sName
from student
where sNo in (select sNo
from sc
group by sNo
having count(cNo) = 3
and min(score)>60);
/*(12)查询人数多于6的学院名称及其学生人数*/
select dName,count(s.sNo) as total_number
from department d left outer join student s
on d.dNo=s.dNo
group by d.dName
having count(sNo)>6
/*(13)查询平均成绩高于王兵的学生姓名*/
select sName
from student
where sNo in (select sNo
from sc
group by sNo
having avg(score)>all(select avg(score)
from sc
where sNo in (select sNo
from student
where sName='王兵')
group by sNo));
/*(14)查询所有选修了离散数学并且选修了编译原理课程的学生姓名*/
select sName
from student
where sNo in(select sNo
from sc
where cNo= (select cNo
from course
where cName = '离散数学'))
and sNo in(select sNo
from sc
where cNo= (select cNo
from course
where cName = '编译原理'));
/*(15)查询软件学院离散数学课程平均分*/
select avg(score) as avg_score
from sc
where sNo in(select sNo
from student
where dNo=(select dNo
from department
where dName='软件学院'))
group by cNo
having cNo=(select cNo
from course
where cName='离散数学');
/*(16)查询年龄与“软件学院”所有学生年龄都不相同学生姓名及其年龄和学院*/
select sName,age,dNo
from student
where age not in(select age
from student
where dNo = (select dNo
from department
where dName = '软件学院')
and age is not null);
/*(17)查询各学院选修同一门课人数大于4的学院、课程及选课人数*/
select d.dName,c.cName,v.countOf
from department d,course c,
(select dNo,cNo,count(s.sNo)
from student s natural join sc
group by dNo,cNo
having count(s.sNo)>4) as v(dNo,cNo,countOf)
where c.cNo=v.cNo and d.dNo= v.dNo;
/*(18)查询仅仅选修了“高等数学”一门课程的学生姓名;(学号、姓名及所在学院名称)*/
select s.sNo,s.sName,d.dName
from student s left join department d on s.dNo=d.dNo
where s.sNo in(select sNo
from sc
where cNo in(select cNo
from course
where cName = '高等数学'))
and s.sNo in(select sNo
from sc
group by sNo
having count(cNo) = 1);
/*(19)查询平均学分积小于70分的学生姓名*/
select sName
from student
where sNo in(select sNo
from sc natural join course
group by sNo
having(sum(score*credit)/sum(credit))<70);
/*(20)查询选修了“信息学院”开设全部课程的学生姓名*/
/*等价于"没有一门信息学院开设的课程是他没选的"*/
select sName
from student s
where not exists(select *
from course c
where dNo = (select dNo
from department
where dName = '信息学院')
and not exists(select *
from sc
where sNo=s.sNo and cNo=c.cNo));
/*(21)查询选修了“杨佳伟”同学所选修的全部课程的学生姓名*/
/*等价于没有一门杨佳伟选修的课是他没选的*/
select sName
from student
where sNo in(select sNo
from sc sc1
where not exists(select *
from sc sc2
where not exists(select *
from sc sc3
where sc1.sNo=sc3.sNo and sc3.cNo=sc2.cNo)
and sc2.sNo=(select sNo
from student
where sName='杨佳伟')))
and sName!='杨佳伟';
|