--方法2
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and Student.S# not in (Select SC_2.S# from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#
--11、查询没有学全所有课程的同学的信息 --11.1、 select Student.* from Student , SC where Student.S# = SC.S#
group by Student.S# , Student.Sname , Student.Sage , Student.Ssex having count(C#) < (select count(C#) from Course) --11.2 select Student.* from Student left join SC on Student.S# = SC.S#
group by Student.S# , Student.Sname , Student.Sage , Student.Ssex having count(C#) < (select count(C#) from Course)
--12、查询至少有一门课与学号为\的同学所学相同的同学的信息
select distinct Student.* from Student , SC where Student.S# = SC.S# and SC.C# in (select C# from SC where S# = '01') and Student.S# <> '01'
--13、查询和\号的同学学习的课程完全相同的其他同学的信息 select Student.* from Student where S# in
(select distinct SC.S# from SC where S# <> '01' and SC.C# in (select distinct C# from SC where S# = '01')
group by SC.S# having count(1) = (select count(1) from SC where S#='01'))
--14、查询没学过\张三\老师讲授的任一门课程的学生姓名 select student.* from student where student.S# not in
(select distinct sc.S# from sc , course , teacher where sc.C# = course.C# and course.T# = teacher.T# and teacher.tname = N'张三') order by student.S#
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.S# , student.sname , cast(avg(score) as decimal(18,2)) avg_score from student , sc where student.S# = SC.S# and student.S# in (select S# from SC where score < 60 group by S# having count(1) >= 2)
group by student.S# , student.sname
--16、检索\课程分数小于60,按分数降序排列的学生信息 select student.* , sc.C# , sc.score from student , sc
where student.S# = SC.S# and sc.score < 60 and sc.C# = '01' order by sc.score desc
--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 --17.1 SQL 2000 静态
select a.S# 学生编号 , a.Sname 学生姓名 ,
max(case c.Cname when N'语文' then b.score else null end) [语文], max(case c.Cname when N'数学' then b.score else null end) [数学], max(case c.Cname when N'英语' then b.score else null end) [英语], cast(avg(b.score) as decimal(18,2)) 平均分 from Student a
left join SC b on a.S# = b.S# left join Course c on b.C# = c.C# group by a.S# , a.Sname order by 平均分 desc --17.2 SQL 2000 动态 declare @sql nvarchar(4000)
set @sql = 'select a.S# ' + N'学生编号' + ' , a.Sname ' + N'学生姓名'
select @sql = @sql + ',max(case c.Cname when N'''+Cname+''' then b.score else null end)
['+Cname+']'
from (select distinct Cname from Course) as t
set @sql = @sql + ' , cast(avg(b.score) as decimal(18,2)) ' + N'平均分' + ' from Student a left join SC b on a.S# = b.S# left join Course c on b.C# = c.C# group by a.S# , a.Sname order by ' + N'平均分' + ' desc' exec(@sql)
--24、查询学生平均成绩及其名次
--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。 select t1.* , px = (select count(1) from (
select m.S# [学生编号] , m.Sname [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] from Student m left join SC n on m.S# = n.S# group by m.S# , m.Sname
) t2 where 平均成绩 > t1.平均成绩) + 1 from (
select m.S# [学生编号] , m.Sname [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] from Student m left join SC n on m.S# = n.S# group by m.S# , m.Sname ) t1 order by px
select t1.* , px = (select count(distinct 平均成绩) from (
select m.S# [学生编号] , m.Sname [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] from Student m left join SC n on m.S# = n.S# group by m.S# , m.Sname
) t2 where 平均成绩 >= t1.平均成绩) from (
select m.S# [学生编号] , m.Sname [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] from Student m left join SC n on m.S# = n.S# group by m.S# , m.Sname ) t1 order by px
--24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
select t.* , px = rank() over(order by [平均成绩] desc) from (
select m.S# [学生编号] , m.Sname [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] from Student m left join SC n on m.S# = n.S# group by m.S# , m.Sname ) t order by px
select t.* , px = DENSE_RANK() over(order by [平均成绩] desc) from (
select m.S# [学生编号] , m.Sname [学生姓名] ,
isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] from Student m left join SC n on m.S# = n.S# group by m.S# , m.Sname ) t order by px
--25、查询各科成绩前三名的记录 --25.1 分数重复时保留名次空缺
select m.* , n.C# , n.score from Student m, SC n where m.S# = n.S# and n.score in
(select top 3 score from sc where C# = n.C# order by score desc) order by n.C# , n.score desc --25.2 分数重复时不保留名次空缺,合并名次 --sql 2000用子查询实现
select * from (select t.* , px = (select count(distinct score) from SC where C# = t.C# and score >= t.score) from sc t) m where px between 1 and 3 order by m.c# , m.px --sql 2005用DENSE_RANK实现
select * from (select t.* , px = DENSE_RANK() over(partition by c# order by score desc) from sc t) m where px between 1 and 3 order by m.C# , m.px
--26、查询每门课程被选修的学生数 select c# , count(S#)[学生数] from sc group by C#
--27、查询出只有两门课程的全部学生的学号和姓名 select Student.S# , Student.Sname from Student , SC where Student.S# = SC.S#
group by Student.S# , Student.Sname having count(SC.C#) = 2 order by Student.S#
--28、查询男生、女生人数
select count(Ssex) as 男生人数 from Student where Ssex = N'男'