--学生表
create table student(s_id string,s_name string,s_birth string,s_sex string)
row format delimited fields terminated by '\t';
--课程表
create table course(c_id string,c_name string,t_id string)
row format delimited fields terminated by '\t';
--教师表
create table teacher(t_id string,t_name string)
row format delimited fields terminated by '\t';
--成绩表
create table score(s_id string,c_id string,s_score int)
row format delimited fields terminated by '\t';
--学生表数据
01 赵雷 1990-01-01 男
02 钱电 1990-12-21 男
03 孙风 1990-05-20 男
04 李云 1990-08-06 男
05 周梅 1991-12-01 女
06 吴兰 1992-03-01 女
07 郑竹 1989-07-01 女
08 王菊 1990-01-20 女
--课程表测试数据
01 语文 02
02 数学 01
03 英语 03
--教师表测试数据
01 张三
02 李四
03 王五
--成绩表测试数据
01 01 80
01 02 90
01 03 99
02 01 70
02 02 60
02 03 80
03 01 80
03 02 80
03 03 80
04 01 50
04 02 30
04 03 20
05 01 76
05 02 87
06 01 31
06 03 34
07 02 89
07 03 98
-- 将文件上传到tmp目录下导入数据
load data local inpath '/tmp/student.txt' into table student;
load data local inpath '/tmp/course.txt' into table course;
load data local inpath '/tmp/score.txt' into table score;
load data local inpath '/tmp/teacher.txt' into table teacher;
set hive.exec.mode.local.auto=true
select st.s_id, st.s_name, s1.s_score cou_01, s2.s_score cou_01
from student st
join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
join score s2 on st.s_id = s2.s_id and s2.c_id = '02'
where s1.s_score > s2.s_score;
select st.s_id, st.s_name, s1.s_score cou_01, s2.s_score cou_01
from student st
join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
join score s2 on st.s_id = s2.s_id and s2.c_id = '02'
where s1.s_score < s2.s_score;
select st.s_id, st.s_name, avg(sc.s_score) avgScore
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name
having avgScore > 60;
select st.s_id, st.s_name, avg(sc.s_score) avgScore
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name
having avgScore < 60
or avgScore is null;
select st.s_id, st.s_name, count(sc.c_id) sumCourse, sum(sc.s_score) sumScore
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name;
select count(*)
from teacher
where t_name like '李%';
select *
from student stu
join (
select s_id
from teacher t
join course c on t.t_name = '张三' and t.t_id = c.t_id
JOIN score s on c.c_id = s.c_id
) t1 on stu.s_id = t1.s_id;
select stu.*
from student stu
left join (
select s_id
from teacher t
join course c on t.t_name = '张三' and t.t_id = c.t_id
join score s on c.c_id = s.c_id
) t1 on stu.s_id = t1.s_id
where t1.s_id is null;
select st.*
from student st
join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
join score s2 on st.s_id = s2.s_id and s2.c_id = '02';
select st.*
from student st
join (select t1.s_id
from (select s_id, c_id from score where c_id = '01') t1
left join (select s_id, c_id from score where c_id = '02') t2
on t1.s_id = t2.s_id
where t2.c_id is null) t3 on st.s_id = t3.s_id;
-----------------------------------------------------------------------
select st.*
from student st
join score s1 on st.s_id = s1.s_id and s1.c_id = '01'
where not exists (select 1 from score s2 where s2.c_id = '02' and st.s_id = s2.s_id);
select st.*
from student st
join (select count(c_id) n1 from course) t1
left join (select s_id, count(c_id) n2 from score group by s_id) t2
on st.s_id = t2.s_id and t1.n1 = t2.n2
where t2.s_id is null;
select distinct st.*
from student st
join score sc1 on sc1.s_id = '01'
join score sc2 on sc1.c_id = sc2.c_id and st.s_id = sc2.s_id
where st.s_id <> '01';
select st.s_id, st.s_name, st.s_birth, st.s_sex
from student st
join score sc on st.s_id != '01' and st.s_id = sc.s_id
join (select collect_set(s1.c_id) coll, count(s1.s_id) num
from score s1
where s1.s_id = '01') t1
where array_contains(t1.coll, sc.c_id)
group by st.s_id, st.s_name, st.s_birth, st.s_sex, t1.num
having t1.num = count(st.s_id);
---------------------------------------------------------------
select st.*
from student st
join (select s1.s_id, concat_ws(',', sort_array(collect_set(s1.c_id))) allCid
from score s1
group by s1.s_id
) t1 on st.s_id != '01' and st.s_id = t1.s_id
join (select concat_ws(',', sort_array(collect_set(s2.c_id))) firstCid
from score s2
where s2.s_id = '01') t2 on t1.allCid = t2.firstCid;
select *
from student st
where not exists (
select 1
from teacher t
join course c on t.t_name = '张三' and t.t_id = c.t_id
join score s on c.c_id = s.c_id
where st.s_id = s.s_id);
select st.s_id, st.s_name, round(t1.avgScore, 2)
from student st
join (select s1.s_id, avg(s1.s_score) avgScore
from score s1
where s1.s_score < 60
group by s1.s_id
having count(*) >= 2) t1 on st.s_id = t1.s_id;
select st.*, sc.s_score
from student st
join score sc on sc.c_id = '01' and sc.s_score < 60 and st.s_id = sc.s_id
order by sc.s_score desc;
select st.s_id,
st.s_name,
max(if(c.c_name = '语文', sc.s_score, 0)) chinese,
max(if(c.c_name = '数学', sc.s_score, 0)) math,
max(if(c.c_name = '英语', sc.s_score, 0)) english,
round(nvl(avg(sc.s_score), 0), 2) avgScore
FROM student st
left join score sc on st.s_id = sc.s_id
left join course c on sc.c_id = c.c_id
group by st.s_id, st.s_name
order by avgScore desc;
课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
select co.c_id,
co.c_name,
max(sc.s_score) max_score,
min(sc.s_score) min_score,
round(avg(sc.s_score), 2) avg_score,
sum(if(sc.s_score >= 60, 1, 0)) / count(*) pass_rate,
sum(if(sc.s_score >= 70 and sc.s_score < 80, 1, 0)) / count(*) medium_rate,
sum(if(sc.s_score >= 80 and sc.s_score < 90, 1, 0)) / count(*) good_rate,
sum(if(sc.s_score >= 90, 1, 0)) / count(*) excellence_rate
from score sc
join course co on sc.c_id = co.c_id
group by co.c_id, co.c_name;
select *, row_number() over (partition by sc.c_id order by sc.s_score desc)
from score sc;
select st.s_id, sum(sc.s_score) sumScore, row_number() over (order by sum(sc.s_score) desc ) rk
from student st
join score sc
on st.s_id = sc.s_id
group by st.s_id
order by sumScore desc;
select t.t_id, c.c_name, round(avg(sc.s_score), 2) avgScore
from teacher t
join course c on t.t_id = c.t_id
join score sc on c.c_id = sc.c_id
group by t.t_id, c.c_name
order by avgScore desc;
select st.s_id, st.s_name, t1.c_id, t1.s_score, t1.rk
from student st
join (select sc.s_id,
sc.c_id,
sc.s_score,
row_number() over (partition by sc.c_id order by sc.s_score desc) rk
from score sc) t1
on st.s_id = t1.s_id
where t1.rk = 2
or t1.rk = 3;
select c.c_id,
c.c_name,
sum(if(s.s_score <= 100 and s.s_score > 85, 1, 0)) 100_85,
round(sum(if(s.s_score <= 100 and s.s_score > 85, 1, 0)) / count(*), 2) 100_85percentage,
sum(if(s.s_score <= 85 and s.s_score > 70, 1, 0)) 70_85,
round(sum(if(s.s_score <= 85 and s.s_score > 70, 1, 0)) / count(*), 2) 70_85percentage,
sum(if(s.s_score <= 70 and s.s_score > 60, 1, 0)) 60_70,
round(sum(if(s.s_score <= 70 and s.s_score > 60, 1, 0)) / count(*), 2) 60_70percentage,
sum(if(s.s_score <= 60 and s.s_score >= 0, 1, 0)) 0_60,
round(sum(if(s.s_score <= 60 and s.s_score >= 0, 1, 0)) / count(*), 2) 0_60percentage
from score s
join course c on s.c_id = c.c_id
group by c.c_id, c.c_name;
select t1.s_id,
t1.s_name,
t1.avg_score,
row_number() over (order by t1.avg_score desc) rk
from (select st.s_id, st.s_name, round(nvl(avg(sc.s_score), 0), 2) avg_score
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id, st.s_name) t1;
select t1.*
from (select sc.s_id,
sc.c_id,
sc.s_score,
row_number() over (partition by sc.c_id order by sc.s_score desc) rk
from score sc) t1
where t1.rk <= 3;
select sc.c_id, count(sc.s_id)
from score sc
group by sc.c_id;
select sc.s_id, st.s_name
from student st
join score sc on st.s_id = sc.s_id
group by sc.s_id, st.s_name
having count(sc.c_id) = 2;
select s_sex, count(*) num
from student
group by s_sex;
select *
from student
where s_name like '%风%';
select s_name, s_sex, count(*)
from student
group by s_name, s_sex
having count(*) > 1;
select *
from student
where year(s_birth) = '1990';
select c.c_id, c.c_name, round(avg(s.s_score), 2) avgScore
from score s
join course c on s.c_id = c.c_id
group by c.c_id, c_name
order by avgScore desc, c.c_id;
select st.s_id, st.s_name, avg(s.s_score) avgScore
from student st
join score s on st.s_id = s.s_id
group by st.s_id, st.s_name
having avgScore >= 85;
select st.s_name, c.c_name, s_score
from student st
join score s on s.s_score < 60 and st.s_id = s.s_id
join course c on c.c_name = '数学' and s.c_id = c.c_id;
select *
from student st
left join score s on st.s_id = s.s_id
left join course c on s.c_id = c.c_id;
select st.s_name, c.c_name, s.s_score
from student st
join score s on st.s_id = s.s_id
join course c on s.c_id = c.c_id
where s.s_score > 70;
select st.s_name, c.c_name, s.s_score
from student st
join score s on st.s_id = s.s_id
join course c on s.c_id = c.c_id
where s.s_score < 60;
select st.s_id, st.s_name, s.s_score
from student st
join score s on s.c_id = '01' and s.s_score > 80 and st.s_id = s.s_id;
select c.c_id, c.c_name, count(*) num_student
from score s
join course c on s.c_id = c.c_id
group by c.c_id, c_name;
select st.s_id, st.s_name, c.c_name, s.s_score
from teacher t
join course c on t.t_name = '张三' and t.t_id = c.t_id
join score s on c.c_id = s.c_id
join student st on s.s_id = st.s_id
order by s.s_score desc
limit 1;
SELECT distinct s1.s_id, s1.c_id, s1.s_score
FROM score s1
JOIN score s2 ON s1.s_id = s2.s_id and s1.s_score = s2.s_score
where s1.c_id != s2.c_id;
select t1.*
from (select st.s_id,
st.s_name,
s.c_id,
s.s_score,
row_number() over (partition by s.c_id order by s.s_score desc) rk
from student st
join score s on st.s_id = s.s_id) t1
where t1.rk <= 3;
select c.c_id, c_name, count(*) num_student
from score s
join course c on s.c_id = c.c_id
group by c.c_id, c_name
having num_student > 5;
select s_id, count(*) num_course
from score
group by s_id
having num_course >= 2;
select st.*
from student st
join (select s_id, count(*) stu_num
from score
group by s_id) t1 on st.s_id = t1.s_id
join (select count(*) all_num
from course) t2 on t1.stu_num = t2.all_num;
select *, floor(datediff(current_date, s_birth) / 365.25) age
from student;
select *
from student
where weekofyear(current_date()) = weekofyear(s_birth);
select *
from student
where weekofyear(date_add(current_date(), 7)) = weekofyear(s_birth);
select *
from student
where month(s_birth) = month(current_date);
select *
from student
where month(s_birth) = 12;