logo资料库

sql100个例子(内含建表代码,答案).doc

第1页 / 共12页
第2页 / 共12页
第3页 / 共12页
第4页 / 共12页
第5页 / 共12页
第6页 / 共12页
第7页 / 共12页
第8页 / 共12页
资料共12页,剩余部分请下载后查看
问题及描述: --1.学生表 Student(S#,Sname,Sage,Ssex) --S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course(C#,Cname,T#) --C# --课程编号,Cname 课程名称,T# 教师编号 --3.教师表 Teacher(T#,Tname) --T# 教师编号,Tname 教师姓名 --4.成绩表 SC(S#,C#,score) --S# 学生编号,C# 课程编号,score 分数 */ --创建测试数据 create table Student(S# varchar(10),Sname varchar(10),Sage date,Ssex varchar(10)); insert into Student values('01' , '赵雷' , to_date(19891214,'YYYYMMDD') , '男'); insert into Student values('02' , '钱电' , to_date(19901221,'YYYYMMDD') , '男'); insert into Student values('03' , '孙风' , to_date(19900520,'YYYYMMDD') , '男'); insert into Student values('04' , '李云' , to_date(19900806,'YYYYMMDD') , '男'); insert into Student values('05' , '周梅' , to_date(19911201,'YYYYMMDD') , '女'); insert into Student values('06' , '吴兰' , to_date(19920301,'YYYYMMDD') , '女'); insert into Student values('07' , '郑竹' , to_date(19890701,'YYYYMMDD') , '女'); insert into Student values('08' , '王菊' , to_date(19900120,'YYYYMMDD') , '女'); create table Course(C# varchar(10),Cname varchar(10),T# varchar(10)); insert into Course values('01' , '语文' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03'); create table Teacher(T# varchar(10),Tname varchar(10)); insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('03' , '王五'); create table SC(S# varchar(10),C# varchar(10),score decimal(18,1)); insert into SC values('01' , '01' , 80); insert into SC values('01' , '02' , 90); insert into SC values('01' , '03' , 99); insert into SC values('02' , '01' , 70); insert into SC values('02' , '02' , 60); insert into SC values('02' , '03' , 80); insert into SC values('03' , '01' , 80); insert into SC values('03' , '02' , 80); insert into SC values('03' , '03' , 80); insert into SC values('04' , '01' , 50); insert into SC values('04' , '02' , 30); insert into SC values('04' , '03' , 20); insert into SC values('05' , '01' , 76); insert into SC values('05' , '02' , 87); insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34); insert into SC values('07' , '02' , 89); insert into SC values('07' , '03' , 98); go --1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 --1.1、查询同时存在"01"课程和"02"课程的情况 select a.* , b.score [课程'01'的分数],c.score [课程'02'的分数] from Student a , SC b , SC c where a.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 on a.S# = b.S# and b.C# = '01' isnull(c.score,0) left join SC b left join SC c on a.S# = c.S# and c.C# = '02' where b.score > --2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数 --2.1、查询同时存在"01"课程和"02"课程的情况 select a.* , b.score [ 课 程'01' 的 分 数],c.score [ 课 程'02' 的 分 数] from Student a , SC b , SC c where a.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 c on a.S# = c.S# and c.C# = '02' left join SC where b on a.S# = b.S# and b.C# = '01' isnull(b.score,0) < c.score --3、查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score where a.S# = b.S# order by a.S# from Student a , sc b having cast(avg(b.score) as decimal(18,2)) >= 60 group by a.S# , a.Sname group by a.S# , a.Sname --4、查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩 --4.1、查询在 sc 表存在成绩的学生信息的 SQL 语句。 select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score where a.S# = b.S# 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 left join sc b decimal(18,2)),0) group by a.S# , a.Sname on a.S# = b.S# < 60 order by a.S# having cast(avg(b.score) as decimal(18,2)) from Student a , sc b < 60 from Student a having isnull(cast(avg(b.score) as --5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 --5.1、查询所有有成绩的 SQL。 select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总 成绩] order by a.S# --5.2、查询所有(包括有成绩和无成绩)的 SQL。 group by a.S#,a.Sname from Student a , SC b where a.S# = b.S#
select a.S# [学生编号], a.Sname [学生姓名], count(b.C#) 选课总数, sum(score) [所有课程的总 order by a.S# 成绩] from Student a left join SC b group by a.S#,a.Sname on a.S# = b.S# --6、查询"李"姓老师的数量 --方法 1 select count(Tname) ["李"姓老师的数量] from Teacher where Tname like N'李%' --方法 2 select count(Tname) ["李"姓老师的数量] from Teacher where left(Tname,1) = N'李' 姓老师的数量 ----------- 1 */ /* "李" --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# --8、查询没学过"张三"老师授课的同学的信息 select m.* from Student m where 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 m.S# --9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 --方法 1 select Student.* from Student , SC where Student.S# = SC.S# and SC.C# = '01' and 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# = '02' and exists (Select 1 from 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 ( SC where C# = '01' by S# having count(1) = 2 select distinct S# from ) t group select distinct S# from SC where C# = '02' ) order by m.S# select S# from union all ( --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# 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#) --11.2 select Student.* Student.Sname , Student.Sage , Student.Ssex having count(C#) from Student left join SC < (select count(C#) from Course) on Student.S# = SC.S# group by Student.S# , < (select count(C#) from Course) --12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 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、查询和"01"号的同学学习的课程完全相同的其他同学的信息 select Student.* from Student where S# in (select distinct SC.S# from SC where S# SC.C# in (select distinct C# from SC where S# = '01') count(1) from SC where S#='01')) <> '01' and group by SC.S# having count(1) = (select --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、检索"01"课程分数小于 60,按分数降序排列的学生信息 select student.* , sc.C# , sc.score from student , sc 60 and sc.C# = '01' order by sc.score desc where student.S# = SC.S# and sc.score < --17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 --17.1 SQL 2000 静态 select a.S# 学生编号 , a.Sname 学生姓名 , 语文' then b.score else null end) [语文], then b.score else null end) [数学], b.score else null end) [英语], from Student a left join SC b on a.S# = b.S# group by a.S# , a.Sname order by 平均分 desc max(case c.Cname when N' max(case c.Cname when N'数学' max(case c.Cname when N'英语' then cast(avg(b.score) as decimal(18,2)) 平均分 left join Course c on b.C# = c.C# --17.2 SQL 2000 动态 declare @sql nvarchar(4000) + N'学生姓名' b.score else null end) ['+Cname+']' set @sql = 'select a.S# ' + N'学生编号' + ' , a.Sname ' select @sql = @sql + ',max(case c.Cname when N'''+Cname+''' then 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) from Course m , SC n where m.C# = n.C# (select min(score) from SC where C# = m.C#) [最低分], cast(avg(n.score) as decimal(18,2)) [平均分], max(n.score) [最高分], cast((select count(1) from SC where C# = m.C# and score >= 70 and score --18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程 ID,课程 name,最 高分,最低分,平均分,及格率,中等率,优良率,优秀率 --及格为>=60,中等为:70-80, 优良为:80-90,优秀为:>=90 --方法 1 min(n.score) [最 select m.C# [课程编号], m.Cname [课程名称], 低分], cast((select count(1) from SC where C# = m.C# and score >= 60)*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [及格率(%)], < 80 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [ 中 等 率 (%)], cast((select count(1) from SC where C# = m.C# and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [优良率(%)], cast((select count(1) from SC where C# = m.C# and score >= 90)*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [ 优 秀 率 (%)] group by m.C# , m.Cname order by m.C# --方法 2 (select max(score) from SC where C# = m.C#) select m.C# [课程编号], m.Cname [课程名称], (select cast(avg(score) [最高分], cast((select count(1) from SC where C# as decimal(18,2)) from SC where C# = m.C#) [平均分], = m.C# and score >= 60)*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [及 格 率(%)], < 80 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [ 中 等 率 (%)], cast((select count(1) from SC where C# = m.C# and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [优良率(%)], cast((select count(1) from SC where C# = m.C# and score >= 90)*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [优秀率(%)] cast((select count(1) from SC where C# = m.C# and score >= 70 and score from Course m order by m.C# --19、按各科成绩进行排序,并显示排名 --19.1 sql 2000 用子查询完成 --Score 重复时保留名次空缺 select t.* , px = (select count(1) from SC where C# = t.C# and score > t.score) + 1 from sc t order by t.c# , px --Score 重复时合并名次 select t.* , px = (select count(distinct score) from SC where C# = t.C# and score >= t.score) from sc t order by t.c# , px --19.2 sql 2005 用 rank,DENSE_RANK 完成 --Score 重复时保留名次空缺(rank 完成) select t.* , px = rank() over(partition by c# order by score desc) from sc t order by t.C# , px --Score 重复时合并名次(DENSE_RANK 完成) select t.* , px = DENSE_RANK() over(partition by c# order by score desc) from sc t order by t.C# , px
m.Sname [学生姓名] , from Student m left join SC n on m.S# = n.S# isnull(sum(score),0) [总成 group by m.S# , m.Sname order by [总成 --20、查询学生的总成绩并进行排名 --20.1 查询学生的总成绩 select m.S# [学生编号] , 绩] 绩] desc --20.2 查询学生的总成绩并进行排名,sql 2000 用子查询完成,分总分重复时保留名次空缺 和不保留名次空缺两种。 方法 1 重复时保留名次空缺 select t1.* , px = (select count(1) from ( [学生姓名] , m.S# = n.S# ( isnull(sum(score),0) [总成绩] m.S# , m.Sname m.Sname from Student m left join SC n on ) t2 where 总 成 绩 > t1. 总 成 绩 ) + 1 from , group by from Student m left join SC n on m.S# = n.S# select m.S# [ 学 生 编 号 ] isnull(sum(score),0) [总成绩] m.Sname [ 学 生 姓 名 ] group by m.S# , m.Sname select m.S# [学生编号] , ) t1 order by px , 方法 2 不保留名次空缺两种。 select t1.* , px = (select count(distinct 总 成 绩 ) from m.Sname [学生姓名] , n on m.S# = n.S# ( isnull(sum(score),0) [总成绩] m.S# , m.Sname select m.S# [ 学 生 编 号 ] group by m.S# , m.Sname ) t1 order by px , isnull(sum(score),0) [总成绩] ( select m.S# [ 学 生 编 号 ] , from Student m left join SC ) t2 where 总 成 绩 >= t1. 总 成 绩) from , group by m.Sname [ 学 生 姓 名 ] from Student m left join SC n on m.S# = n.S# order by px group by m.S# , m.Sname select m.S# [ 学 生 编 号 ] , from Student m left join SC select t.* , px = select m.S# [ 学 生 编 号 ] , from Student m left join SC --20.3 查询学生的总成绩并进行排名,sql 2005 用 rank,DENSE_RANK 完成,分总分重复时保 留名次空缺和不保留名次空缺两种。 select t.* , px = rank() over(order by [ 总 成 绩] desc) from ( m.Sname [学生姓名] , isnull(sum(score),0) [总成绩] n on m.S# = n.S# ) t DENSE_RANK() over(order by [ 总 成 绩 ] desc) from ( m.Sname [学生姓名] , n on m.S# = n.S# --21、查询不同老师所教不同课程平均分从高到低显示 select m.T# , m.Tname , cast(avg(o.score) as decimal(18,2)) avg_score n , SC o where m.T# = n.T# and n.C# = o.C# desc --22、查询所有课程的成绩第 2 名到第 3 名的学生信息及该课程成绩 --22.1 sql 2000 用子查询完成 --Score 重复时保留名次空缺 select * from (select t.* , px = (select count(1) from SC where C# = t.C# and score > t.score) + 1 from sc t) m where px between 2 and 3 order by m.c# , m.px isnull(sum(score),0) [总成绩] ) t order by px from Teacher m , Course order by avg_score group by m.T# , m.Tname group by m.S# , m.Sname --Score 重复时合并名次 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 2 and 3 order by m.c# , m.px
--22.2 sql 2005 用 rank,DENSE_RANK 完成 --Score 重复时保留名次空缺(rank 完成) select * from (select t.* , px = rank() over(partition by c# order by score desc) from sc t) m where px between 2 and 3 order by m.C# , m.px --Score 重复时合并名次(DENSE_RANK 完成) select * from (select t.* , px = DENSE_RANK() over(partition by c# order by score desc) from sc t) m where px between 2 and 3 order by m.C# , m.px --23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占 百分比 3 --23.1 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60] 向显示 select Course.C# [课程编号] , Cname as [课程名称] , 0 end) [85-100], sum(case when score >= 60 and score score Course.C# , Course.Cname order by Course.C# sum(case when score >= 85 then 1 else < 85 then 1 else 0 end) [70-85], sum(case when group by sum(case when score >= 70 and score < 70 then 1 else 0 end) [60-70], < 60 then 1 else 0 end) [0-60] where SC.C# = Course.C# from sc , Course --横 < 85 then '70-85' end) , else '0-60' case when n.score >= 85 then when count(1) 数量 case when < 85 then '70-85' order by end) group by m.C# , m.Cname , ( when n.score >= 70 and n.score else '0-60' when n.score >= 70 and n.score --纵向显示 1(显示存在的分数段) select m.C# [课程编号] , m.Cname [课程名称] , 分数段 = ( '85-100' n.score >= 60 and n.score < 70 then '60-70' from Course m , sc n where m.C# = n.C# n.score >= 85 then '85-100' when n.score >= 60 and n.score m.C# , m.Cname , 分数段 --纵向显示 2(显示存在的分数段,不存在的分数段用 0 显示) select m.C# [课程编号] , m.Cname [课程名称] , 分数段 = ( '85-100' n.score >= 60 and n.score < 70 then '60-70' from Course m , sc n where m.C# = n.C# n.score >= 85 then '85-100' when n.score >= 60 and n.score m.C# , m.Cname , 分数段 when n.score >= 70 and n.score < 70 then '60-70' < 70 then '60-70' < 85 then '70-85' end) , else '0-60' case when n.score >= 85 then when count(1) 数量 case when < 85 then '70-85' order by end) group by all m.C# , m.Cname , ( when n.score >= 70 and n.score else '0-60' --23.2 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[ <60]及所占 百分比 --横向显示 select m.C# 课程编号, m.Cname 课程名称, (select count(1) from SC where C# = m.C# and score SC where C# = m.C# and score decimal(18,2)) [百分比(%)], score score cast((select count(1) from < 60)*100.0 / (select count(1) from SC where C# = m.C#) as (select count(1) from SC where C# = m.C# and score >= 60 and < 70) [60-70], cast((select count(1) from SC where C# = m.C# and score >= 60 and < 70)*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [百分比(%)], < 60) [0-60],
< 70 then '60-70' when n.score >= 70 and n.score case when n.score >= 85 then when count(1) 数 cast(count(1) * 100.0 / (select count(1) from sc where C# = m.C#) as decimal(18,2)) case < 85 then '70-85' end) order by from Course m , sc n where m.C# = n.C# < 85 then '70-85' end) , when n.score >= 70 and n.score group by m.C# , m.Cname , ( < 85) [70-85], (select count(1) from SC where C# = m.C# and score >= 70 and score < 85)*100.0 / (select cast((select count(1) from SC where C# = m.C# and score >= 70 and score (select count(1) from SC count(1) from SC where C# = m.C#) as decimal(18,2)) [百分比(%)], cast((select count(1) from SC where C# = m.C# where C# = m.C# and score >= 85) [85-100], and score >= 85)*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) [百分比(%)] from Course m order by m.C# --纵向显示 1(显示存在的分数段) select m.C# [课程编号] , m.Cname [课程名称] , 分数段 = ( '85-100' n.score >= 60 and n.score 量 , [百分比(%)] when n.score >= 85 then '85-100' when n.score >= 60 and n.score m.C# , m.Cname , 分数段 --纵向显示 2(显示存在的分数段,不存在的分数段用 0 显示) select m.C# [课程编号] , m.Cname [课程名称] , 分数段 = ( '85-100' n.score >= 60 and n.score 量 , [ 百 分 比 (%)] ( then '70-85' end) order by m.C# , m.Cname , 分数段 case when n.score >= 85 then when count(1) 数 cast(count(1) * 100.0 / (select count(1) from sc where C# = m.C#) as decimal(18,2)) group by all m.C# , m.Cname , < 85 else '0-60' when n.score >= 70 and n.score < 70 then '60-70' from Course m , sc n where m.C# = n.C# < 85 then '70-85' end) , case when n.score >= 85 then '85-100' when n.score >= 70 and n.score when n.score >= 60 and n.score < 70 then '60-70' < 70 then '60-70' else '0-60' else '0-60' else '0-60' --24、查询学生平均成绩及其名次 --24.1 查询学生的平均成绩并进行排名,sql 2000 用子查询完成,分平均成绩重复时保留名 次空缺和不保留名次空缺两种。 select m.S# [学生编号] , isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] group by m.S# , m.Sname order by px select m.S# [学生编号] , isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] group by m.S# , m.Sname ( select t1.* , px = (select count(1) from ( [学生姓名] , m left join SC n on m.S# = n.S# 成 绩) + 1 from select m.S# [ 学 生 编 号] , isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] n.S# count(distinct 平均成绩) from ( 姓名] , left join SC n on m.S# = n.S# select m.S# [ 学 生 编 号 ] , 成 绩 ) from isnull(cast(avg(score) as decimal(18,2)),0) [平均成绩] n.S# group by m.S# , m.Sname group by m.S# , m.Sname ) t1 ) t1 order by px ( m.Sname from Student ) t2 where 平均成绩 > t1.平均 m.Sname [ 学 生 姓 名] , from Student m left join SC n on m.S# = select t1.* , px = (select m.Sname [学生 from Student m ) t2 where 平均成绩 >= t1.平均 m.Sname [ 学 生 姓 名 ] , from Student m left join SC n on m.S# =
分享到:
收藏