一個項目SQL3

--35、查詢所有學生的課程及分數情況;
select Student.* , Course.Cname , SC.C# , SC.score  
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C#
order by Student.S# , SC.C#

--36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
select Student.* , Course.Cname , SC.C# , SC.score  
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C# and SC.score >= 70
order by Student.S# , SC.C#

--37、查詢不及格的課程
select Student.* , Course.Cname , SC.C# , SC.score  
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C# and SC.score < 60
order by Student.S# , SC.C#

--38、查詢課程編號爲01且課程成績在80分以上的學生的學號和姓名;
select Student.* , Course.Cname , SC.C# , SC.score  
from Student, SC , Course
where Student.S# = SC.S# and SC.C# = Course.C# and SC.C# = '01' and SC.score >= 80
order by Student.S# , SC.C#

--39、求每門課程的學生人數
select Course.C# , Course.Cname , count(*) [學生人數]
from Course , SC
where Course.C# = SC.C#
group by  Course.C# , Course.Cname
order by Course.C# , Course.Cname

--40、查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績
--40.1 當最高分只有一個時
select top 1 Student.* , Course.Cname , SC.C# , SC.score  
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 SC.score desc
--40.2 當最高分出現多個時
select Student.* , Course.Cname , SC.C# , SC.score  
from Student, SC , Course , Teacher
where Student.S# = SC.S# and SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'張三' and
SC.score = (select max(SC.score) from SC , Course , Teacher where SC.C# = Course.C# and Course.T# = Teacher.T# and Teacher.Tname = N'張三')

--41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
--方法1
select m.* from SC m ,(select C# , score from SC group by C# , score having count(1) > 1) n
where m.C#= n.C# and m.score = n.score order by m.C# , m.score , m.S#
--方法2
select m.* from SC m where exists (select 1 from (select C# , score from SC group by C# , score having count(1) > 1) n
where m.C#= n.C# and m.score = n.score) order by m.C# , m.score , m.S#

--42、查詢每門功成績最好的前兩名
select t.* from sc t where score in (select top 2 score from sc where C# = T.C# order by score desc) order by t.C# , t.score desc

--43、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列  
select Course.C# , Course.Cname , count(*) [學生人數]
from Course , SC
where Course.C# = SC.C#
group by  Course.C# , Course.Cname
having count(*) >= 5
order by [學生人數] desc , Course.C#

--44、檢索至少選修兩門課程的學生學號
select student.S# , student.Sname
from student , SC
where student.S# = SC.S#
group by student.S# , student.Sname
having count(1) >= 2
order by student.S#

--45、查詢選修了全部課程的學生信息
--方法1 根據數量來完成
select student.* from student where S# in
(select S# from sc group by S# having count(1) = (select count(1) from course))
--方法2 使用雙重否定來完成
select t.* from student t where t.S# not in
(
  select distinct m.S# from
  (
    select S# , C# from student , course
  ) m where not exists (select 1 from sc n where n.S# = m.S# and n.C# = m.C#)
)
--方法3 使用雙重否定來完成
select t.* from student t where not exists(select 1 from
(
  select distinct m.S# from
  (
    select S# , C# from student , course
  ) m where not exists (select 1 from sc n where n.S# = m.S# and n.C# = m.C#)
) k where k.S# = t.S#
)

--46、查詢各學生的年齡
--46.1 只按照年份來算
select * , datediff(yy , sage , getdate()) [年齡] from student
--46.2 按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一
select * , case when right(convert(varchar(10),getdate(),120),5) < right(convert(varchar(10),sage,120),5) then datediff(yy , sage , getdate()) - 1 else datediff(yy , sage , getdate()) end [年齡] from student

--47、查詢本週過生日的學生
select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = 0

--48、查詢下週過生日的學生
select * from student where datediff(week,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = -1

--49、查詢本月過生日的學生
select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = 0

--50、查詢下月過生日的學生
select * from student where datediff(mm,datename(yy,getdate()) + right(convert(varchar(10),sage,120),6),getdate()) = -1

drop table  Student,Course,Teacher,SC

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章