Student(Sid,Sname,Sage,Ssex) 学生表
Course(Cid,Cname,Tid) 课程表
SC(Sid,Cid,score) 成绩表
Teacher(Tid,Tname) 教师表
练习内容:
1.查询“某1”课程比“某2”课程成绩高的所有学生的学号;
SELECT a.Sid
FROM SC AS a ,SC AS b
Where a.Cid IN (SELECT Cid
FROM Course
Where Cname="某1")
AND b.Cid IN (SELECT Cid
FROM Course
Where Cname="某2")
AND a.score>b.score
AND a.Sid=b.Sid
2.查询平均成绩大于60分的同学的学号和平均成绩;
SELECT Sid ,avg(score)
FROM SC
GROUP BY Sid
HAVING avg(score)>60
3.查询所有同学的学号、姓名、选课数、总成绩
SELECT Sid,
Sname,
(SELECT count(*)
FROM SC
WHERE SC.Sid=Student.Sid),
(SELECT count(score)
FROM SC
WHERE SC.Sid=Student.Sid),
FROM Student;
用联结表:
SELECT Student.Sid,
Student.Sname,
count(SC.Cid)
sum(SC.score)
FROM Student LEFT OUTER JOIN SC
ON Student.Sid=SC.Sid
GROUP BY Student.Sid,Student.Sname;
4.查询姓“李”的老师的个数;
SELECT count(*)
FROM Teacher
WHERE Tname LIKE "李%";
5.查询没学过“叶平”老师课的同学的学号、姓名;
SELECT DISTINC Student.Sid, DISTINCT Student.Sname
FROM Student,Course,SC,Teacher
WHERE Student.Sid=SC.Sid
AND SC.Cid=Course.Cid
AND Course.Tid=Teacher.Tid
AND Tname != "叶平";
6.查询学过“```”并且也学过编号“```”课程的同学的学号、姓名;
使用子查询
SELECT Sid,Sname
FROM Student
WHERE Sid IN (SELECT Sid
FROM SC
WHERE Cid="1")
AND Sid IN (SELECT Sid
FROM SC
WHERE Cid="2");
使用联结表
SELECT Student.Sid,Student.Sname
FROM Student,SC AS a,SC AS b
WHERE a.Sid =Student.Sid
AND b.Sid =Student.Sid
AND a.Cid="1"
AND b.Cid="2";
7.查询学过“叶平”老师所教的所有课的同学的学号、姓名;
SELECT Sid,Sname
FROM Student
WHERE Sid IN (SELECT Sid
FROM SC,Course,Teacher
WHERE SC.Cid=Course.Cid
AND Course.Tid=Teacher.Tid
AND Teacher.Tname="叶平"
GROUP BY Sid
HAVING count(Cid)=(SELECT count(Cid)
FROM Course,Teacher
WHERE Course,Tid=Teacher.Tid
AND Teacher.Tname="叶平"));
8.查询课程编号“”的成绩比课程编号“”课程低的所有同学的学号、姓名;
SELECT a.Sid,a.Sname
FROM (SELECT Student.Sid,Student.Sname,SC.score
FROM Student,SC
WHERE Student.Sid=SC.Sid
AND SC.Cid=1)a,
(SELECT Student.Sid,Student.Sname,SC.score
FROM Student,SC
WHERE Student.Sid=SC.Sid
AND SC.Cid=2)b,
WHERE a.score<b.score
9.查询所有课程成绩小于60分的同学的学号、姓名;
SELECT DISTINCT Sid,DISTINCT Sname
FROM (SELECT Sid,Sname,count(Cid)
FROM Student,SC
WHERE Student.Sid=SC.Sid
AND SC.score<60
GROUP BY Sid,Sname
HAVING count(Cid)=(SELECT count(Cid)
FROM Student,SC
WHERE Student.Sid=SC.Sid
GROUP BY Sid,Sname))a;
另一种解法:
SELECT Sid,Sname
FROM Student
WHERE Sid NOT IN (SELECT Sid,Sname
FROM Student,SC
WHERE Student.Sid=SC.Sid
AND SC.score>60);
10.查询没有学全所有课的同学的学号、姓名;
SELECT DISTINCT Sid,DISTINCT Sname
FROM (SELECT Sid,Sname,count(Cid)
FROM Student,SC
WHERE Student.Sid=SC.Sid
GROUP BY Sid,Sname
HAVING count(Cid)<(SELECT count(Cid)
FROM SC))a;
11.查询至少有一门课与学号为“”的同学所学相同的同学的学号和姓名;
12.查询至少学过学号为“”同学所有一门课的其他同学学号和姓名;
13.把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
14.查询和“”号的同学学习的课程完全相同的其他同学学号和姓名;
15.删除学习“叶平”老师课的SC表记录;
16.向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“”课程的同学学号、课程的平均成绩;
17.按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分;(没做出来)
18.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
19.按各科平均成绩从低到高和及格率的百分数从高到低顺序
26.查询每门课程被选修的学生数
27.查询出只选修了一门课程的全部学生的学号和姓名
32.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
37.查询不及格的课程,并按课程号从大到小排列
38.查询课程编号为且课程成绩在分以上的学生的学号和姓名;
40.查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
41.查询各个课程及相应的选修人数
44.统计每门课程的学生选修人数(超过人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
45.检索至少选修两门课程的学生学号
rownum的用法
查询所有成绩第二名到第四名的成绩
47.查询没学过“叶平”老师讲授的任一门课程的学生姓名
48.查询两门以上不及格课程的同学的学号及其平均成绩
49.检索“”课程分数小于,按分数降序排列的同学学号
50.删除“”同学的“”课程的成绩