1. 程式人生 > >sql語句多表查詢(學生表/課程表/教師表/成績表 )

sql語句多表查詢(學生表/課程表/教師表/成績表 )

問題及描述:
--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 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"課程成績高的學生的資訊及課程分數
--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"課程成績低的學生的資訊及課程分數
--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 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(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分的同學的學生編號和學生姓名和平均成績
--4.1、查詢在sc表存在成績的學生資訊的SQL語句。
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.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、查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績
--5.1、查詢所有有成績的SQL。
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(score) [所有課程的總成績]
from Student a left join SC b
on a.S# = b.S#
group by a.S#,a.Sname
order by a.S#

--6、查詢"李"姓老師的數量
--方法1
select count(Tname) ["李"姓老師的數量] from Teacher where Tname like N'李%'
--方法2
select count(Tname) ["李"姓老師的數量] from Teacher where left(Tname,1) = N'李'

--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
(
  select S# from
  (
    select distinct S# from SC where C# = '01'
    union all
    select distinct S# from SC where C# = '02'
  ) t group by S# having count(1) = 2
)
order by m.S#

--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#) < (select count(C#) from Course)
--11.2
select Student.*
from Student left join SC
on Student.S# = SC.S#
group by Student.S# , Student.Sname , Student.Sage , Student.Ssex having count(C#) < (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# <> '01' and SC.C# in (select distinct C# from SC where S# = '01')
group by SC.S# having count(1) = (select count(1) from SC where S#='01'))

--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
where student.S# = SC.S# and sc.score < 60 and sc.C# = '01'
order by sc.score desc

--17、按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
--17.1 SQL 2000 靜態
select a.S# 學生編號 , a.Sname 學生姓名 ,
       max(case c.Cname when N'語文' then b.score else null end) [語文],
       max(case c.Cname when N'數學' then b.score else null end) [數學],
       max(case c.Cname when N'英語' then b.score else null end) [英語],
       cast(avg(b.score) as decimal(18,2)) 平均分
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 平均分 desc
--17.2 SQL 2000 動態
declare @sql nvarchar(4000)
set @sql = 'select a.S# ' + N'學生編號' + ' , a.Sname ' + N'學生姓名'
select @sql = @sql + ',max(case c.Cname when N'''+Cname+''' then b.score else null end) ['+Cname+']'
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)


--24、查詢學生平均成績及其名次
--24.1 查詢學生的平均成績並進行排名,sql 2000用子查詢完成,分平均成績重複時保留名次空缺和不保留名次空缺兩種。
select t1.* , px = (select count(1) from
(
  select m.S# [學生編號] ,
         m.Sname [學生姓名] ,
         isnull(cast(avg(score) as decimal(18,2)),0) [平均成績]
  from Student m left join SC n on m.S# = n.S#
  group by m.S# , m.Sname
) t2 where 平均成績 > t1.平均成績) + 1 from
(
  select m.S# [學生編號] ,
         m.Sname [學生姓名] ,
         isnull(cast(avg(score) as decimal(18,2)),0) [平均成績]
  from Student m left join SC n on m.S# = n.S#
  group by m.S# , m.Sname
) t1
order by px

select t1.* , px = (select count(distinct 平均成績) from
(
  select m.S# [學生編號] ,
         m.Sname [學生姓名] ,
         isnull(cast(avg(score) as decimal(18,2)),0) [平均成績]
  from Student m left join SC n on m.S# = n.S#
  group by m.S# , m.Sname
) t2 where 平均成績 >= t1.平均成績) from
(
  select m.S# [學生編號] ,
         m.Sname [學生姓名] ,
         isnull(cast(avg(score) as decimal(18,2)),0) [平均成績]
  from Student m left join SC n on m.S# = n.S#
  group by m.S# , m.Sname
) t1
order by px
--24.2 查詢學生的平均成績並進行排名,sql 2005用rank,DENSE_RANK完成,分平均成績重複時保留名次空缺和不保留名次空缺兩種。
select t.* , px = rank() over(order by [平均成績] desc) from
(
  select m.S# [學生編號] ,
         m.Sname [學生姓名] ,
         isnull(cast(avg(score) as decimal(18,2)),0) [平均成績]
  from Student m left join SC n on m.S# = n.S#
  group by m.S# , m.Sname
) t
order by px

select t.* , px = DENSE_RANK() over(order by [平均成績] desc) from
(
  select m.S# [學生編號] ,
         m.Sname [學生姓名] ,
         isnull(cast(avg(score) as decimal(18,2)),0) [平均成績]
  from Student m left join SC n on m.S# = n.S#
  group by m.S# , m.Sname
) t
order by px
 
--25、查詢各科成績前三名的記錄
--25.1 分數重複時保留名次空缺
select m.* , n.C# , n.score from Student m, SC n where m.S# = n.S# and n.score in
(select top 3 score from sc where C# = n.C# order by score desc) order by n.C# , n.score desc
--25.2 分數重複時不保留名次空缺,合併名次
--sql 2000用子查詢實現
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 1 and 3 order by m.c# , m.px
--sql 2005用DENSE_RANK實現
select * from (select t.* , px = DENSE_RANK() over(partition by c# order by score desc) from sc t) m where px between 1 and 3 order by m.C# , m.px

--26、查詢每門課程被選修的學生數
select c# , count(S#)[學生數] from sc group by C#

--27、查詢出只有兩門課程的全部學生的學號和姓名
select Student.S# , Student.Sname
from Student , SC
where Student.S# = SC.S#
group by Student.S# , Student.Sname
having count(SC.C#) = 2
order by Student.S#

--28、查詢男生、女生人數
select count(Ssex) as 男生人數 from Student where Ssex = N'男'
select count(Ssex) as 女生人數 from Student where Ssex = N'女'
select sum(case when Ssex = N'男' then 1 else 0 end) [男生人數],sum(case when Ssex = N'女' then 1 else 0 end) [女生人數] from student
select case when Ssex = N'男' then N'男生人數' else N'女生人數' end [男女情況] , count(1) [人數] from student group by case when Ssex = N'男' then N'男生人數' else N'女生人數' end

--29、查詢名字中含有"風"字的學生資訊
select * from student where sname like N'%風%'
select * from student where charindex(N'風' , sname) > 0

--30、查詢同名同性學生名單,並統計同名人數
select Sname [學生姓名], count(*) [人數] from Student group by Sname having count(*) > 1

--31、查詢1990年出生的學生名單(注:Student表中Sage列的型別是datetime)
select * from Student where year(sage) = 1990
select * from Student where datediff(yy,sage,'1990-01-01') = 0
select * from Student where datepart(yy,sage) = 1990
select * from Student where convert(varchar(4),sage,120) = '1990'

--32、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select m.C# , m.Cname , cast(avg(n.score) as decimal(18,2)) avg_score
from Course m, SC n
where m.C# = n.C#   
group by m.C# , m.Cname
order by avg_score desc, m.C# asc

--33、查詢平均成績大於等於85的所有學生的學號、姓名和平均成績
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)) >= 85
order by a.S#

--34、查詢課程名稱為"數學",且分數低於60的學生姓名和分數
select sname , score
from Student , SC , Course
where SC.S# = Student.S# and SC.C# = Course.C# and Course.Cname = N'數學' and score < 60

--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