SQL查询设计
给定四个关联表,其定义和数据加载如下:
-- 学生表 Student
create table Student(Sno varchar(6), Sname varchar(10), Sdate datetime, Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1999-01-01' , '男');
insert into Student values('02' , '钱电' , '1999-12-21' , '男');
insert into Student values('03' , '孙风' , '1999-05-20' , '男');
insert into Student values('04' , '李云' , '1999-08-06' , '男');
insert into Student values('05' , '周梅' , '2000-12-01' , '女');
insert into Student values('06' , '吴兰' , '2001-03-01' , '女');
insert into Student values('07' , '郑竹' , '1998-07-01' , '女');
insert into Student values('08' , '王菊' , '1999-01-20' , '女');
-- 选课表 SC
create table SC(Sno varchar(10), Cno varchar(10), score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
-- 课程表 Course
create table Course(Cno varchar(10),Cname varchar(10),Tno varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表 Teacher
create table Teacher(Tno varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
insert into Teacher values('04' , '张小五');
试按以下操作要求完成SQL语言设计。
-
1.查询「张」姓老师的数量。
select tname,count(tno)as '数量' from teacher group by tno having tname like'%张%';
SELECT COUNT(1) AS counts FROM Teacher WHERE TName LIKE '%张%';
-
2.查询平均成绩大于等于 80 分的学生信息,输出其编号Sno、姓名Sname和平均成绩Avg_score三列内容。
SELECT t1.Sno,t1.Sname,t2.Avg_score
FROM Student AS t1
INNER JOIN (SELECT Sno,AVG(score) AS Avg_score FROM SC GROUP BY Sno
HAVING AVG(score)>=80) AS t2 ON t1.Sno=t2.Sno
-
3.查询Student表中所有同学的学生编号Sno、学生姓名Sname和对应选课总数、所有课程的总成绩(没有成绩的显示为 null )。
SELECT t1.Sno,t1.Sname,COUNT(t2.Cno) AS CountCno,SUM(t2.score) AS score
FROM Student AS t1
LEFT JOIN SC AS t2 ON t1.Sno=t2.Sno
GROUP BY t1.Sno,t1.Sname
-
4.查询选课表SC内没有选修课程表Course里所列所有课程的学生信息,输出其Sno、Sname、Sdate和Ssex四列内容。
SELECT t3.*,t4.*
FROM (
SELECT t1.Sno,t1.Sname,t1.Sdate,t1.Ssex,t2.Cno,t2.Cname
FROM Student AS t1
LEFT JOIN Course AS t2 ON 1=1
) AS t3
LEFT JOIN SC AS t4 ON t3.Sno=t4.Sno AND t3.Cno=t4.Cno
WHERE t4.Sno IS NULL
-
5.查询和" 01 "号学生学习的课程完全相同的其他学生信息,输出其学号Sno内容。
select sc.Sno from sc as sc
where Sno in
(select distinct sc.Sno from sc
where Sno<>'01'
and Cno in(select distinct Cno from sc where Sno='01')
group by Sno
having
count(1)=(select count(1) from sc where Sno='01'))
group by Sno
having count(1)=(select count(sc.Sno) from sc as sc where sc.Sno='01')
-
6. 查询没有学过"张三"老师讲授的任一门课程的学生姓名。
SELECT t1.Sno,t1.Sname FROM Student AS t1 LEFT JOIN ( SELECT DISTINCT Sno FROM SC WHERE Cno NOT IN (SELECT Cno FROM Course WHERE Tno IN (SELECT Tno FROM Teacher WHERE Tname='张三')) ) AS t2 ON t1.Sno=t2.Sno
-
7.编程
- 向Course中添加新课(课号和课名)时,第三个Tno教师号字段值自动填入对应SC表中按教师分类成绩平均分最高的教师编号。
- 请测试追加新课程“04,数据库”的效果。(切记:测试操作请放在最后进行!!!)
-
create trigger trigg on Course after insert as declare @topTno varchar(20); SELECT @topTno=t3.Tno FROM ( SELECT t2.Tno,AVG(t1.score) AS AvgScore FROM SC AS t1 LEFT JOIN Course AS t2 ON t1.Cno=t2.Cno GROUP BY t2.Tno ) AS t3 ORDER BY t3.AvgScore DESC limit 0,1 UPDATE Course SET Tno=@topTno WHERE Tno in (select Tno from inserted) GO INSERT INTO Course(Cno,Cname) VALUES ('04','数据库')
- 提示:可以新建一个数据库,然后加载下列表及数据。