--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
--1.1、查询同时存在"01"课程和"02"课程的情况
select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数] from Student a , SC b , SC c wherea.S# = b.S# and a.S# = c.S# and b.C# = '01' and c.C# = '02' and b.score > c.score
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数] from Student a left join SC b ona.S# = b.S# and b.C# = '01' left join SC c on a.S# = c.S# and c.C# = '02' where b.score >isnull(c.score,0)
注解:
1. select a.*, b.score as 课程1, bb.score as 课程2 from student a,sc b,sc bb where a.s=b.s and a.s=bb.s and b.c=1 and bb.c=2 and b.score>bb.score;
2. select a.*, b.score as 课程1, bb.score as 课程2 from student a left join sc b on a.s=b.s and b.c=1 left join sc bb on a.s=bb.s and bb.c=2 where b.score>bb.score;
3. select a.*, b.score as 课程1, bb.score as 课程2 from student a,sc b,sc bb where a.s=b.s and a.s=bb.s and b.c=1 and bb.c=2 and b.score> ifnull(bb.score,0);
4. select a.*, b.score as 课程1, bb.score as 课程2 from student a left join sc b on a.s=b.s and b.c=1 left join sc bb on a.s=bb.s and bb.c=2 where b.score> ifnull(bb.score,0);
运行上面4句话的结果如下:
分析:第三句中 and bb.c=2 把没有学习课程2的项排除在外,所以后面的 ifnull(bb.score,0)也就没意义了。可以看出这是left join 和 第一句and之间的区别。
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
--2.1、查询同时存在"01"课程和"02"课程的情况
select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数] from Student a , SC b , SC c wherea.S# = b.S# and a.S# = c.S# and b.C# = '01' and c.C# = '02' and b.score < c.score
--2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况
select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数] from Student a left join SC b ona.S# = b.S# and b.C# = '01' left join SC c on a.S# = c.S# and c.C# = '02' where isnull(b.score,0)< c.score
注解:这个题和第一题只是大于号反写和ifnull的差别
1. select a.*, b.score as 课程1, bb.score as 课程2 from student a,sc b,sc bb where a.s=b.s and a.s=bb.s and b.c=1 and bb.c=2 and b.score<bb.score;
2. select a.*, b.score as 课程1, bb.score as 课程2 from student a left join sc b on a.s=b.s and b.c=1 left join sc bb on a.s=bb.s and bb.c=2 where b.score<bb.score;
3. select a.*, b.score as 课程1, bb.score as 课程2 from student a,sc b,sc bb where a.s=b.s and a.s=bb.s and b.c=1 and bb.c=2 and ifnull(b.score,0)< ifnull(bb.score,0);
4. select a.*, b.score as 课程1, bb.score as 课程2 from student a left join sc b on a.s=b.s and b.c=1 left join sc bb on a.s=bb.s and bb.c=2 where ifnull(b.score,0)< ifnull(bb.score,0);
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score from Student a , sc b wherea.S# = b.S# group by a.S# , a.Sname having cast(avg(b.score) as decimal(18,2)) >= 60 order bya.S#
注解:
1. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a,sc b where a.s=b.s;
2. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a,sc b where a.s=b.s group by a.s;
3. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a,sc b where a.s=b.s having avg(b.score)>60;
4. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a,sc b where a.s=b.s group by a.s having avg(b.score)>60;
1和2比较:GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
为什么不能使用1呢?解释如下:上面的 SELECT 语句指定了3列(学生编号,姓名和 平均成绩)。"avg(b.score)" 返回一个单独的值,而 "Customer" 返回 7 个值(每个值对应 "student表和sc表" 组合的每一行,sc表中没有学生8的记录,8号学生没参加任何考试)。因此,我们得不到正确的结果。不过,您已经看到了,GROUP BY 语句解决了这个问题。
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用。
--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
--4.1、查询在sc表存在成绩的学生信息的SQL语句。
select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score from Student a , sc b wherea.S# = b.S# group by a.S# , a.Sname having cast(avg(b.score) as decimal(18,2)) < 60 order by a.S#
--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
select a.S# , a.Sname , isnull(cast(avg(b.score) as decimal(18,2)),0) avg_score from Student aleft join sc b on a.S# = b.S# group by a.S# , a.Sname having isnull(cast(avg(b.score) asdecimal(18,2)),0) < 60 order by a.S#
注解:
1. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a, sc b where a.s=b.s group by a.s; 2. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a, sc b where a.s=b.s group by a.s having avg(b.score)<60; 3. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a left join sc b on a.s=b.s group by a.s; 4. select a.s as 学生编号, a.sname as 姓名, avg(b.score) as 平均成绩 from student a left join sc b on a.s=b.s group by a.s having ifnull(avg(b.score),0)<60;
解释:1和3的区别在于where和left join的区别,第一题已经讲过。group by和having的作用第3题讲过了。
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
--5.1、查询所有有成绩的SQL。
select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总成绩]from Student a , SC b where a.S# = b.S# group by a.S#,a.Sname order by a.S#
--5.2、查询所有(包括有成绩和无成绩)的SQL。
select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总成绩]from Student a left join SC b on a.S# = b.S# group by a.S#,a.Sname order by a.S#
注解:
1. select a.s as 学生编号, a.sname as 姓名, sum(b.score) as 平均成绩 from student a, sc b where a.s=b.s group by a.s; 2. select a.s as 学生编号, a.sname as 姓名, sum(b.score) as 平均成绩 from student a left join sc b on a.s=b.s group by a.s; 3. select a.s as 学生编号, a.sname as 姓名, ifnull(sum(b.score),0) as 平均成绩 from student a left join sc b on a.s=b.s group by a.s;
--6、查询"李"姓老师的数量
--方法1
select count(Tname) ["李"姓老师的数量] from Teacher where Tname like N'李%'
--方法2
select count(Tname) ["李"姓老师的数量] from Teacher where left(Tname,1) = N'李'
--7、查询学过"张三"老师授课的同学的信息
select distinct Student.* from Student , SC , Course , Teacher where Student.S# = SC.S# and SC.C#= Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三' order by Student.S#
注解:SQL SELECT DISTINCT 语句
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。
关键词 DISTINCT 用于返回唯一不同的值。
--8、查询没学过"张三"老师授课的同学的信息
select m.* from Student m where S# not in (select distinct SC.S# from SC , Course , Teacher whereSC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'张三') order by m.S#
注解:
1. select * from student where student.s not in (select a.* from student a,sc b,course co,teacher te where a.s=b.s and b.c=co.c and co.t=te.t and te.Tname='张三');
2. select * from student where student.s not in (select b.s from sc b,course co,teacher te where b.c=co.c and co.t=te.t and te.Tname='张三');
第1句有语法错误,第二句正确。
--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
--方法1
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and exists (Select 1from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#
--方法2
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '02' and exists (Select 1from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '01') order by Student.S#
--方法3
select m.* from Student m where S# in ( select S# from ( select distinct S# from SC where C# ='01' union all select distinct S# from SC where C# = '02' ) t group by S# having count(1) = 2 )order by m.S#
注解:
1. select a.* from student a,sc b,sc bb where a.s=b.s and b.c=1 and bb.c=2; 2. select distinct a.* from student a,sc b,sc bb where a.s=b.s and b.c=1 and bb.c=2; 3. select a.* from student a,sc b,sc bb where a.s=b.s and b.c=1 and bb.c=2 group by a.s; 4. select a.* from student a,sc b,sc bb where a.s=b.s and b.c=1 and bb.c=2 order by a.s;
--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
--方法1
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and not exists(Select 1 from SC SC_2 where SC_2.S# = SC.S# and SC_2.C# = '02') order by Student.S#
--方法2
select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and Student.S# notin (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、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct Student.* from Student , SC where Student.S# = SC.S# and SC.C# in (select C# fromSC where S# = '01') and Student.S# <> '01'
--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 select Student.* from Student where S# in (select distinct SC.S# from SC where S# <> '01' andSC.C# in (select distinct C# from SC where S# = '01') group by SC.S# having count(1) = (selectcount(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 bystudent.S#
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.S# , student.sname , cast(avg(score) as decimal(18,2)) avg_score from student , scwhere student.S# = SC.S# and student.S# in (select S# from SC where score < 60 group by S# havingcount(1) >= 2) group by student.S# , student.sname
--16、检索"01"课程分数小于60,按分数降序排列的学生信息
select student.* , sc.C# , sc.score from student , sc where student.S# = SC.S# and sc.score < 60and 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 nullend) [语文], max(case c.Cname when N'数学' then b.score else null end) [数学], max(case c.Cnamewhen N'英语' then b.score else null end) [英语], cast(avg(b.score) as decimal(18,2)) 平均分 fromStudent a left join SC b on a.S# = b.S# left join Course c on b.C# = c.C# group by a.S# , a.Snameorder 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)