3.3.2连接查询——练习
练习:4个表:
Student(Sno,Sname,Ssex,Sage,Sclass)
Teacher(Tno,Tname,Tsex,Tage,Tprof,Tdept)
Course(Cno,Cname,Tno)
SC(Sno,Cno,Grade)
- 查询所有选课学生的姓名、课程号和成绩。
答:
Select Sname,Cno,Grade
From Student,SC
Where Student.Sno=SC.Sno;
2.查询95033班同学所选各门课程的课号及平均分。
答:
Select Cno,AVG(Grade)
From Student,SC
Where Student.Sno=SC.Sno And
Sclass=‘95033’
Group By Cno;
3.查询选修“3-105”号课程的成绩高于“109”号同学的所有同学记录。
答:
Select*
From SC a
Where Sno in(
Select Sno
From SC b
Where Cno=‘3-105’ And
b.Grade>(Select Grade
From SC
Where Sno=‘109’ And
Cno=‘3-105’));
3.3.3嵌套查询——练习
练习:4个表:
Student(Sno,Sname,Ssex,Sage,Sclass)
Teacher(Tno,Tname,Tsex,Tage,Tprof,Tdept)
Course(Cno,Cname,Tno)
SC(Sno,Cno,Grade)
-
查询和学号为108的学生同岁的所有学生的学号、姓名和年龄。
答:
Select Sno,Sname,Sage
From Student
Where Sage=
(Select Sage
From Student
Where Sno=‘108’); -
查询计算机系教师所授课程的成绩表。
答:
Select Cno,Sno,Grade
From SC
Where Cno in
(Select Course.Cno
From Course,Teacher
Where Course.Tno=Teacher.Tno
and Tdept=‘计算机系’); -
查询选修了“3-105”课程且成绩至少高于某一个选修“3-245”课程的同学学号,课号和成绩,并按成绩由高到低排序。
答:
Select Cno,Sno,Grade
From SC
Where Cno=‘3-105’
and Grade>any (Select Grade
From SC
Where Cno=‘3-245’)
Order by Grade DESC; -
查询所有未任课教师的姓名和所在系。
答:
Select Tname,Tdept
From Teacher
Where NOT EXISTS
Select*
From Course
Where Course.Tno=Teacher.Tno);
3.3.4集合查询——练习
练习:4个表:
Student(Sno,Sname,Ssex,Sage,Sclass)
Teacher(Tno,Tname,Tsex,Tage,Tprof,Tdept)
Course(Cno,Cname,Tno)
SC(Sno,Cno,Grade)
- 将姓名为王娜,女,20岁,学号为200,班级为96100的学生记录加入Student表中。
答:
Insert
Into Student
Values(‘200’,‘王娜’,‘女’,20,‘96100’);
2.把平均成绩大于80的所有男学生的学号和平均成绩存入另一个基本M_Grade(Sno,Avg_Grade)中。
答:
第一步:建表
CREATE TABLE M_Grade
(Sno CHAR(10) primary key,
Avg_Grade SMALLINT
);
第二步:插入数据
INSERT
INTO M_Grade(Sno,Avg_Grade)
SELECT SC.Sno,avg(Grade)
FROM SC,Student
WHERE SC.Sno=Student.Sno
and Ssex='男
Group BY SC.Sno
Having avg(Grade)>80;
3.将选修”3-105”课程的学生成绩增加5分。
答:
update SC
set Grade=Grade+5
where Cno=‘3-105’;
select *
from SC
4.删除选修“3-105”课程中成绩低于该门课程平均成绩的选课记录。
答:
delete
from SC
where Cno='3-105
and Grade<(select avg(Grade)
from SC
where Cno=‘3-105’);
5.删除计算机导论这门课程以及对应的选课记录。
答:
Delete
From SC
Where Cno=(Select Cno From Course
Where C name=‘计算机导论’);
Delete
From Course
Where C name=‘计算机导论;