大数据第57天—Mysql练习题12道之十二-百度-杨大伟
--第12题 百度
--现在有三个表student(学生表)、course(课程表)、score(成绩单),结构如下:
1 create table test_twelve_student 2 ( 3 id bigint comment '学号', 4 name string comment '姓名', 5 age bigint comment '年龄' 6 ) 7 row format delimited fields terminated by '\t'; 8 9 create table test_twelve_course 10 ( 11 cid string comment '课程号,001/002格式', 12 cname string comment '课程名' 13 ) 14 row format delimited fields terminated by '\t'; 15 16 Create table test_twelve_score 17 ( 18 id bigint comment '学号', 19 cid string comment '课程号', 20 score bigint comment '成绩' 21 ) 22 row format delimited fields terminated by '\t';
1 --插入数据 2 insert into table test_twelve_student values (1001,'wsl1',21); 3 insert into table test_twelve_student values (1002,'wsl2',22); 4 insert into table test_twelve_student values (1003,'wsl3',23); 5 insert into table test_twelve_student values (1004,'wsl4',24); 6 insert into table test_twelve_student values (1005,'wsl5',25); 7 8 insert into table test_twelve_course values ('001','math'); 9 insert into table test_twelve_course values ('002','English'); 10 insert into table test_twelve_course values ('003','Chinese'); 11 insert into table test_twelve_course values ('004','music'); 12 13 insert into table test_twelve_score values (1001,'004',10); 14 insert into table test_twelve_score values (1002,'003',21); 15 insert into table test_twelve_score values (1003,'002',32); 16 insert into table test_twelve_score values (1004,'001',43); 17 insert into table test_twelve_score values (1005,'003',54); 18 insert into table test_twelve_score values (1001,'002',65); 19 insert into table test_twelve_score values (1002,'004',76); 20 insert into table test_twelve_score values (1003,'002',77); 21 insert into table test_twelve_score values (1001,'004',48); 22 insert into table test_twelve_score values (1002,'003',39);
1 --其中score中的id、cid,分别是student、course中对应的列请根据上面的表结构,回答下面的问题 2 --1)请将本地文件(/home/users/test/20190301.csv)文件,加载到分区表score的20190301分区中,并覆盖之前的数据 3 load data local inpath '/home/users/test/20190301.csv' overwrite into table test_twelve_score partition(event_day='20190301'); 4 --2)查出平均成绩大于60分的学生的姓名、年龄、平均成绩
1 select 2 stu.name, 3 stu.age, 4 t1.avg_score 5 from 6 test_twelve_student stu 7 join 8 ( 9 select 10 id, 11 avg(score) avg_score 12 from test_twelve_score 13 group by id 14 ) t1 15 on t1.id = stu.id 16 where avg_score > 60; 17 --3)查出没有'001'课程成绩的学生的姓名、年龄 18 select 19 stu.name, 20 stu.age 21 from 22 test_twelve_student stu 23 join 24 ( 25 select 26 id 27 from test_twelve_score 28 where cid != 001 29 group by id 30 ) t1 31 on stu.id = t1.id; 32 --4)查出有'001'\'002'这两门课程下,成绩排名前3的学生的姓名、年龄 33 select 34 stu.name, 35 stu.age 36 from 37 ( 38 select 39 id, 40 cid, 41 score, 42 rank() over(partition by cid order by score desc) ran 43 from 44 test_twelve_score 45 where cid = 001 or cid = 002 46 ) t1 47 join test_twelve_student stu 48 on t1.id = stu.id 49 where ran <= 3; 50 51 52 --5)创建新的表score_20190317,并存入score表中20190317分区的数据 53 create table score_20190317 54 as select * from test_twelve_score where dt = '20190317'; 55 --6)如果上面的score_20190317score表中,uid存在数据倾斜,请进行优化,查出在20190101-20190317中,学生的姓名、年龄、课程、课程的平均成绩 56 select 57 stu.name, 58 stu.age, 59 cou.cname, 60 t1.avg_score 61 62 from 63 ( 64 select 65 id, 66 cid, 67 avg(score) avg_score 68 from test_twelve_score 69 group by id,cid 70 where dt >= '20190101' and dt <= '20190317' 71 ) t1 72 left join test_twelve_student stu on t1.id = stu.id 73 left join test_twelve_course cou on t1.cid = cou.cid 74 --7)描述一下union和union all的区别,以及在mysql和HQL中用法的不同之处? 75 76 union会对数据进行排序去重,union all不会排序去重。 77 HQL中要求union或union all操作时必须保证select 集合的结果相同个数的列,并且每个列的类型是一样的。 78 79 --8)简单描述一下lateral view语法在HQL中的应用场景,并写一个HQL实例 80 -- 比如一个学生表为: 81 -- 学号 姓名 年龄 成绩(语文|数学|英语) 82 -- 001 张三 16 90,80,95 83 -- 需要实现效果: 84 -- 学号 成绩 85 -- 001 90 86 -- 001 80 87 -- 001 95 88 89 90 create table student( 91 `id` string, 92 `name` string, 93 `age` int, 94 `scores` array<string> 95 ) 96 row format delimited fields terminated by '\t' 97 collection items terminated by ','; 98 99 100 select 101 id, 102 score 103 from 104 student lateral view explode(scores) tmp_score as score;