sql server 2005 查询练习

数据库建表语句:

if exists (select * from sysobjects where id = OBJECT_ID('[Course]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [Course]

CREATE TABLE [Course] (
[c_no] [varchar] (50) NOT NULL,
[c_name] [varchar] (50) NOT NULL)

INSERT [Course] ([c_no],[c_name]) VALUES ( N'C001',N'语文')
INSERT [Course] ([c_no],[c_name]) VALUES ( N'C002',N'数学')
INSERT [Course] ([c_no],[c_name]) VALUES ( N'C003',N'英语')
INSERT [Course] ([c_no],[c_name]) VALUES ( N'C004',N'化学')
INSERT [Course] ([c_no],[c_name]) VALUES ( N'C005',N'物理')
if exists (select * from sysobjects where id = OBJECT_ID('[Score]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [Score]

CREATE TABLE [Score] (
[s_s_no] [varchar] (50) NOT NULL,
[s_c_no] [varchar] (50) NOT NULL,
[s_score] [int] NOT NULL)

INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120001',N'C001',19)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120001',N'C002',72)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120001',N'C003',13)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120001',N'C004',78)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120001',N'C005',52)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120002',N'C001',67)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120002',N'C002',26)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120002',N'C003',88)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120002',N'C004',36)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120002',N'C005',56)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120003',N'C001',70)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120003',N'C002',71)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120003',N'C003',8)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120003',N'C004',4)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120003',N'C005',11)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120004',N'C001',4)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120004',N'C002',73)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120004',N'C003',9)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120004',N'C004',45)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120004',N'C005',66)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120005',N'C002',82)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120005',N'C003',97)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120005',N'C004',100)
INSERT [Score] ([s_s_no],[s_c_no],[s_score]) VALUES ( N'S120005',N'C005',98)
if exists (select * from sysobjects where id = OBJECT_ID('[Student]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [Student]

CREATE TABLE [Student] (
[s_no] [varchar] (50) NOT NULL,
[s_name] [varchar] (50) NOT NULL)

INSERT [Student] ([s_no],[s_name]) VALUES ( N'S120001',N'张三')
INSERT [Student] ([s_no],[s_name]) VALUES ( N'S120002',N'李四')
INSERT [Student] ([s_no],[s_name]) VALUES ( N'S120003',N'王五')
INSERT [Student] ([s_no],[s_name]) VALUES ( N'S120004',N'陆六')
INSERT [Student] ([s_no],[s_name]) VALUES ( N'S120005',N'马七')
if exists (select * from sysobjects where id = OBJECT_ID('[Teacher]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [Teacher]

CREATE TABLE [Teacher] (
[t_no] [varchar] (50) NOT NULL,
[t_name] [varchar] (50) NOT NULL,
[t_c_no] [varchar] (50) NOT NULL)

INSERT [Teacher] ([t_no],[t_name],[t_c_no]) VALUES ( N'T001',N'杨老师',N'C002')
INSERT [Teacher] ([t_no],[t_name],[t_c_no]) VALUES ( N'T002',N'周老师',N'C001')
INSERT [Teacher] ([t_no],[t_name],[t_c_no]) VALUES ( N'T003',N'王老师',N'C005')
INSERT [Teacher] ([t_no],[t_name],[t_c_no]) VALUES ( N'T004',N'李老师',N'C004')
INSERT [Teacher] ([t_no],[t_name],[t_c_no]) VALUES ( N'T005',N'蒋老师',N'C003')

 

查询练习:

/*1.删除学生表完全重复数据(保留一条)*/
select distinct * into #Tmp from Student /*新建表并存入查询出的数据*/
delete Student
insert into Student(s_no,s_name) (select * from #Tmp)
/*另一种写法:insert into Student select s_no,s_name from #Tmp*/
drop table #Tmp

/*2.查询各科成绩最低、最高的记录(课程编号、课程名称、教师编号、教师名称、最高分、学生、学号、最低分、学生、学号)*/
select c_no,c_name,t_no,t_name,
s1.s_s_no,(select top 1 s_name from Student where s_no = s1.s_s_no) s1_name,max_score,
s2.s_s_no,(select top 1 s_name from Student where s_no = s2.s_s_no) s2_name,min_score
from(
select c_no,c_name,t_no,t_name,max(s_score) max_score,min(s_score) min_score
from Score inner join Course on s_c_no = c_no
inner join Teacher on c_no = t_c_no
group by c_no,c_name,t_no,t_name
)t
inner join Score s1 on s1.s_c_no = t.c_no and s1.s_score = t.max_score
inner join Score s2 on s2.s_c_no = t.c_no and s2.s_score = t.min_score

/*3.打印所有学生课程成绩,成绩从高到低排序(学号、学生、平均分、语文、数学...)*/
select s_no,s_name,avg(s_score) avg_score,
sum(case c_name when '语文' then s_score else 0 end) 语文,
sum(case c_name when '数学' then s_score else 0 end) 数学,
sum(case c_name when '英语' then s_score else 0 end) 英语,
sum(case c_name when '化学' then s_score else 0 end) 化学,
sum(case c_name when '物理' then s_score else 0 end) 物理
from Score inner join Course on s_c_no = c_no
inner join Student on s_s_no = s_no
group by s_no,s_name

/*4.查询数学成绩第三名到第五名成绩单(学号、学生、平均成绩、语文、数学...)*/
select *
from(
select row_number() over(order by sum(case c_name when '数学' then s_score else 0 end)) id,
s_no,s_name,avg(s_score) avg_score,
sum(case c_name when '语文' then s_score else 0 end) 语文,
sum(case c_name when '数学' then s_score else 0 end) 数学,
sum(case c_name when '英语' then s_score else 0 end) 英语,
sum(case c_name when '化学' then s_score else 0 end) 化学,
sum(case c_name when '物理' then s_score else 0 end) 物理
from Score inner join Course on s_c_no = c_no
inner join Student on s_s_no = s_no
group by s_no,s_name
)t
where id between 3 and 5

/*5.查询各科的平均成绩和及格率(课程编号、课程名称、平均分、及格率)*/
select c_no,c_name,avg(s_score) avg_score,
cast(cast((sum(case when s_score>=60 then 1 else 0 end)*100/count(c_no)) as decimal(18,2)) as varchar(20)) + '%' perc
from Score inner join Course on s_c_no = c_no
group by c_no,c_name
order by avg(s_score) desc

/*6.一条记录显示各科平均成绩和及格率(语文平均成绩、语文及格率、数学平均成绩、数学及格率...)*/
select sum(case c_name when '语文' then s_score else 0 end)/sum(case c_name when '语文' then 1 else 0 end) 语文平均分,
cast(cast(sum(case c_name when '语文' then (case when s_score >=60 then 1 else 0 end) else 0 end)*100/sum(case c_name when '语文' then 1 else 0 end)as decimal(18,2))as varchar(20))+ '%' 语文及格比,
sum(case c_name when '数学' then s_score else 0 end)/sum(case c_name when '数学' then 1 else 0 end) 数学平均分,
cast(cast(sum(case c_name when '数学' then (case when s_score >=60 then 1 else 0 end) else 0 end)*100/sum(case c_name when '数学' then 1 else 0 end)as decimal(18,2))as varchar(20))+ '%' 数学及格比,
sum(case c_name when '英语' then s_score else 0 end)/sum(case c_name when '英语' then 1 else 0 end) 英语平均分,
cast(cast(sum(case c_name when '英语' then (case when s_score >=60 then 1 else 0 end) else 0 end)*100/sum(case c_name when '英语' then 1 else 0 end)as decimal(18,2))as varchar(20))+ '%' 英语及格比,
sum(case c_name when '化学' then s_score else 0 end)/sum(case c_name when '化学' then 1 else 0 end) 化学平均分,
cast(cast(sum(case c_name when '化学' then (case when s_score >=60 then 1 else 0 end) else 0 end)*100/sum(case c_name when '化学' then 1 else 0 end)as decimal(18,2))as varchar(20))+ '%' 化学及格比,
sum(case c_name when '物理' then s_score else 0 end)/sum(case c_name when '物理' then 1 else 0 end) 物理平均分,
cast(cast(sum(case c_name when '物理' then (case when s_score >=60 then 1 else 0 end) else 0 end)*100/sum(case c_name when '物理' then 1 else 0 end)as decimal(18,2))as varchar(20))+ '%' 物理及格比
from Score inner join Course on s_c_no = c_no

/*7.查询各科平均分(去掉一个最高分和一个最低分,计算平均分)*/
select t_no,t_name,c_no,c_name,(sum(s_score)-max(s_score)-min(s_score))/(count(s_s_no)-2) avg_score
from Score inner join Course on s_c_no = c_no
inner join Teacher on c_no = t_c_no
group by t_no,t_name,c_no,c_name

/*8.查询各科各分数段的人数(课程编号、课程名称、[100-85][85-70][70-60][不及格][及格][合计])*/
select c_no,c_name,
sum(case when s_score>=85 then 1 else 0 end) '100-85',
sum(case when (s_score>=70 and s_score<85) then 1 else 0 end) '85-70',
sum(case when (s_score>=60 and s_score<70) then 1 else 0 end) '70-60',
sum(case when s_score<60 then 1 else 0 end) '不及格',
sum(case when s_score>=60 then 1 else 0 end) '及格',
count(s_s_no) '合计'
from Score inner join Course on s_c_no = c_no
group by c_no,c_name

/*9.查询学生平均成绩及名次,考虑并列情况(学号、学生、平均成绩、名次)*/
select s_no,s_name,avg(s_score) avg_score,
(select id
from(
select distinct row_number() over(order by avg(s_score)) id,avg(s_score) avg_score
from Score
group by s_s_no
)t
where t.avg_score = avg(s_score)) 名次
from Score inner join Student on s_s_no = s_no
group by s_no,s_name
order by avg_score

/*10.查询各科成绩前三名(学号、学生、课程编号、课程名称、教师编号、教师姓名、成绩)*/
select *
from(
select row_number() over(partition by c_no order by s_score) id,s_no,s_name,c_no,c_name,s_score,t_no,t_name
from Score inner join Course on s_c_no = c_no
inner join Teacher on c_no = t_c_no
inner join Student on s_s_no = s_no
)t
where id <= 3
order by c_no,s_score

/*11.查询参加所有考试科目的学生(学号、学生姓名)*/
select s_no,s_name
from Score inner join Student on s_s_no = s_no
group by s_no,s_name having count(s_c_no) = 5
order by s_no

/*12.查询参加了“王五”考试科目的学生(学号、学生)*/
select distinct s_no,s_name
from Score inner join Student on s_s_no = s_no
where s_c_no in (select s_c_no from Score inner join Student on s_s_no = s_no where s_name = '王五')

 

 

你可能感兴趣的:(sql server 2005)