在网上做了一套基本的sql题目,以下是我的写的答案,适合基础人员练练
--创建测试数据
use test
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
注意:有些题目为了方便测试,可以自行修改表中的数据
--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select s.* ,a.score as ‘01‘ ,b.score as ‘02‘ from Student s
left join sc a on a.S#=s.S# and a.C#=‘01‘
left join sc b on b.S#=s.S# and b.C#=‘02‘
where a.score>b.score
select a.* ,b.C# , b.score from Student a,SC b where a.S#=b.S#
and a.S# in ( select S# from SC c1 where C#=‘01‘ and score>
( select score from SC c2 where C#=‘02‘ and c2.S#=c1.S# )
);
--1.1、查询同时存在"01"课程和"02"课程的情况
select a.S#,a.Sname,b.score,c.score from Student a
left join sc b on b.s#=a.s# and b.c#=‘01‘
left join sc c on c.s#=a.s# and c.c#=‘02‘
where b.score>0 and c.score>0;
--1.2、查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)
select a.S#,a.Sname,b.score as ‘01‘,c.score as ‘02‘ from Student a
left join sc b on b.s#=a.s# and b.c#=‘01‘
left join sc c on c.s#=a.s# and c.c#=‘02‘
Where b.score>isnull(c.score,0);
--1.2.1存在01,但是可能不存在02
--这个表示左连接后c#编号为null的也包含进去
select * from student s left join sc a on s.S#=a.S# and a.C#=‘02‘
--这个表示左连接后c#编号必须为‘02‘,不包含null
select * from student s left join sc a on s.S#=a.S# where a.C#=‘02‘
--这两个结起来可以是01必须有,02可以不要
select s.*,a.score as ‘01‘,b.score as ‘02‘ from student s
left join sc a on a.S#=s.S#
left join sc b on b.s#=s.s# and b.C#=‘02‘
where a.C#=‘01‘
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select a.S#,a.Sname,b.score ,c.score from Student a
left join sc b on b.s#=a.s# and b.c#=’01’
left join sc c on c.s#=a.s# and c.c#=’02’
Where b.score<c.score;
--2.1、查询同时存在"01"课程和"02"课程的情况
select a.S#,a.Sname,b.score as ‘01‘,c.score as ‘02‘ from Student a
inner join sc b on b.s#=a.s# and b.c#=‘01‘
inner join sc c on c.s#=a.s# and c.c#=‘02‘;
--2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况
select a.S#,a.Sname,b.score as ‘01‘,c.score as ‘02‘ from Student a
left join sc b on b.s#=a.s# and b.c#=‘01‘
left join sc c on c.s#=a.s# and c.c#=‘02‘
Where c.score>isnull(b.score,0);
2.2.1存在01,但是不存在02课程的人
select distinct s.* from student s
left join sc a on a.s#=s.s#
left join sc b on b.s#=s.s#
--"<>"是指不等于的意思
where a.c#=‘02‘ and b.C#<>‘01‘
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.S# ,s.Sname ,avgs from Student s ,
(select s# ,cast(avg(score) as decimal(18,2)) as avgs from sc group by s# having AVG(score)>=60) c
where c.S#=s.S#
--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
select s.S# ,s.Sname ,avgs from Student s ,
(select s# ,cast(avg(score) as decimal(18,2)) as avgs from sc group by s# having AVG(score)<60) c
where c.S#=s.S#
--4.1、查询在sc表存在成绩的学生信息的SQL语句。
select * from Student where s# in (select s# from sc);
--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。
select * from Student where s# not in (select s# from sc);
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select s.s# ,s.Sname ,b.c as 课程数 ,b.d as 总分数 from Student s
left join (select s# ,count(c#) as c,sum(score) as d from sc group by s# ) b
on s.s#=b.S#
--5.1、查询所有有成绩的SQL。
select s.* ,sc.C# ,sc.score from Student s ,sc
where s.S#=sc.S#
--5.2、查询所有(包括有成绩和无成绩)的SQL。
select s.* ,sc.C# ,sc.score from Student s left join
sc on s.S#=sc.S#
--6、查询"李"姓老师的数量
select count(1) as 数量 from Teacher t where t.Tname=‘李%‘
--7、查询学过"张三"老师授课的同学的信息
Select s.s#, s.sname from student s,sc where s.s#=sc.s# and
Sc.c# in(select c.c# from course c,teacher t where c.t#=t.t# and t.tname=‘张三‘);
--8、查询没学过"张三"老师授课的同学的信息
Select distinct(s.s#) , s.sname from student s,sc where s.s#=sc.s# and
Sc.c# not in(select c.c# from course c,teacher t where c.t#=t.t# and t.tname=‘张三‘);
--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select a.S#,a.Sname,b.score ,c.score from Student a
inner join sc b on b.s#=a.s# and b.c#=‘01‘
inner join sc c on c.s#=a.s# and c.c#=‘02‘
--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select a.S#,a.Sname,b.score,c.score from Student a
left join sc b on b.s#=a.s# and b.c#=‘01‘
left join sc c on c.s#=a.s# and c.c#=‘02‘
where c.score is null and b.score>0;
--11、查询没有学全所有课程的同学的信息
select a.S#,a.Sname,b.score,c.score ,d.score from Student a
left join sc b on b.s#=a.s# and b.c#=‘01‘
left join sc c on c.s#=a.s# and c.c#=‘02‘
left join sc d on d.S#=a.S# and d.C#=‘03‘
where c.score is null or b.score is null or d.score is null;
select s.* from Student s
where s# not in (select s# from sc group by s# having count(1)=3)
--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select distinct(a.S#),a.Sname from Student a,sc
where a.S#=sc.S# and sc.C#=any(select C# from Student a,sc
where a.S#=sc.S# and a.S#=‘01‘) and sc.S#!=‘01‘;
select s.* from Student s
where s# in
( select s# from sc where c# in (select C# from sc where S#=‘01‘and s#!=‘01‘))
select distinct s.* from student s
left join sc on s.s# = sc.s#
--先将学号为01 的去处掉,然后再将查找有这几门课程的学生
where sc.c# in ( select c# from sc where sc.s#=‘01‘) and sc.s# <> ‘01‘
--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select S# from SC where S# not in
(select distinct(S#) from SC where C# not in (select C# from SC where S#=‘01‘))
group by S# having count(*)=(select count(*) from SC where S#=‘01‘);
select s.* from Student s
where s# in
( select distinct(s#) from sc
where c# in (select C# from sc where S#=‘01‘) and s#!=‘01‘
group by s# having count(1)=3
)
--更全面些
select * from student s
where s# in (
select s# from sc
where c# in (select c# from sc where s#=‘01‘)
group by s# having count(1)=(select count(1) from sc where s#=‘01‘))
--14、查询没学过"张三"老师讲授的任一门课程的学生姓名
Select distinct(s.s#) , s.sname from student s,sc where s.s#=sc.s# and
Sc.c# not in(select c.c# from course c,teacher t where c.t#=t.t# and t.tname=‘张三‘);
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.S#,a.Sname,avg(sc.score) from Student a,sc
where a.S#=sc.S# and sc.S# in(
select s# from sc where score>=60
group by s# having count(*)>=2 )
group by a.S#,a.Sname;
--16、检索"01"课程分数小于60,按分数降序排列的学生信息
select a.S#,a.Sname,sc.score from Student a,sc
where a.S#=sc.S# and sc.score<60 and sc.c#=‘01‘
order by sc.score desc;
--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select *from
(select s.S#, avg(f.score) as avg from Student s,sc f
where s.S#=f.s#
group by s.S#) a left join
(select a.S#,a.Sname,b.score as bs ,c.score as cs ,d.score as ds from Student a
left join sc b on b.s#=a.s# and b.c#=‘01‘
left join sc c on c.s#=a.s# and c.c#=‘02‘
left join sc d on d.S#=a.S# and d.C#=‘03‘)b on a.S#=b.S#;
--用左连接将一个一个数连接起来,然后再显示出来
select s.*,b.score as ‘01‘,c.score as ‘02‘ , d.score as ‘03‘,a.avg_score from student s
--求出平均分,不计算null值
left join (select s# ,avg (score) as avg_score from sc group by s# ) a on s.S# =a.S#
left join sc b on b.S#=s.s# and b.c#=‘01‘
left join sc c on c.s#=s.s# and c.c#=‘02‘
left join sc d on d.s#= s.s# and d.c#=‘03‘
order by a.avg_score desc
--18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
select m.C# [课程编号], m.Cname [课程名称],
max(n.score) [最高分],
min(n.score) [最低分],
cast(avg(n.score) as decimal(18,2)) [平均分]--,
,cast((select count(1) from SC where C# = m.C# and score >= 60)*100.00 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) as 合格率
,cast((select count(1) from SC where C# = m.C# and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where C# = m.C#) as decimal(18,2)) as 中等率
,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)) as 优良率
,cast((select count(1) from SC where C# = m.C# and score >= 90)*100.00/ (select count(1) from SC where C# = m.C#) as decimal(18,2)) as 优秀率
from Course m , SC n
where m.C# = n.C#
group by m.C# , m.Cname
--19、按各科成绩进行排序,并显示排名
select c#,a.Sname ,score ,rank() over(partition by c# order by score) as ‘排名‘ from sc,student a
where a.S#=sc.S#;
--20、查询学生的总成绩并进行排名
select sc.s#,a.Sname,sum(sc.score) as ‘总成绩‘ from sc,Student a
where sc.S#=a.S#
group by sc.s#,a.Sname order by sum(sc.score)desc;
--20.1 查询学生的总成绩
select sc.s#,a.Sname,sum(sc.score) as ‘总成绩‘ from sc,Student a
where sc.S#=a.S#
group by sc.s#,a.Sname;
--20.2 查询学生的总成绩并进行排名,sql 2000用子查询完成,分总分重复时保留名次空缺和不保留名次空缺两种。
--20.3 查询学生的总成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分总分重复时保留名次空缺和不保留名次空缺两种。
select sc.s#,a.Sname,sum(sc.score) as ‘sscore‘, rank() over(order by sum(sc.score))
from sc,Student a
where sc.S#=a.S#
group by sc.s#,a.Sname
select sc.s#,a.Sname,sum(sc.score) as ‘sscore‘, dense_rank() over(order by sum(sc.score))
from sc,Student a
where sc.S#=a.S#
group by sc.s#,a.Sname
--21、查询不同老师所教不同课程平均分从高到低显示
select a.C#,a.Cname,b.Tname ,avg(sc.score) as ‘avg‘ from sc,Course a,Teacher b
where a.T#=b.T# and sc.C#=a.C#
group by a.C#,a.Cname,b.Tname
order by avg(score) desc
--22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select *from (
select c#,a.Sname ,score ,DENSE_RANK() over(partition by c# order by score) as ‘srank‘ from sc,student a
where a.S#=sc.S#) a
where a.srank between 2 and 3;
--23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select t1.*,round(t1.num1*100/t2.num,2) as ‘百分比‘ from
( select s.C#,c.Cname,
(case when s.score>=85 then ‘100-85‘
when s.score>=70 and s.score<85 then ‘85-70‘
when s.score>=60 and s.score<70 then ‘70-60‘
else ‘0-60‘
end) as px,count(1) as num1
from sc s,Course c
where c.C#=s.C#
group by s.C#,c.Cname,(case when s.score>=85 then ‘100-85‘
when s.score>=70 and s.score<85 then ‘85-70‘
when s.score>=60 and s.score<70 then ‘70-60‘
else ‘0-60‘
end)) t1,
(select s.C#,c.Cname,count(1)as num from sc s,Course c
where s.C#=c.C#
group by s.C#,c.Cname
) t2
where t1.C#=t2.C#;
select
s.c#,c.cname,(select count(1) from sc where C#=s.c# and score between 85 and 100) as [85-100],
(select count(1) from sc where c# = s.c# and score between 70 and 85) as [70-85],
(select count(1) from sc where c# = s.c# and score between 60 and 70) as [60-70],
(select count(1) from sc where c# = s.c# and score<60) as [0-60],
cast(cast((select count(1) from sc where c# = s.c# and score between 85 and 100) as decimal(18,2))*100/
cast((select count(1) from sc where c# = s.c#) as decimal(18,2)) as decimal(18,2)) as[85-100百分比],
cast(cast((select count(1) from sc where c# = s.c# and score between 70 and 85) as decimal(18,2))*100/
cast((select count(1) from sc where c# = s.c#) as decimal(18,2)) as decimal(18,2)) as[70-85百分比],
cast(cast((select count(1) from sc where c# = s.c# and score between 60 and 70) as decimal(18,2))*100/
cast((select count(1) from sc where c# = s.c#) as decimal(18,2)) as decimal(18,2)) as[60-70百分比],
cast(cast((select count(1) from sc where c# = s.c# and score<60) as decimal(18,2))*100/
cast((select count(1) from sc where c# = s.c#) as decimal(18,2)) as decimal(18,2)) as[0-60百分比]
from sc s inner join course c on c.c#=s.c#
group by s.c#,c.cname
--23.1 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]
select s.C#,c.Cname,
( case when s.score>=85 then ‘100-85‘
when s.score>=70 and s.score<85 then ‘85-70‘
when s.score>=60 and s.score<70 then ‘70-60‘
else ‘0-60‘
end
) as px,count(1) as num from sc s,Course c
where s.C#=c.C#
group by s.C#,c.Cname, ( case when s.score>=85 then ‘100-85‘
when s.score>=70 and s.score<85 then ‘85-70‘
when s.score>=60 and s.score<70 then ‘70-60‘
else ‘0-60‘
end
)
order by s.C#;
--23.2 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[<60]及所占百分比
select t1.*,round(t1.num1*100/t2.num,2) as ‘百分比‘ from
( select s.C#,c.Cname,
(case when s.score>=85 then ‘100-85‘
when s.score>=70 and s.score<85 then ‘85-70‘
when s.score>=60 and s.score<70 then ‘70-60‘
else ‘0-60‘
end) as px,count(1) as num1
from sc s,Course c
where c.C#=s.C#
group by s.C#,c.Cname,(case when s.score>=85 then ‘100-85‘
when s.score>=70 and s.score<85 then ‘85-70‘
when s.score>=60 and s.score<70 then ‘70-60‘
else ‘0-60‘
end)) t1,
(select s.C#,c.Cname,count(1)as num from sc s,Course c
where s.C#=c.C#
group by s.C#,c.Cname
) t2
where t1.C#=t2.C#;
--简化版
select s.c#,c.cname,
(case when s.score between 85 and 100 then ‘85-100‘
when s.score between 70 and 85 then ‘70-85‘
when s.score between 60 and 70 then ‘60-70‘
else ‘0-60‘ end) as fshu,count(1) as num,
cast(cast(count(1)*100 as decimal(18,2))
/cast((select count(1) from sc where c#=s.c#) as decimal(18,2))as decimal(18,2)) as ‘百分比‘
from sc s
inner join course c on s.C# = c.C#
group by s.c#,c.cname,
(case when s.score between 85 and 100 then ‘85-100‘
when s.score between 70 and 85 then ‘70-85‘
when s.score between 60 and 70 then ‘60-70‘
else ‘0-60‘ end)
--24、查询学生平均成绩及其名次
select sc.S#,s.Sname,cast(avg(sc.score) as decimal(18,2)) as 平均分,rank() over(order by avg(sc.score)) as 排名 from sc,Student s
where sc.S#=s.S#
group by sc.S#,s.Sname;
select s.s#,s.sname,convert(decimal(18,2),avg(sc.score)) as [分数],
rank() over(order by avg(sc.score)desc ) as 排名 from student s
left join sc on s.s#=sc.s#
group by s.s#,s.sname
--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
--24.2 查询学生的平均成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。
select sc.S#,s.Sname,cast(avg(sc.score) as decimal(18,2)) as 平均分, isnull (rank() over(order by avg(sc.score)),null) as 排名 from sc,Student s
where sc.S#=s.S#
group by sc.S#,s.Sname;
select sc.S#,s.Sname,cast(avg(sc.score) as decimal(18,2)) as 平均分,rank() over(order by avg(sc.score)) as 排名 from sc,Student s
where sc.S#=s.S#
group by sc.S#,s.Sname;
--不保留名次空缺
select s.s#,s.sname,convert(decimal(18,2),avg(sc.score)) as [平均成绩],
dense_rank() over(order by avg(sc.score) desc) as [排名] from student s
left join sc on s.s#=sc.s#
group by s.s#,s.sname
--25、查询各科成绩前三名的记录
--25.1 分数重复时保留名次空缺 ???????????????????????????????
select sc.c# ,sc.s# ,s.Sname , sc.score ,rank() over(partition by sc.c# order by sc.score desc)
from sc ,Student s
where sc.S#=s.S#
--25.2 分数重复时不保留名次空缺,合并名次
select * from(
select c.c#,c.cname,sc.score,dense_rank() over(partition by c.c# order by sc.score desc) as [排名]
from sc left join course c on c.c#=sc.c#
left join student s on sc.s#=s.s# ) t
where t.排名<4
--26、查询每门课程被选修的学生数
select c.C#,c.Cname,count(1) as 数量
from Course c,sc
where c.C#=sc.C#
group by c.C#,c.Cname;
--27、查询出只有两门课程的全部学生的学号和姓名
select s.S#,s.Sname,count(1) as 课程数 from Student s,sc
where s.S#=sc.S#
group by s.S#,s.Sname
having count(1)=2
--28、查询男生、女生人数
select Ssex, count(1) from Student
group by Ssex;
--29、查询名字中含有"风"字的学生信息
select *from student
where sname like ‘%风%‘;
--30、查询同名同性学生名单,并统计同名人数
select Sname, count(1) as 数量 from Student
group by Sname
having COUNT(1)>1;
--31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
select * from Student where year(sage)=1990;
--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select c#, cast(avg(score ) as decimal(18,2)) as avgscore from sc
group by c#
order by avgscore desc,C#
--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select s.S# ,s.Sname ,cast(AVG(sc.score) as decimal(18,2)) as avgscore from Student s, sc
where s.S#=sc.S#
group by s.S#,s.Sname
having avg(score)>=85
--34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select s.Sname ,sc.score from Student s ,Course c ,sc
where s.S#=sc.S# and c.C#=sc.C# and sc.score<60 and c.Cname=‘数学‘;
--35、查询所有学生的课程及分数情况;
select s.Sname ,c.Cname ,sc.score from Student s ,Course c ,sc
where s.S#=sc.S# and sc.C#=c.C#
order by score
--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
select s.S# ,s.Sname ,c.Cname ,sc.score from sc ,Course c ,Student s
where sc.S#=s.S# and c.C#=sc.C# and s.S# in (
select s# from sc a where score>70 group by s#
having count(1)=(select count(1) from sc where s#=a.S# group by s#))
--37、查询不及格的课程
select s.S# ,s.Sname ,c.Cname ,sc.score from sc ,Course c ,Student s
where sc.S#=s.S# and c.C#=sc.C# and sc.score<60
--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
select s.S# ,s.Sname ,sc.score from Student s ,sc
where s.S#=sc.S# and sc.C#=‘01‘ and sc.score>=80
--39、求每门课程的学生人数
select sc.C# ,count(1) as 人数 from sc
group by sc.C#
--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select sc.C# ,s.* ,sc.score from Student s ,sc ,(
select sc.C# ,max(sc.score) as maxscore from Teacher t ,Course c ,sc
where t.T#=c.T# and t.Tname=‘张三‘ and sc.C#=c.C#
group by sc.C# ) t
where s.S#=sc.S# and t.C#=sc.C# and sc.score=t.maxscore
order by sc.C#
--将所有的表连接起来,排列出该老所教课程的所有学生的分数,根据分数从高到低进行排序
--取第一条数据,便是成绩最高的学生了
select top 1 s.s#, s.sname, sc.score from student s inner join sc on s.s#=sc.s#
inner join course c on c.c#=sc.c#
inner join teacher t on c.t#=t.t#
where t.tname=‘张三‘
order by sc.score desc
--40.1 当最高分只有一个时
select s.* ,sc.score from student s ,sc ,
(select sc.C#,t.maxscore ,count(1) as aount from sc , (
select sc.C# ,max(sc.score) as maxscore from Teacher t ,Course c ,sc
where t.T#=c.T# and t.Tname=‘张三‘ and sc.C#=c.C#
group by sc.C# ) t
where t.C#=sc.C# and sc.score=t.maxscore
group by sc.C# ,t.maxscore
having count(1)<2 ) a
where s.S#=sc.S# and a.C#=sc.C# and a.maxscore=sc.score
--将所有的表连接起来,排列出该老所教课程的所有学生的分数,根据分数从高到低进行排序
--取第一条数据,便是成绩最高的学生了
select top 1 s.s#, s.sname, sc.score from student s inner join sc on s.s#=sc.s#
inner join course c on c.c#=sc.c#
inner join teacher t on c.t#=t.t#
where t.tname=‘张三‘
order by sc.score desc
--40.2 当最高分出现多个时
select sc.C# ,s.* ,sc.score from student s ,sc ,
(select sc.C#,t.maxscore ,count(1) as aount from sc , (
select sc.C# ,max(sc.score) as maxscore from Teacher t ,Course c ,sc
where t.T#=c.T# and t.Tname=‘张三‘ and sc.C#=c.C#
group by sc.C# ) t --算出每一门科目的最高分及科目号
where t.C#=sc.C# and sc.score=t.maxscore
group by sc.C# ,t.maxscore
having count(1)>1 ) a --算出多人得分的科目及最高分
where s.S#=sc.S# and a.C#=sc.C# and a.maxscore=sc.score --根据科目号和最高分匹配学生
--先将最高分和课程取出来,然后再将将表进行连接,group by
select t.s#,t.sname,sc.score from student t inner join sc on t.S#=sc.S#
inner join
(
select c.c#, max(sc.score) as maxscore from sc inner join course c on c.C#=sc.C#
inner join teacher t on t.t#=c.t#
where t.tname=‘张三‘
group by c.C#
)a on a.C#=sc.C# and a.maxscore = sc.score
--先将最高分和课程取出来,然后再将将表进行连接,order by
select t.s#,t.sname,sc.score from student t inner join sc on t.S#=sc.S#
inner join
(
select top 1 sc.c#, sc.score as maxscore from sc inner join course c on c.C#=sc.C#
inner join teacher t on t.t#=c.t#
where t.tname=‘张三‘
order by sc.score desc
)a on a.C#=sc.C# and a.maxscore = sc.score
--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
--查询某个学生的课程不同,但是成绩同的情况
select s.* ,a.score as ‘01‘ ,b.score as ‘02‘,c.score as ‘03‘ from Student s
left join sc a on s.S#=a.S# and a.C#=‘01‘
left join sc b on s.S#=b.S# and b.C#=‘02‘
left join sc c on s.S#=c.S# and c.C#=‘03‘
where a.score=b.score or a.score=c.score or b.score=c.score
--42、查询每门功成绩最好的前两名
select * from (
select sc.C# ,s.Sname , sc.score ,
rank() over( partition by sc.c# order by sc.score desc) as scorank
from sc,Student s
where s.S#=sc.S# ) t
where t.scorank<3;
--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select c# ,count(1) as acount from sc
group by c#
having count(1)>5
order by acount desc ,C#
--44、检索至少选修两门课程的学生学号
select s# as 学生学号,count(1) as 课程数 from sc
group by s#
having count(1)>1
--45、查询选修了全部课程的学生信息
select sc.s# ,count(1) as 课程数 from sc
group by sc.S#
having count(1)=(select count(1) from Course )
select distinct(s.S#), s.* from sc,Student s
where sc.S#=s.S# and s.S#=(
select sc.s# as 课程数 from sc
where sc.S#=s.S#
group by sc.S#
having count(1)=(select count(1) from Course ) )
--46、查询各学生的年龄
select s# ,sname ,( year(GETDATE())-year(Sage)) as 年龄 ,Ssex from Student
--46.1 只按照年份来算
select s# ,sname ,( year(GETDATE())-year(Sage)) as 年龄 ,Ssex from Student
或
select s# ,sname ,DATEDIFF(yy ,sage ,getdate()) as 年龄 from student
--46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
--dateadd(month,2,getdate())--日期相加 :比如getdate是2017-12-27 ,显示是:2018-02-27
--datediff(month,sage, getdate()) --日期相减
- 日期指定的部分:月month(这年的第几个月) ,年year, 天打day(这个月的第几天)
- 开始的日期
- 结束的日期
--datename(week,getdate()) --返回日期的指定部分,放回该日期的第几个星期,nvarchar
注释:这个返回的是这个年的第几个星期
--datepart(week,getdate()) --同上,结果返回int型
--convert(varchar(10),getdate(), 120) --以varchar的形式显示前10个字符,显示格式是120
即:2017-12-27
--cast(getdate() as varchar(10)) --与上相同,但是没有显示格式,所以显示出来不好看
select s#, sname,
case when month(getdate())<month(sage) then datediff(year,sage,getdate())-1
when month(getdate())>month(sage) then datediff(year,sage,getdate())
else (case when day(getdate())<=day(sage) then datediff(year,sage,getdate())-1
else datediff(year,sage,getdate())
end)
end
from student
--47、查询本周过生日的学生
--datename(week,getdate()) --返回日期的指定部分,放回该日期的第几个星期,nvarchar
注释:这个返回的是这个年的第几个星期
--datepart(week,getdate()) --同上,结果返回int型
本周的星期一
- SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)
--***wk == week ,从系统指定时间一共过了多少个礼拜
--***datediff只会显示数量,不会显示日期
--显示系统判定时间到现在一共有多少个礼拜了
select datediff(wk,0,getdate())--wk代表星期,getdate()代表现在日期
--***dateadd( [指定格式(wk,day,year)] , [要加入的数量],[从哪天开始加 (getdate())] )
--***dateadd只会显示日期,不会显示数量
--这周星期一的日期,从日期是0开始加入礼拜数量
select dateadd(wk,datediff(wk,0,getdate()),0)
select day(dateadd(wk,datediff(wk,0,getdate()),0))--这天是几号
--这周星期日的日期
select dateadd(wk,datediff(wk,0,getdate())+1,0)-1
--计算是这周过生日的
--流程:1、计算这周的第一天和最后一天,取这两天的几月几号,下面都是这种情况
--2、判断是否存在跨年的,即判断第一天和最后一天(12-25,1-1)的大小,可能存在2017-12-25到2018-01-01这种情况
--如果第一天比最后一天小,则生日就是大于第一天,小于最后一天,否则生日大于最后一天,小于第一天。
select s# , sname,convert(varchar(10),sage,120) as [生日] from student
where convert(varchar(5),sage,110) >=
case when convert(varchar(5),dateadd(wk,datediff(wk,0,getdate()),0),110) < convert(varchar(5),dateadd(wk,datediff(wk,0,getdate())+1,0)-1,110)
then convert(varchar(5),dateadd(wk,datediff(wk,0,getdate()),0),110)
else convert(varchar(5),dateadd(wk,datediff(wk,0,getdate())+1,0)-1,110)
end
and convert(varchar(5),sage,110) <=
case when convert(varchar(5),dateadd(wk,datediff(wk,0,getdate()),0),110) > convert(varchar(5),dateadd(wk,datediff(wk,0,getdate())+1,0)-1,110)
then convert(varchar(5),dateadd(wk,datediff(wk,0,getdate()),0),110)
else convert(varchar(5),dateadd(wk,datediff(wk,0,getdate())+1,0)-1,110)
end
--48、查询下周过生日的学生
select s.* from Student s
where datename(week,day(sage))=DateName(week,day(Getdate()))+1
--这周是月底怎么搞,还有这周是指第1-7天 为第一周,所以有问题
--49、查询本月过生日的学生
select s.* from Student s
where month(sage)=month(getdate())
--50、查询下月过生日的学生
select s.* from Student s
where month(sage)=month(getdate())+1
但是这个月是12月怎么去算
所以,修改下
--**dateadd加上一个月的日期,就能跳到下个月了,然后下个月的月数
select s#,sname, convert(varchar(10), sage, 120) as [生日] from student
where month(sage) = month(dateadd(month , 1, getdate()))
--用case...when 函数,当这个月为12月时就将他改为1
select s# , sname, convert(varchar(10) ,sage, 120) from student s
where datepart(month,sage) =
case when datepart(month,getdate()) = 12 then 1
else datepart(month,getdate())+1 end
1、以下纯取值,不关联表了
- 月初
--datediff从初始到现在一共有多少个月,然后将这些月数相加换成日期
select dateadd(month,datediff(month,0,getdate()),0);
- 月末
--datediff从初始日期到现在一共有多少个月,然后+1就多一个月,最后—1就是下个月的月初变为月末
select dateadd(month,datediff(month, 0, getdate())+1,0)-1
- 下个月的月初
select dateadd(month,datediff(month,0,getdate())+1,0)
- 下个月的月末
select dateadd(month,datediff(month, 0, getdate())+2,0)-1
原文地址:https://www.cnblogs.com/liu0799/p/8135633.html