SQL高级查询训练题,含答案

--create database practice
--on primary 
--(name='practice_data',
--filename='E:\sqlspace\practice_data.mdf'
--)
--use practice

--create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))
--insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男')
--insert into Student values('02' , N'钱电' , '1990-12-21' , N'男')
--insert into Student values('03' , N'孙风' , '1990-05-20' , N'男')
--insert into Student values('04' , N'李云' , '1990-08-06' , N'男')
--insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')
--insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女')
--insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女')
--insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
--create table Course(C# varchar(10),Cname nvarchar(10),T# varchar(10))
--insert into Course values('01' , N'语文' , '02')
--insert into Course values('02' , N'数学' , '01')
--insert into Course values('03' , N'英语' , '03')
--create table Teacher(T# varchar(10),Tname nvarchar(10))
--insert into Teacher values('01' , N'张三')
--insert into Teacher values('02' , N'李四')
--insert into Teacher values('03' , N'王五')
--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

use practice
select * from SC 
select * from Teacher
select * from Student
select * from Course

--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

select a.*,b.score as '01课程分数',c.score as '02课程分数'
from student as a 
 left join SC as b on a.S#=b.S# and b.C#='01' 
 left join SC as c on c.S#=a.S# and c.C#='02'
where  b.score>ISNULL(c.score,0)  
 
   
--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 
left join SC b on a.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)
 
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select a.*,c.score as '01课程分数',b.score as '02课程分数'
from Student as a 
left join SC as c on c.S#=a.S# and c.C#='01' 
left join SC as b on b.S#=a.S# and b.C#='02'
where c.score<ISNULL(b.score,0) 
 
--2.1、查询同时存在"01"课程和"02"课程的情况
select a.*,b.score as'01课程分数',c.score as '02课程分数'
 from  student as a,SC as b ,SC as c
 where b.C#='01' and c.C#='02'and b.S#=a.S# and c.S#=a.S# and b.score<c.score

----------------------------------------------
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"课程的情况
--分析:即如果没有01的分数,有02的分数就认为是01的分数比02的分数低,就要用isnull 函数,判断是否为空,如果空则用0分替代
--所以查询时要用left做外链接,把所有的人都查进来

select a.*,b.score as  '01课程分数' ,c.score as '02课程分数'
 from Student as a 
left join SC as b on a.S#=b.S# and b.C#='01'
left join SC as c on c.S#=a.S# and c.C#='02'
where ISNULL(b.score,0)<c.score
 -------------------------------------------------------------------
  
select a.* , b.score [课程"01"的分数],c.score [课程"02"的分数] from Student a 
left join SC b on a.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
 
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select a.S#,a.Sname  ,CAST(avg(c.score) as decimal(5,2))
from Student as a 
join SC as c on a.S#=c.S#
group by a.S#,a.Sname
having CAST(avg(c.score) as decimal(5,2))>=60
order by a.S#
 
----------------------------------------------------------
select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score
from Student a , sc b
where a.S# = b.S#
group by a.S# , a.Sname
having cast(avg(b.score) as decimal(18,2)) >= 60 
order by a.S#
 
--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select a.S# 学号,a.Sname 姓名,CAST(AVG(b.score) as decimal(5,2)) 平均分
from Student as a ,SC as b
 where a.S#=b.S# 
 group by a.S#,a.Sname
 having  CAST(AVG(b.score) as decimal(5,2))<60

  ----------------------------------------------------


--4.1、查询在sc表存在成绩的学生信息的SQL语句。
select a.* 
 from student as a
  where a.S# in( select distinct(b.S#) from SC as b )

    

---------------------------------------------------
select a.S# , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score
from Student a , sc b
where a.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.*
from student as a 
where a.S# not in (select b.S# from SC as b )

 --------------------------------------------------
select a.S# , a.Sname , isnull(cast(avg(b.score) as decimal(18,2)),0) avg_score
from Student a left join sc b
on a.S# = b.S#
group by a.S# , a.Sname
having isnull(cast(avg(b.score) as decimal(18,2)),0) < 60 
order by a.S#
 
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.S# 学生编号,a.Sname 姓名,COUNT(b.C#)  选课总数, 总成绩=isnull(convert(varchar(20),sum(b.score)),'空')---聚合函数里面的内容可以不放分组里面
 from Student as  a 
   join SC as b   
 on a.S#=b.S# 
 group by a.S#,b.S#,a.Sname
 ----------------------------------------
  
   use practice 
--5.1、查询所有有成绩的SQL。
select a.S# 学号,a.Sname 姓名,count(b.C#) 选课总数,SUM(b.score) 总成绩
 from Student as a ,SC as b
 where a.S#=b.S# 
 group by a.S#,a.Sname


 ------------------------------------
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(b.score) 总分
from Student as a left join 
SC as b on a.S#=b.S#
group by a.S#,a.Sname

 
--------------------
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#
 
--6、查询"李"姓老师的数量 
select count(*) 李老师的数量
from Teacher 
where left(Tname,1)=N'李'
--where Tname like'李%'

-------------------
--方法1
select count(Tname) ["李"姓老师的数量] from Teacher where Tname like N'李%'
--方法2
select count(Tname) ["李"姓老师的数量] from Teacher where left(Tname,1) = N'李'
/*
"李"姓老师的数量   
----------- 
1
*/
 
--7、查询学过"张三"老师授课的同学的信息
select a.* 
from student as a 
where a.S# in (select distinct(b.S#) from SC as b ,Course as c ,Teacher as d where b.C#=c.C# and c.T#=c.T# and d.Tname='张三')

-----------------------------------------------------------------------------------------------


 
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 a.* 
from student as a 
where a.S# not  in (select distinct(b.S#) from SC as b ,Course as c ,Teacher as d where b.C#=c.C# and c.T#=c.T# and d.Tname='张三')
--------------------------------------------------------------
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"的课程的同学的信息
select a.*
from Student as a 
join SC as b on b.S#=a.S# 
join SC as c on c.S#=a.S#
where c.C#='01' and b.C#='02' 


---方法二
select a.*
from Student as a 
join SC as c on c.S#=a.S# 
where c.C#='01' and  a.S# in (select distinct(b.S#) from SC as b where b.C#='02')
--方法三
select a.*
from student as a 
join SC as b on b.S#=a.S#
where b.C#='01' and exists(select * from SC as c where c.S#=a.S# and c.C#='02')
---方法三
select  a.*
from student as a 
    where a.S# in (
        select s# from 
            (
                select distinct s# from SC   where c#='01'
                union all                                ----合并结果集,不会消除相同的结果
                select distinct s# from sc  where c#='02'
            )  b group by s# having count(*)>=2 --选中的课程数大于等于二

)

-----------------------------------------------------
--方法一
select a.*
from Student as a ,SC as b 
where a.S#=b.S# and b.C#='01' and exists(select * from SC as c where c.S#=b.S# and c.C#='02')
---

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值