Student(SId,Sname,Sage,Ssex) 学生表
SId:学号;
Sname:学生姓名;
Sage:学生年龄;
Ssex:学生性别
Course(CId,Cname,TId) 课程表
CId,课程编号;
Cname:课程名字;
TId:教师编号
SC(SId,CId,score) 成绩表
SId:学号;
CId,课程编号;
score:成绩
Teacher(TId,Tname) 教师表
TId:教师编号;
Tname:教师名字
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.SId from (select sId,score from SC where CId='001') a,(select sId,score
from SC where CId='002') b where 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 Student.SId,Student.Sname,count(SC.CId),sum(score)
from Student left Outer join SC on Student.SId=SC.SId
group by Student.SId,Sname
4、查询姓“李”的老师的个数;
select count(distinct(Tname)) from Teacher where Tname like '李%';
5、查询没学过“叶平”老师课的同学的学号、姓名;
select Student.SId,Student.Sname rom Student
where SId not in (select distinct( SC.SId) from SC,Course,Teacher
where SC.CId=Course.CId and Teacher.TId=Course.TId and Teacher.Tname='叶平');
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select Student.SId,Student.Sname from Student,SC
where Student.SId=SC.SId and SC.CId='001'and
exists( Select * from SC as SC_2 where SC_2.SId=SC.SId and SC_2.CId='002');
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
select SId,Sname from Student where SId in (select SId from SC ,Course ,Teacher where SC.CId=Course.CId and
Teacher.TId=Course.TId and Teacher.Tname='叶平' group by SId having
count(SC.CId)=(select count(CId) from Course,Teacher where Teacher.TId=Course.TId and Tname='叶平'));
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
Select SId,Sname from (select Student.SId,Student.Sname,score ,
(select score from SC SC_2 where SC_2.SId=Student.SId and SC_2.CId='002') score2
from Student,SC where Student.SId=SC.SId and CId='001') S_2 where score2 <score;
9、查询所有课程成绩小于60分的同学的学号、姓名;
select SId,Sname from Student
where SId not in (select Student.SId from Student,SC where S.SId=SC.SId and score>60);
10、查询没有学全所有课的同学的学号、姓名;
select Student.SId,Student.Sname from Student,SC
where Student.SId=SC.SId group by Student.SId,Student.Sname having
count(CId) <(select count(CId) from Course);
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
select SId,Sname from Student,SC where Student.SId=SC.SId and CId in select CId from SC where SId='1001';
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;
select distinct SC.SId,Sname from Student,SC
where Student.SId=SC.SId and CId in (select CId from SC where SId=‘001’);
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;
update SC set score=(select avg(SC_2.score) from SC SC_2
where SC_2.CId=SC.CId ) from Course,Teacher where Course.CId=SC.CId and
Course.TId=Teacher.TId and Teacher.Tname='叶平');
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
select SId from SC where CId in (select CId from SC where SId=‘1002’)
group by SId having count()=(select count() from SC where SId=‘1002’);
15、删除学习“叶平”老师课的SC表记录;
Delete SC from course ,Teacher
where Course.CId=SC.CId and Course.TId= Teacher.TId and Tname=‘叶平’;
16、向SC表中插入一些记录,这些记录要求符合以下条件:1, 没有上过编号“003”课程的同学学号 2, ‘002’号课的平均成绩;
Insert into Sc select SId,’003’,(Select avg(score) from sc where cId=’002’)
from student where SId not in (Select SId from scwhere cid=’003’);
17、查询各科成绩前三名的记录:(不考虑成绩并列情况,思路:成绩超过他但人数小于3)
select sid, cid, score
from sc s1
where
( select count(1)
from sc s2
where s2.cid=s1.cid and s2.score>=s1.score
) <=3 order by cid, score desc;
18、查询每门课程被选修的学生数
select cId,count(SId) from sc group by CId;
19、查询出只选修了一门课程的全部学生的学号和姓名
select SC.SId,Student.Sname,count(CId) AS 选课数 from SC ,Student
where SC.SId=Student.SId group by SC.SId ,Student.Sname having count(CId)=1;
20、查询男生、女生人数
Select count(Ssex) as 男生人数 from Student group by Ssex having Ssex=‘男’;
Select count(Ssex) as 女生人数 from Student group by Ssex having Ssex=‘女’;
21、查询姓“张”的学生名单
SELECT Sname FROM Student WHERE Sname like ‘张%’;
22、查询同名同性学生名单,并统计同名人数
select Sname,count() from Student group by Sname having count()>1;
23、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age
from student where CONVERT(char(11),DATEPART(year,Sage))=‘1981’;
24、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
Select CId,Avg(score) from SC group by CId order by Avg(score),CId DESC ;
33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select Sname,SC.SId ,avg(score) from Student,SC
where Student.SId=SC.SId group by SC.SId,Sname having avg(score)>85;
25、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
Select Sname,isnull(score,0) from Student,SC,Course
where SC.SId=Student.SId and SC.CId=Course.CId and Course.Cname='数据库'and score <60;
26、查询所有学生的选课情况;
SELECT SC.SId,SC.CId,Sname,Cname FROM SC,Student,Course
where SC.SId=Student.SId and SC.CId=Course.CId ;
27、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT distinct student.SId,student.Sname,SC.CId,SC.score
FROM student,Sc WHERE SC.score>=70 AND SC.SId=student.SId;
28、查询不及格的课程,并按课程号从大到小排列
select cId from sc where scor e <60 order by CId ;
29、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
select SC.SId,Student.Sname from SC,Student where SC.SId=Student.SId and Score>80 and CId=‘003’;
30、求选了课程的学生人数
select count(*) from sc;
31、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select Student.Sname,score from Student,SC,Course C,Teacher
where Student.SId=SC.SId and SC.CId=C.CId and C.TId=Teacher.TId and Teacher.Tname=‘叶平’ and SC.score=(select max(score)from SC where CId=C.CId );
32、查询各个课程及相应的选修人数
select count(*) from sc group by CId;
33、查询每门功成绩最好的前两名
SELECT t1.SId as 学生ID,t1.CId as 课程ID,Score as 分数
FROM SC t1 WHERE score IN (SELECT TOP 2 score FROM SC
WHERE t1.CId= CId ORDER BY score DESC ) ORDER BY t1.CId;
34、检索至少选修两门课程的学生学号
select SId from sc group by sId having count(*) > = 2
35、查询没学过“叶平”老师讲授的任一门课程的学生姓名
select Sname from Student where SId not in (select SId from Course,Teacher,SC where Course.TId=Teacher.TId and SC.CId=course.CId and Tname=‘叶平’);
36、检索“004”课程分数小于60,按分数降序排列的同学学号
select SId from SC where CId='004'and score <60 order by score desc;
37、删除“002”同学的“001”课程的成绩
delete from Sc where SId='002'and CId='001';