1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
| --1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点): select student.*,s.s_score as score1,b.s_score as score2 from student left join score s on student.s_id = s.s_id and s.c_id = '1' left join score b on student.s_id = b.s_id and b.c_id = '2' where b.s_score<s.s_score;
--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数(重点): select student.*,c.s_score as score2,s.s_score as score1 from student left join score s on student.s_id = s.s_id and s.c_id = '1' left join score c on student.s_id = c.s_id and c.c_id = '2' where c.s_score > s.s_score;
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩(重点): select stu.s_id,stu.s_name,round(avg(s.s_score),1) as avgScore from student stu left join score s on stu.s_id = s.s_id group by stu.s_id,stu.s_name having avg(s.s_score) >= 60;
set hive.strict.checks.cartesian.product = true; set hive.mapred.mode = nonstrict;
--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩: select stu.s_id,stu.s_name,round(avg(s.s_score),1) as avgScore from student stu left join score s on stu.s_id = s.s_id group by stu.s_id, stu.s_name having round(avg(s.s_score),1) < 60 union all select stu2.s_id,stu2.s_name,0 from student stu2 where stu2.s_id not in (select sc.s_id from score sc);
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩: select stu.s_id,stu.s_name,count(s.c_id),sum(s.s_score) from student stu left join score s on stu.s_id = s.s_id group by stu.s_id, stu.s_name;
--6、查询"李"姓老师的数量: select t_name,count(1) from teacher where teacher.t_name like '李%' group by t_name;
--7、查询学过"张三"老师授课的同学的信息(重点): select stu.* from student stu left join score s on stu.s_id = s.s_id left join course c on s.c_id = c.c_id left join teacher t on c.t_id = t.t_id where t.t_name = '张三';
--8、查询没学过"张三"老师授课的同学的信息(重点): select student.*,tmp.s_id from student left join (select s_id from score join course on course.c_id=score.c_id join teacher on course.t_id=teacher.t_id and t_name='张三')tmp on student.s_id =tmp.s_id where tmp.s_id is null;
--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息(重点): select stu.* from student stu join score s on stu.s_id = s.s_id and s.c_id='2' join score c on stu.s_id = c.s_id and c.c_id='1'; --10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息(重点): set hive.strict.checks.cartesian.product = true; set hive.mapred.mode = nonstrict; select stu.* from student stu join score c on stu.s_id = c.s_id and c.c_id='1' left join score s on stu.s_id = s.s_id and s.c_id='2' where s.s_id is null;
--11、查询没有学全所有课程的同学的信息(重点): select student.* from student join (Select count(c_id) num1 from course) tmp1 left join (select s_id,count(1) num2 from score group by s_id) tmp2 on tmp1.num1 = tmp2.num2 and student.s_id=tmp2.s_id where tmp2.s_id is null;
--12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息(重点): with t1 as ( select c_id from score where s_id='1' ) select stu.* from student stu join score s on stu.s_id = s.s_id join t1 on t1.c_id = s.c_id where stu.s_id <> '1' group by stu.s_id,s_name,s_sex,s_birth;
--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息(重点): with t1 as ( select c_id from score where s_id='1' ) select stu.*,count(s.c_id) from student stu join score s on stu.s_id = s.s_id join t1 on t1.c_id = s.c_id where stu.s_id <> '1' group by stu.s_id,s_name,s_sex,s_birth;
--14、查询没学过"张三"老师讲授的任一门课程的学生姓名(重点): select stu.s_name from student stu left join (select s.c_id,s_id from score s join course c on s.c_id = c.c_id join teacher t on c.t_id = t.t_id and t.t_name = '张三' ) tmp on tmp.s_id = stu.s_id where tmp.s_id is null;
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩(重点): with t1 as ( select stu.s_id,stu.s_name,count(s.s_score) as countBadScore from student stu left join score s on stu.s_id = s.s_id and s.s_score < 60 group by stu.s_id, stu.s_name ) select t1.s_id,t1.s_name,round(avg(s_score),1) from t1 left join score s on s.s_id=t1.s_id where countBadScore >= 2 group by t1.s_id,t1.s_name;
--16、检索"01"课程分数小于60,按分数降序排列的学生信息 select stu.*,tmp.scores FROM student stu join (select sid,scores from score where cid=1 and scores<60)tmp on tmp.sid=stu.id order by tmp.scores;
--17、按平均成绩从高到低显示所有学生 的所有课程的成绩以及平均成绩 select a.sid,tmp1.scores as c1,tmp2.scores as c2,tmp3.scores as c3, round(avg (a.scores),2) as avgScore from score a left join (select sid,scores from score s1 where cid='01')tmp1 on tmp1.sid=a.sid left join (select sid,scores from score s2 where cid='02')tmp2 on tmp2.sid=a.sid left join (select sid,scores from score s3 where cid='03')tmp3 on tmp3.sid=a.sid group by a.sid,tmp1.scores,tmp2.scores,tmp3.scores order by avgScore desc;
--18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
select course.cid,course.cname,tmp.maxScore,tmp.minScore,tmp.avgScore,tmp.passRate,tmp.moderate,tmp.goodRate,tmp.excellentRates from course join(select cid, max(scores) as maxScore, min(scores) as minScore, round(avg(scores),2) avgScore, round(sum(case when scores>=60 then 1 else 0 end)/count(cid),2)passRate, round(sum(case when scores>=60 and scores<70 then 1 else 0 end)/count(cid),2) moderate, round(sum(case when scores>=70 and scores<80 then 1 else 0 end)/count(cid),2) goodRate, round(sum(case when scores>=80 and scores<90 then 1 else 0 end)/count(cid),2) excellentRates from score group by cid) tmp on tmp.cid=course.cid;
--19、按各科成绩进行排序,并显示排名: – row_number() over()分组排序功能(mysql没有该方法) select cou.cname,stu.name,scores,rank() over(partition by cou.cname order by scores desc) from score left join student stu on stu.id=score.sid left join course cou on cou.cid=score.cid;
--20、查询学生的总成绩并进行排名 select stu.name,sum(scores) sum_sco,rank() over(order by sum(scores) desc) from score left join student stu on stu.id=score.sid group by stu.name;
--21、查询不同老师所教不同课程平均分从高到低显示
select teacher.tname,course.cname,round(avg(scores),2) avg_scores from score join course on score.cid=course.cid join teacher on teacher.tid=course.tid group by course.cname,teacher.tname order by avg_scores desc;
--22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩 select course.cname,stu.*,tmp.scores,tmp.cno from student stu join (select *,row_number() over (partition by cid order by scores desc)cno from score )tmp on stu.id=tmp.sid join course on course.cid=tmp.cid where tmp.cno between 2 and 3;
--23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select score.cid,course.cname, round(sum(case when score.scores>=85 and score.scores<=100 then 1 else 0 end)/count(score.scores),2) as 100and85, round(sum(case when score.scores>=70 and score.scores<85 then 1 else 0 end)/count(score.scores),2) as 85and70, round(sum(case when score.scores>=60 and score.scores<70 then 1 else 0 end)/count(score.scores),2) as 70and60, round(sum(case when score.scores>=0 and score.scores<60 then 1 else 0 end)/count(score.scores),2) as 60and0 from score left join course on score.cid=course.cid group by score.cid,course.cname;
--24、查询学生平均成绩及其名次 select student.name,nvl(round(avg(scores),2),0.0),rank() over(order by avg(scores) desc) from score right join student on student.id=score.sid group by sid,student.name;
--25、查询各科成绩前三名的记录
select course.cname,stu.*,tmp.scores,tmp.cno from student stu join(select cid,sid,scores,row_number() over(partition by cid order by scores desc) cno from score )tmp on stu.id=tmp.sid join course on course.cid=tmp.cid where cno<=3;
--26、查询每门课程被选修的学生数 select course.cname,count(sid) from score join course on course.cid=score.cid group by score.cid,course.cname;
--27、查询出只有两门课程的全部学生的学号和姓名 SELECT student.*,count(cid) from score join student on student.id=score.sid group by student.name,id,birthday,sex having count(cid)=2;
--28、查询男生、女生人数 SELECT sex,count(sex) from student group by sex;
--29、查询名字中含有"风"字的学生信息 select * from student where name like("%风%");
--30、查询同名同性学生名单,并统计同名人数
select name,sex,count(name) from student group by name,sex;
--31、查询1990年出生的学生名单 SELECT *,count(*) over() from student where substring(birthday,1,4) = '1990' group by student.id,name,sex,birthday;
select * from student where birthday like("1990%");
select * from student where year(birthday)=1990;
--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 select cid,round(avg(scores),2) as avgs,row_number() over(order by round(avg(scores),2) desc,cid) from score group by cid; --33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 select id,name,tmp.cno from student join (select sid,avg(scores) cno from score group by sid having avg(scores) >=85) tmp on tmp.sid=student.id;
--34、查询课程名称为"数学",且分数低于60的学生姓名和分数 select stu.name,sco.scores from student stu join score sco join course cor on stu.id=sco.sid and sco.cid=cor.cid where cor.cname='数学' and sco.scores<60;
--35、查询所有学生的课程及分数情况 SELECT id,name,tmp.Chinese,tmp.English,tmp.Math from student left join( select sco.sid, sum(case cor.cname when '语文' then sco.scores else 0 end) as Chinese, sum(case cor.cname when '英语' then sco.scores else 0 end) as English, sum(case cor.cname when '数学' then sco.scores else 0 end) as Math from score sco join course cor on cor.cid=sco.cid group by sco.sid) tmp on tmp.sid=student.id;
--36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数 select student.name,course.cname,scores from score join student on student.id=score.sid join course on score.cid=course.cid where scores>=70;
--37、查询课程不及格的学生 select student.name,course.cname,scores from score join student on student.id=score.sid join course on score.cid=course.cid where scores<60;
--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select student.id,student.name,scores from score join student on student.id=score.sid where scores>=80 and cid=1;
--39、求每门课程的学生人数 select cid,count(sid) from score group by cid;
--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select student.*,scores ,RANK() over(order by scores desc) from score join student on student.id=score.sid join course on course.cid=score.cid join teacher on teacher.tid=course.tid where teacher.tname = "张三";
--41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 select s1.* from score s1,score s2 where s1.cid<>s2.cid and s1.scores=s2.scores order by s1.sid; --42、查询每门课程成绩最好的前三名 select tmp.cid,stu.*,tmp.scores,tmp.cno from student stu join (select cid,sid,scores,row_number() over(partition by cid order by scores desc) cno from score) tmp on stu.id=tmp.sid where tmp.cno<=3;
--43、统计每门课程的学生选修人数(超过5人的课程才统计): –要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid,count(sid) as cc from score group by cid having count(sid)>=5 order by cc desc ,cid asc; --44、检索至少选修两门课程的学生学号 select sid,count(cid) as cc from score group by sid having cc >= 2;
--45、查询选修了全部课程的学生信息 select stu.* from score join student stu on stu.id=score.sid group by sid,stu.id,name,sex,birthday having count(cid)=3;
--46 查询各学生的年龄(周岁):按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 ?
--sum 分组完 必须聚合 否则语法不对 select name, sum(case month(student.birthday)<month(current_date()) when true then tmp.tage-1 else tmp.tage end) s_age from student join (select id,year(current_date())-year(birthday) as tage from student) tmp on tmp.id = student.id group by student.name;
--47、查询本周过生日的学生:
select * from student where weekofyear(concat(year(current_date()),'-',date_format(birthday,'MM-dd')))= --把学生的年份换今年 月日不变 求第几周 weekofyear(current_date())
48、查询下周过生日的学生: select * from student where weekofyear(concat(year(current_date()),'-',date_format(birthday,'MM-dd')))= weekofyear(current_date())+1;
49、查询本月过生日的学生: select * from student where month(CURRENT_DATE())=month(birthday);
50、查询12月份过生日的学生: select * from student where month(birthday)='12';
|