下面这个是题目所用到的数据库!
首先你需要在你的SQL Sever数据库中创建[TestDb]这个数据库,接下来下面这个代码。直接复制在数据库里运行就好了!
1 USE [TestDb] 2 GO 3 /****** Object: Table [dbo].[Course] Script Date: 2018/4/28 17:36:10 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 SET ANSI_PADDING ON 9 GO 10 CREATE TABLE [dbo].[Course]( 11 [Cid] [varchar](10) NULL, 12 [Cname] [nvarchar](10) NULL, 13 [Tid] [varchar](10) NULL 14 ) ON [PRIMARY] 15 16 GO 17 SET ANSI_PADDING OFF 18 GO 19 /****** Object: Table [dbo].[SC] Script Date: 2018/4/28 17:36:10 ******/ 20 SET ANSI_NULLS ON 21 GO 22 SET QUOTED_IDENTIFIER ON 23 GO 24 SET ANSI_PADDING ON 25 GO 26 CREATE TABLE [dbo].[SC]( 27 [Sid] [varchar](10) NULL, 28 [Cid] [varchar](10) NULL, 29 [score] [decimal](18, 1) NULL 30 ) ON [PRIMARY] 31 32 GO 33 SET ANSI_PADDING OFF 34 GO 35 /****** Object: Table [dbo].[Student] Script Date: 2018/4/28 17:36:10 ******/ 36 SET ANSI_NULLS ON 37 GO 38 SET QUOTED_IDENTIFIER ON 39 GO 40 SET ANSI_PADDING ON 41 GO 42 CREATE TABLE [dbo].[Student]( 43 [Sid] [varchar](10) NULL, 44 [Sname] [nvarchar](10) NULL, 45 [Sage] [datetime] NULL, 46 [Ssex] [nvarchar](10) NULL 47 ) ON [PRIMARY] 48 49 GO 50 SET ANSI_PADDING OFF 51 GO 52 /****** Object: Table [dbo].[Teacher] Script Date: 2018/4/28 17:36:10 ******/ 53 SET ANSI_NULLS ON 54 GO 55 SET QUOTED_IDENTIFIER ON 56 GO 57 SET ANSI_PADDING ON 58 GO 59 CREATE TABLE [dbo].[Teacher]( 60 [Tid] [varchar](10) NULL, 61 [Tname] [nvarchar](10) NULL 62 ) ON [PRIMARY] 63 64 GO 65 SET ANSI_PADDING OFF 66 GO 67 INSERT [dbo].[Course] ([Cid], [Cname], [Tid]) VALUES (N‘01‘, N‘语文‘, N‘02‘) 68 INSERT [dbo].[Course] ([Cid], [Cname], [Tid]) VALUES (N‘02‘, N‘数学‘, N‘01‘) 69 INSERT [dbo].[Course] ([Cid], [Cname], [Tid]) VALUES (N‘03‘, N‘英语‘, N‘03‘) 70 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘01‘, N‘01‘, CAST(80.0 AS Decimal(18, 1))) 71 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘01‘, N‘02‘, CAST(90.0 AS Decimal(18, 1))) 72 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘01‘, N‘03‘, CAST(99.0 AS Decimal(18, 1))) 73 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘02‘, N‘01‘, CAST(70.0 AS Decimal(18, 1))) 74 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘02‘, N‘02‘, CAST(60.0 AS Decimal(18, 1))) 75 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘02‘, N‘03‘, CAST(80.0 AS Decimal(18, 1))) 76 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘03‘, N‘01‘, CAST(80.0 AS Decimal(18, 1))) 77 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘03‘, N‘02‘, CAST(80.0 AS Decimal(18, 1))) 78 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘03‘, N‘03‘, CAST(80.0 AS Decimal(18, 1))) 79 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘04‘, N‘01‘, CAST(50.0 AS Decimal(18, 1))) 80 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘04‘, N‘02‘, CAST(30.0 AS Decimal(18, 1))) 81 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘04‘, N‘03‘, CAST(20.0 AS Decimal(18, 1))) 82 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘05‘, N‘01‘, CAST(76.0 AS Decimal(18, 1))) 83 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘05‘, N‘02‘, CAST(87.0 AS Decimal(18, 1))) 84 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘06‘, N‘01‘, CAST(31.0 AS Decimal(18, 1))) 85 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘06‘, N‘03‘, CAST(34.0 AS Decimal(18, 1))) 86 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘07‘, N‘02‘, CAST(89.0 AS Decimal(18, 1))) 87 INSERT [dbo].[SC] ([Sid], [Cid], [score]) VALUES (N‘07‘, N‘03‘, CAST(98.0 AS Decimal(18, 1))) 88 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘01‘, N‘赵雷‘, CAST(0x0000806800000000 AS DateTime), N‘男‘) 89 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘02‘, N‘钱电‘, CAST(0x000081CA00000000 AS DateTime), N‘男‘) 90 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘03‘, N‘孙风‘, CAST(0x000080F300000000 AS DateTime), N‘男‘) 91 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘04‘, N‘李云‘, CAST(0x0000814100000000 AS DateTime), N‘男‘) 92 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘05‘, N‘周梅‘, CAST(0x0000832300000000 AS DateTime), N‘女‘) 93 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘06‘, N‘吴兰‘, CAST(0x0000837E00000000 AS DateTime), N‘女‘) 94 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘07‘, N‘郑竹‘, CAST(0x00007FB000000000 AS DateTime), N‘女‘) 95 INSERT [dbo].[Student] ([Sid], [Sname], [Sage], [Ssex]) VALUES (N‘08‘, N‘王菊‘, CAST(0x0000807B00000000 AS DateTime), N‘女‘) 96 INSERT [dbo].[Teacher] ([Tid], [Tname]) VALUES (N‘01‘, N‘张三‘) 97 INSERT [dbo].[Teacher] ([Tid], [Tname]) VALUES (N‘02‘, N‘李四‘) 98 INSERT [dbo].[Teacher] ([Tid], [Tname]) VALUES (N‘03‘, N‘王五‘)
创建数据库和数据
下面这个就是题目于我说写的答案了!
1 --1 查询"01"课程比"02"课程成绩高的学生的信息及课程分数 2 create proc PB_problem_one 3 as 4 begin 5 select s.*, scb.Cid, scb.score,scc.Cid, scc.score as 成绩 from student as s 6 left join (select * from SC where cid=‘01‘) scb on s.Sid=scb.Sid 7 left join (select * from SC where cid=‘02‘) scc on scc.sid=scb.sid 8 where scc.score<scb.score 9 end 10 11 --1.1 查询同时存在"01"课程和"02"课程的情况 12 create proc PB_problem_one_one 13 as 14 begin 15 select s.* ,scb.Cid,scb.score,scc.Cid,scc.score from Student s,SC scb,SC scc 16 where s.Sid=scb.Sid and s.Sid=scc.Sid and scb.Cid=‘01‘ and scc.Cid=‘02‘ 17 end 18 19 --1.2 查询同时存在"01"课程和"02"课程的情况和存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释) 20 create proc PB_problem_one_two 21 as 22 begin 23 select s.*,scb.Cid,scb.score,scc.Cid,scc.score from Student s 24 left join SC scb on s.Sid=scb.Sid and scb.Cid=‘01‘ 25 left join SC scc on s.Sid=scc.Sid and scc.Cid=‘02‘ 26 where scb.score>ISNULL(scc.score,0) 27 end 28 29 --2 查询"01"课程比"02"课程成绩低的学生的信息及课程分数 30 create proc PB_problem_two 31 as 32 begin 33 select s.*, scb.Cid, scb.score,scc.Cid,scc.score as 成绩 from student as s 34 left join (select * from SC where cid=‘01‘) scb on s.Sid=scb.Sid 35 left join (select * from SC where cid=‘02‘) scc on scc.sid=scb.sid 36 where scc.score>scb.score 37 end 38 39 --2.1 这个和1.1的题目相同 查询同时存在"01"课程和"02"课程的情况 40 create proc PB_problem_two_one 41 as 42 begin 43 select s.* , scb.Cid,scb.score,scc.Cid,scc.score from Student s,SC scb,SC scc 44 where s.Sid=scb.Sid and s.Sid=scc.Sid and scb.Cid=‘01‘ and scc.Cid=‘02‘ 45 end 46 47 --2.2 查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况 48 create proc PB_problem_two_two 49 as 50 begin 51 select * from Student s 52 left join SC scb on s.Sid=scb.Sid and scb.Cid=‘01‘ 53 left join SC scc on s.Sid=scc.Sid and scc.Cid=‘02‘ 54 where ISNULL(scb.score,0) < scc.score 55 end 56 57 --3 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩 58 create proc PB_problem_three 59 as 60 begin 61 select s.Sname,s.Sid ,CAST(avg(scb.score) as int) as ‘平均成绩‘ from Student s,SC scb where s.Sid=scb.Sid 62 group by s.Sname,s.Sid 63 having Cast(avg(scb.score) as int) >= 60 64 end 65 66 --4 查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩 67 create proc PB_problem_four 68 as 69 begin 70 select s.Sname,s.Sid ,CAST(avg(scb.score) as int) as ‘平均成绩‘ from Student s,SC scb where s.Sid=scb.Sid 71 group by s.Sname,s.Sid 72 having Cast(avg(scb.score) as int) <= 60 73 end 74 75 --4.1 查询在sc表存在成绩的学生信息的SQL语句。‘ 76 create proc PB_problem_four_one 77 as 78 begin 79 select DISTINCT s.* from Student s,SC sc where s.Sid=sc.Sid 80 end 81 82 --4.2 查询在sc表中不存在成绩的学生信息的SQL语句。 83 create proc PB_problem_four_two 84 as 85 begin 86 select s.*,sc.score from Student s 87 left join SC sc on s.Sid=sc.Sid 88 where sc.score is null 89 end 90 91 --5 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩 92 create proc PB_problem_fives 93 as 94 begin 95 select s.Sid,s.Sname,COUNT(scb.Sid) as ‘选课总数‘,CAST(SUM(scb.score) as int) as ‘课程总成绩‘ from Student s 96 left join SC scb on s.Sid=scb.Sid 97 group by s.Sid,s.Sname 98 order by SUM(scb.score) desc 99 end 100 101 --5.1 查询所有有成绩的SQL。 102 create proc PB_problem_fives_one 103 as 104 begin 105 select DISTINCT s.* from Student s,SC sc where s.Sid=sc.Sid 106 end 107 108 --5.2 查询所有(包括有成绩和无成绩)的SQL。 109 create proc PB_problem_fives_two 110 as 111 begin 112 select distinct s.* from Student s left join SC sc on s.Sid=sc.Sid 113 end 114 115 --6 查询"李"姓老师的数量 116 create proc PB_problem_six_function_one 117 as 118 begin 119 select COUNT(Tname) as ‘姓李老师的数量‘ from Teacher where Tname like ‘李%‘ 120 end 121 122 create proc PB_problem_six_function_two 123 as 124 begin 125 select COUNT(Tname) as ‘姓李老师的数量‘ from Teacher where Tname like ‘%李%‘ 126 end 127 128 --7 查询学过"张三"老师授课的同学的信息 129 create proc PB_problem_seven 130 as 131 begin 132 select st.* from Course c 133 left join Teacher t on c.Tid=t.Tid 134 left join SC s on s.Cid=c.Cid 135 left join Student st on st.Sid=s.Sid 136 where t.Tname=‘张三‘ 137 end 138 139 --8 查询没学过"张三"老师授课的同学的信息 140 create proc PB_problem_eight 141 as 142 begin 143 --先查询出学过张三老师课程的学号,然后使用Not in这个方法去查出没学过张三老师课程的学生 144 select * from Student s where s.Sid Not in (select s.Sid from SC s 145 left join Course c on s.Cid=c.Cid 146 left join Teacher t on c.Tid=t.Tid where t.Tname=‘张三‘) 147 end 148 149 --9 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息 150 create proc PB_problem_nine 151 as 152 begin 153 select s.*,scb.Cid,scb.score,scc.Cid,scc.score from Student s,SC scb,SC scc 154 where s.Sid=scb.Sid and s.Sid=scc.Sid and scb.Cid=‘01‘ and scc.Cid=‘02‘ 155 end 156 157 --10 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息 158 create proc PB_problem_ten 159 as 160 begin 161 select * from Student s, SC sc 162 where s.Sid=sc.Sid and sc.Cid=‘01‘ and not exists (select 1 from sc scc where scc.Sid=sc.Sid and scc.Cid=‘02‘) 163 order by s.Sid 164 end 165 166 --11 查询没有学全所有课程的同学的信息 167 create proc PB_problem_eleven 168 as 169 begin 170 select * from Student where Student.Sid not in( 171 --先查询出学完全部课程的学生的ID,然后再去查询学生表条件是不等于这些查询出来的ID 172 select s.Sid from Student s 173 left join SC sc on s.Sid=sc.Sid 174 group by s.Sid 175 having COUNT(sc.Cid) =(select COUNT(Course.Cname) from Course) 176 ) 177 end 178 179 --12 查询至少有一门课与学号为"01"的同学所学相同的同学的信息 180 create proc PB_problem_twelve 181 as 182 begin 183 select * from Student s,SC sc 184 where s.Sid=sc.Sid and sc.Cid in(select Sid from SC where Sid=‘01‘) and s.Sid <> ‘01‘ 185 end 186 187 --13 查询和"01"号的同学学习的课程完全相同的其他同学的信息 188 create proc PB_problem_thirteen 189 as 190 begin 191 select Student.* from Student where Sid in 192 (select distinct SC.Sid from SC where Sid <> ‘01‘ and SC.Cid in (select distinct Cid from SC where Sid = ‘01‘) 193 group by SC.Sid having count(1) = (select count(1) from SC where Sid=‘01‘)) 194 end 195 196 --14 查询没学过"张三"老师讲授的任一门课程的学生姓名 197 create proc PB_problem_fourteen 198 as 199 begin 200 select s.Sname from Student s where s.Sid Not in (select s.Sid from SC s 201 left join Course c on s.Cid=c.Cid 202 left join Teacher t on c.Tid=t.Tid where t.Tname=‘张三‘) 203 end 204 205 --15 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 206 create proc PB_problem_fifteen 207 as 208 begin 209 select s.Sid,s.Sname,CAST(AVG(sc.score) AS decimal(18,2)) as ‘平均成绩‘ from Student s,SC sc 210 where s.Sid=sc.Sid and s.Sid in (select sc.Sid from SC where sc.score<60 group by sc.Sid having COUNT(1)>=2) 211 group by s.Sid,s.Sname 212 end 213 214 --16 检索"01"课程分数小于60,按分数降序排列的学生信息 215 create proc PB_problem_sixteen 216 as 217 begin 218 select * from Student s 219 where s.Sid in (select SC.Sid from SC where SC.Cid=‘01‘ and SC.score<60) 220 order by s.Sid asc 221 end 222 223 --17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 静态sql 224 create proc PB_problem_seventeen 225 as 226 begin 227 select s.Sid as ‘学生编号‘,s.Sname as ‘学生姓名‘, 228 MAX(case c.Cname when ‘语文‘ then sc.score else null end)[语文], 229 MAX(case c.Cname when ‘数学‘ then sc.score else null end)[数学], 230 MAX(case c.Cname when ‘英语‘ then sc.score else null end)[英语], 231 CAST(AVG(sc.score) as int) as ‘平均成绩‘ from Student s 232 left join SC sc on s.Sid=sc.Sid 233 left join Course c on sc.Cid=c.Cid 234 group by s.Sid,s.Sname 235 order by CAST(AVG(sc.score) as int) desc 236 end 237 238 --18 查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 239 --及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 240 create proc PB_problem_eighteen 241 as 242 begin 243 select sc.Cid as ‘课程ID‘,c.Cname as ‘课程名称‘, 244 max(SC.score) as 最高分,min(sc.score) as 最低分,CAST(avg(sc.score)as decimal(18,2)) as 平均分, 245 --count(Student.Sid) 246 --先查询每个课程及格的人数,然后再查询选修这个课程的总人数。然后及格的人数除于课程总人数,计算时需要让及格人数乘于 1.00 然后再把最后结果转为decimal(18,2) 不然计算的结果会出现很多的小数位 247 CAST(count(case when sc.score>=60 then sc.score else null end) * 1.00 / count(sc.Cid) as decimal(18,2)) as 积分率 , 248 CAST(count(case when sc.score>=70 and sc.score<80 then sc.score else null end) * 1.00 / count(sc.Cid) as decimal(18,2)) as 中等, 249 CAST(count(case when sc.score>=80 and sc.score<90 then sc.score else null end) * 1.00 / count(sc.Cid)as decimal(18,2)) as 优良, 250 CAST(count(case when sc.score>=90 then sc.score else null end) * 1.00 / count(sc.Cid)as decimal(18,2)) as 优秀 251 from SC sc 252 left join Course c on sc.Cid=c.Cid 253 left join Student s on sc.Sid=s.Sid 254 group by SC.Cid,c.cname 255 end 256 257 --19 按各科成绩进行排序,并显示排名 思路:利用over(partition by 字段名 order by 字段名)函数。 正常排序:1,2,3 258 create proc PB_problem_nineteen 259 as 260 begin 261 --ROW_NUMBER()先用这个函数给每一个行创建一个单独的列id,然后再用over去排序 262 select c.Cname as ‘科目‘,SC.score as ‘成绩‘,ROW_NUMBER() over(partition by c.Cname order by sc.score desc) as ‘各科排名‘ from SC 263 left join Course c on SC.Cid=c.Cid 264 end 265 266 --20 查询学生的总成绩并进行排名 思路:所有学生的总成绩(一个记录集合),再使用函数进行排序。 267 create proc PB_problem_twenty 268 as 269 begin 270 select s.Sname,sum(sc.score) as ‘总成绩‘, ROW_NUMBER() over(order by sum(sc.score) desc) as ‘人员名次‘ from Student s 271 left join SC sc on s.Sid=sc.Sid 272 group by s.Sname 273 end 274 275 -- 20.3 查询学生的总成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分总分重复时保留名次空缺和不保留名次空缺两种。 276 create proc PB_problem_twenty_three 277 as 278 begin 279 select s.Sname,sum(sc.score) as 总成绩,ROW_NUMBER() over(order by sum(sc.score) desc) as ‘人员名次‘ from Student s 280 left join SC sc on s.Sid=sc.Sid 281 group by s.Sname 282 having sum(sc.score) is not null 283 end 284 285 --21 查询不同老师所教不同课程平均分从高到低显示 思路:不同老师所教不同课程的平均分(一个记录集合),再使用函数over(order by 字段名) 286 create proc PB_problem_twentyone 287 as 288 begin 289 select t.Tname,c.Cname,CAST(AVG(s.score) as decimal(18,2)) as 平均分,ROW_NUMBER() over(order by AVG(s.score) desc) as ‘排名‘ from Teacher t 290 left join Course c on t.Tid=c.Tid 291 left join SC s on c.Cid=s.Cid 292 group by t.Tname,c.Cname 293 end 294 295 --22查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 296 --思路:所有课程成绩的学生及课程信息(一个记录集合),再利用函数排序(一个记录集合),选择第2名和第3名的记录。 297 create proc PB_problem_twentytwo 298 as 299 begin 300 select * from (select c.Cname,sc.score,ROW_NUMBER() over(partition by c.Cname order by sc.score desc) as 排名 from SC sc 301 left join Course c on sc.Cid=c.Cid)as TT 302 where TT.排名 between 2 and 3 303 end 304 305 --23 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比 306 create proc PB_problem_twentythree 307 as 308 begin 309 select c.Cid,c.Cname, 310 CAST(count(case when sc.score>0 and sc.score<=60 then sc.score else null end) * 1.00 / count(c.Cid) as decimal(18,2)) as [0-60], 311 CAST(count(case when sc.score>60 and sc.score<=70 then sc.score else null end) * 1.00 / count(c.Cid) as decimal(18,2)) as [60-70], 312 CAST(count(case when sc.score>70 and sc.score<=85 then sc.score else null end) * 1.00 / count(c.Cid) as decimal(18,2)) as [70-85], 313 CAST(count(case when sc.score>85 and sc.score<=100 then sc.score else null end) * 1.00 / count(c.Cid) as decimal(18,2)) as [85-100] 314 from Course c 315 inner join SC sc on c.Cid=sc.Cid 316 group by c.Cid,c.Cname 317 end 318 319 320 --24 查询学生平均成绩及其名次 321 create proc PB_problem_twentyfour 322 as 323 begin 324 select s.Sname, CAST(AVG(sc.score) as decimal(18,2)) as 平均成绩 ,ROW_NUMBER() over(order by CAST(AVG(sc.score) as decimal(18,2)) desc) as 名次 from Student s 325 inner join SC sc on s.Sid=sc.Sid 326 group by s.Sname 327 end 328 329 --25 查询各科成绩前三名的记录 330 create proc PB_problem_twentyfives 331 as 332 begin 333 select * from (select c.Cname,sc.score,ROW_NUMBER() over(partition by c.Cname order by sc.score desc) as 排名 from SC sc 334 inner join Course c on sc.Cid=c.Cid 335 group by c.Cname,sc.score) as TT 336 where TT.排名 between 1 and 3 337 end 338 339 --26 查询每门课程被选修的学生数 340 create proc PB_problem_twentysix 341 as 342 begin 343 select c.Cname,COUNT(sc.Cid) as 选修总数 from SC sc 344 inner join Course c on sc.Cid=c.Cid 345 group by c.Cname 346 end 347 348 --27 查询出只有两门课程的全部学生的学号和姓名 349 create proc PB_problem_twentyseven 350 as 351 begin 352 select s.Sid,s.Sname,COUNT(sc.Cid) as 选课数量 from Student s 353 inner join SC sc on s.Sid=sc.Sid 354 group by s.Sid,s.Sname 355 having COUNT(sc.Cid)=2 356 end 357 358 --28 查询男生、女生人数 359 create proc PB_problem_twentyeight 360 as 361 begin 362 --select COUNT(s.Ssex) as 男生人数 from Student s 363 --where s.Ssex=‘男‘ 364 --select COUNT(s.Ssex) as 女生人数 from Student s 365 --where s.Ssex=‘女‘ 366 367 select sum(case when s.Ssex=‘男‘ then 1 else 0 end) as 男生人数,sum(case when s.Ssex=‘女‘ then 1 else 0 end) as 女生人数 from Student s 368 end 369 370 371 --29 查询名字中含有"风"字的学生信息 372 create proc PB_problem_twentynine 373 as 374 begin 375 select * from Student where Student.Sname like ‘%风%‘ 376 end 377 378 --30 查询同名同性学生名单,并统计同名人数 379 create proc PB_problem_thirty 380 as 381 begin 382 select s.Sname,count(s.Sid) as 数量 383 from student s 384 group by s.Sname 385 having count(s.Sid)>1 386 --下面这个更新是为了测试上面的查询语句是否正确 387 --update Student 388 --set Sname=‘钱电‘ 389 --where Sid=‘02‘ 390 end 391 392 --31 查询1990年出生的学生名单 393 create proc PB_problem_thirtyone 394 @year varchar(10) 395 as 396 begin 397 select * from Student s 398 where year(s.Sage)=@year 399 --下面这个函数是可以获取到日期 400 --CONVERT(VARCHAR(10),QueueDate,23) 401 end 402 403 --32 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 404 create proc PB_problem_thirtytwo 405 as 406 begin 407 select c.Cname,CAST(AVG(sc.score) as decimal(18,2)) as 平均分,ROW_NUMBER() over(order by AVG(sc.score) desc) as 排名 from SC sc 408 inner join Course c on sc.Cid=c.Cid 409 group by c.Cname 410 end 411 412 --33 查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 413 create proc PB_problem_thirtythree 414 as 415 begin 416 select s.Sid,s.Sname,CAST(AVG(sc.score) as decimal(18,2)) as 平均成绩 from Student s 417 inner join SC sc on s.Sid=sc.Sid 418 group by s.Sid,s.Sname 419 having CAST(AVG(sc.score) as decimal(18,2))>85 420 order by AVG(sc.score) desc 421 end 422 423 --34 查询课程名称为"数学",且分数低于60的学生姓名和分数 424 create proc PB_problem_thirtyfour 425 as 426 begin 427 select s.Sname 姓名,sc.score 分数,c.Cname 学科 from student s 428 left join SC sc on s.Sid=sc.Sid 429 left join Course c on sc.Cid=c.Cid 430 group by s.Sname,sc.score,c.Cname 431 having sc.score<60 and c.Cname=‘数学‘ 432 end 433 434 --35 查询所有学生的课程及分数情况; 435 create proc PB_problem_thirtyfive 436 as 437 begin 438 select s.Sname,c.Cname,sc.score,ROW_NUMBER() over(partition by s.Sname order by sc.score desc) from Student s 439 left join SC sc on s.Sid=sc.Sid 440 left join Course c on sc.Cid=c.Cid 441 end 442 443 --36 查询任何一门课程成绩在70分以上的姓名、课程名称和分数 444 create proc PB_problem_thirtysix 445 as 446 begin 447 select s.Sname,c.Cname,sc.score from Student s 448 left join SC sc on s.Sid=sc.Sid 449 left join Course c on sc.Cid=c.Cid 450 where sc.score>70 451 order by sc.score desc 452 --下面这个查的是视图 453 --select * from View_Scores v 454 --where v.score>70 455 end 456 457 --37 查询不及格的课程 458 create proc PB_problem_thirtyseven 459 as 460 begin 461 select s.*,c.Cname,sc.score from Student s 462 left join SC sc on s.Sid=sc.Sid 463 left join Course c on sc.Cid=c.Cid 464 where sc.score<60 465 466 --select * from View_Scores v 467 --where v.score<60 468 end 469 470 --38 查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 471 create proc PB_problem_thirtyeight 472 as 473 begin 474 select s.Sid,s.Sname,c.Cname,sc.score from Student s 475 left join SC sc on s.Sid=sc.Sid 476 left join Course c on sc.Cid=c.Cid 477 where c.Cid=‘01‘ and sc.score>=80 478 479 end 480 481 --39 求每门课程的学生人数 482 create proc PB_problem_thirtynine 483 as 484 begin 485 select c.Cname ,COUNT(sc.Cid) 学生人数 from SC sc 486 left join Course c on sc.Cid=c.Cid 487 group by c.Cname 488 end 489 490 --40 查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩 491 create proc PB_problem_forty 492 as 493 begin 494 select s.Sid,s.Sname,s.Sage,s.Ssex,MAX(sc.score) 最高分 from student s 495 left join SC sc on s.Sid=sc.Sid 496 left join Course c on sc.Cid=c.Cid 497 left join Teacher t on c.Tid=t.Tid 498 group by s.Sid,s.Sname,s.Sage,s.Ssex,t.Tname 499 having t.Tname=‘张三‘ 500 501 --select Sid,Sname,Sage,Ssex,MAX(score) 最高分 from View_Scores v 502 --group by Sid,Sname,Sage,Ssex,Tname 503 --having v.Tname=‘张三‘ 504 end 505 506 --40.2 当最高分出现多个时 507 create proc PB_problem_fortytwo 508 as 509 begin 510 select s.Sid,s.Sname,s.Sage,s.Ssex,MAX(sc.score) 最高分 from Student s 511 left join SC sc on s.Sid=sc.Sid 512 left join Course c on sc.Cid=c.Cid 513 left join Teacher t on c.Tid=t.Tid 514 group by s.Sid,s.Sname,s.Sage,s.Ssex,t.Tname,sc.score 515 having t.Tname=‘张三‘ and sc.score in (select MAX(sc.score) from Student s,SC sc,Course c,Teacher t where s.Sid=sc.Sid and sc.Cid=c.Cid and c.Tid=t.Tid and t.Tname=‘张三‘) 516 --sc.score 后面 in的分数可以用下面这个查视图的语句 517 --select MAX(v.score) from View_Scores v where v.Tname=‘张三‘ 518 --用来做测试的SQL语句 519 --update SC 520 --set score=‘60‘ 521 --where Sid=‘02‘ and Cid=‘02‘ 522 --60 523 end 524 525 --41 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 526 create proc PB_problem_fortyone 527 as 528 begin 529 select a.* from SC a,(select Cid,score from SC group by Cid,score having COUNT(1) > 1) n 530 where a.Cid=n.Cid and a.score=n.score order by a.Cid,a.score,a.Sid 531 end 532 533 --42 查询每门课程成绩最好的前两名 534 create proc PB_problem_fortytwo 535 as 536 begin 537 select * from (select c.Cname,sc.score,ROW_NUMBER() over(partition by c.Cname order by sc.score desc) as 排名 from SC sc 538 inner join Course c on sc.Cid=c.Cid 539 group by c.Cname,sc.score) as TT 540 where TT.排名 between 1 and 2 541 542 543 --select * from(select v.Cname,v.score,ROW_NUMBER() over(partition by v.Cname order by v.score desc) as 排名 from View_Scores v 544 --group by v.Cname,v.score) as TT 545 --where TT.排名 between 1 and 2 and TT.score is not null 546 end 547 548 --43 统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 549 create proc PB_problem_fortythree 550 @Cid int, 551 @num int 552 as 553 begin 554 select c.Cname,COUNT(sc.score) as 选修人数 from SC sc 555 left join Course c on sc.Cid=c.Cid 556 group by c.Cname,c.Cid 557 having c.Cid=@Cid and @num>=5 558 end 559 560 --44 检索至少选修两门课程的学生学号 561 create proc PB_problem_fortyfour 562 as 563 begin 564 select s.Sid,s.Sname,s.Sage,COUNT(1) as 选课总数 from Student s 565 left join SC sc on s.Sid=sc.Sid 566 group by s.Sid,s.Sname,s.Sage 567 having COUNT(1)>=2 568 end 569 570 --45 查询选修了全部课程的学生信息 571 create proc PB_problem_fortyfives 572 as 573 begin 574 select s.*,COUNT(1) as 选课总数 from Student s 575 left join SC sc on s.Sid=sc.Sid 576 group by s.Sid,s.Sname,s.Ssex,s.Sage 577 having COUNT(1)=3 578 end 579 580 --46 查询各学生的年龄 只按照年份来算 581 create proc PB_problem_fortysix 582 @age int 583 as 584 begin 585 select s.Sname,(DATENAME(YYYY,GETDATE())-year(s.Sage)) as 年龄 from Student s 586 --获取当前年份 587 --select DATENAME(YYYY,GETDATE()) 588 end 589 590 --46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 591 create proc PB_problem_fortysixtwo 592 as 593 begin 594 select s.Sname, 595 SUBSTRING( 596 CAST( 597 CAST(CONVERT(varchar(8),GETDATE(),112) as int) - CAST(CONVERT(VARCHAR(10),s.Sage,112) as int) 598 as varchar) 599 ,0,3) as 年龄 from Student s 600 end 601 602 --47 查询本周过生日的学生 603 create proc PB_problem_fortyseven 604 as 605 begin 606 SELECT * FROM Student WHERE datediff(week,Sage,getdate())=0 607 608 --为了验证上面的查询语句是否正确,把01的出生年月日更改为当前日期 609 --update Student 610 --set Sage=CONVERT(varchar,GETDATE(),120) 611 --where Sid=‘01‘ 612 --恢复原始数据 613 --update Student 614 --set Sage=‘1990-01-01 00:00:00.000‘ 615 --where Sid=‘01‘ 616 end 617 618 --48 查询下周过生日的学生 619 create proc PB_problem_fortyeight 620 as 621 begin 622 select * from Student where DATEDIFF(WEEK,Sage,getdate())=-1 623 624 --这个是获取当前日期上一周的时间,相当于当前日期的第前七天的日期 比如当前时间是2018:04:23 15:10:53 结果为:2018-04-16 00:00:00.000 625 --select dateadd(week,-1,DATEADD(week,DATEDIFF(week,0,getdate()),0)) 626 627 --为了验证上面的查询语句是否正确,把01的出生年月日更改为当前日期 628 --update Student 629 --set Sage=‘2018-04-30 00:00:00.000‘ 630 --where Sid=‘01‘ 631 632 --恢复原始数据 633 --update Student 634 --set Sage=‘1990-01-01 00:00:00.000‘ 635 --where Sid=‘01‘ 636 637 --获取当前日期 格式:2018:04:23 15:10:53 638 --select CONVERT(varchar,GETDATE(),120) 639 end 640 641 --49 查询本月过生日的学生 642 create proc PB_problem_fortynine 643 as 644 begin 645 select * from Student s 646 where s.Sid in (select (case 647 when CAST(DATENAME(MM,GETDATE()) as int) - CAST(month(s.Sage) as int) = 0 648 then s.Sid 649 else null end) from Student s) 650 end 651 --下面这个是用来测试上面的查询本月过生日的学生的SQL语句是否正确 652 --update Student 653 --set Sage=‘1990-01-01 00:00:00.000‘ 654 --where sid=‘01‘ 655 --CONVERT(varchar,GETDATE(),120) 656 --1990-01-01 00:00:00.000 657 658 659 --50 查询下月过生日的学生 660 create proc PB_problem_fifty 661 as 662 begin 663 select * from Student s 664 where s.Sid in(select (case 665 when CAST(DATENAME(MM,GETDATE()) as int) - CAST(MONTH(s.Sage) as int) = -1 666 then s.Sid 667 else null end) from Student s) 668 end 669 670 --执行存储过程 671 exec PB_problem_fortythree @Cid=‘02‘,@num=‘8‘ 672 --删除存储过程 673 drop proc PB_problem_fortythree
题目与答案
在写这些题目的时候碰到过很多问题!这些问题在我下面这个周报里面有所详细的描述!
http://www.cnblogs.com/Scholars/articles/8892504.html
还有一些没有写在其中就是最后几题的时间获取的问题!
下面这个地址是获取
sql语句获取本周、上一周、本月数据
http://www.cnblogs.com/Scholars/p/8919600.html
下面这个地址是获取
Sql Server获取当前日期
http://www.cnblogs.com/Scholars/p/8919094.html
我写的这些随笔里有详细的描述以及使用方法!
希望能帮到大家!
原文地址:https://www.cnblogs.com/Scholars/p/8969027.html
时间: 2024-10-20 04:53:09