sql基礎題目測試及正確答案
在網上做了一套基本的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
sql基礎題目測試及正確答案