create table word(line string) location '/data/';
select word,count(*) as word_count
from word
lateral view explode(split(line,' ')) t1 as word
group by word
order by word_count desc;
create database if not exists hql50;
use hql50;
create table if not exists course(course_id int,course_name string,teacher_id int);
create table if not exists score(student_id int,course_id int,score int);
create table if not exists student(student_id int,student_name string,student_birth string,student_sex string);
create table if not exists teacher(teacher_id int,
teacher_name string
);
insert into table course values
(01,'语文',02),
(02,'数学',01),
(03,'英语',03);
insert into table score values
(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);
insert into table student values
(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','女');
insert into table teacher values (01,'张三'),(02,'李四'),(03,'王五');
select s3.*,s1.score,s2.score
from score s1 join score s2 join student s3
on s1.student_id=s2.student_id and s2.student_id = s3.student_id
where s1.course_id=1 and s2.course_id=2 and s1.score>s2.score;
select s3.*,s1.score,s2.score
from score s1 join score s2 join student s3
on s1.student_id=s2.student_id and s2.student_id = s3.student_id
where s1.course_id=1 and s2.course_id=2 and s1.score<s2.score;
with t1 as (
select round(avg(s1.score),2) a,s1.student_id
from score s1
group by s1.student_id)
select t1.* ,s1.student_name
from t1 join student s1
on s1.student_id=t1.student_id
where t1.a>60;
with t1 as (
select round(avg(s1.score),2) a,s1.student_id
from score s1
group by s1.student_id)
select s1.student_id, s1.student_name,t1.a
from student s1 left join t1
on s1.student_id=t1.student_id
where t1.a<60 or t1.a is null ;
with
t1 as
(select distinct * from
(select s1.student_id,count(s1.course_id) over(partition by student_id) as win1,sum(s1.score) over(partition by student_id) as win2
from score s1) t1)
select student.student_id,student_name,case when t1.win1 is null then '0' else t1.win1 end,case when t1.win2 is null then '0' else t1.win2 end
from student left join t1 on t1.student_id = student.student_id;
with
t1 as(
select teacher_name
from teacher
where teacher_name like '李%')
select count(1) from t1;
select s2.student_id
from score s1 join course c join teacher t1 join student s2 join (select teacher_id from teacher where teacher_name='张三') a
on s1.course_id = c.course_id and t1.teacher_id = c.teacher_id and s1.student_id = s2.student_id and a.teacher_id=t1.teacher_id;
select distinct s1.*
from student s1 left join score s3 on s1.student_id=s3.student_id
where not exists
(select s2.*
from score s1 join course c join teacher t1 join student s2 join (select teacher_id from teacher where teacher_name='张三') a
on s1.course_id = c.course_id and t1.teacher_id = c.teacher_id and s1.student_id = s2.student_id and a.teacher_id=t1.teacher_id
where s2.student_id=s3.student_id );
select s1.*
from student s1 join (
SELECT s.`student_id`
FROM score s JOIN student stu
ON s.`student_id`=stu.`student_id`
WHERE s.`course_id` IN(1,2)
GROUP BY s.`student_id`
HAVING COUNT(s.`course_id`)=2) s2
on s1.student_id=s2.student_id;
select a.student_id,a.student_name,a.student_birth,a.student_sex
from student a
join score b on a.student_id=b.student_id and b.course_id=1
where not exists
(select * from score c where a.student_id=c.student_id and c.course_id=2);
select distinct a.*
from student a join course b left join score c
on c.student_id =a.student_id and c.course_id=b.course_id
where c.score is null;
select distinct
a.*
from student a
join score b on b.student_id=1
join score c on b.course_id=c.course_id and a.student_id=c.student_id
where a.student_id!=1;
select s.student_name from student s join score sc on s.student_id=sc.student_id
join(select collect_set(course_id) as sub,count(course_id) as num from score where student_id=1 ) sc2
where array_contains(sc2.sub,sc.course_id)
group by s.student_id,s.student_name,s.student_birth,s.student_sex,sc2.num
having count(sc.course_id)=sc2.num;
select a.*
from student a
where not exists
(select 1 from score b,course c,teacher d
where a.student_id=b.student_id and b.course_id=c.course_id
and c.teacher_id=d.teacher_id and d.teacher_name='张三');
SELECT a.student_id,a.student_name,ROUND(AVG(b.score),2)
FROM student a
JOIN score b ON a.student_id=b.student_id AND b.score<60
GROUP BY a.student_id,a.student_name
HAVING COUNT(1)>=2;
SELECT a.*,b.course_id,b.score
FROM student a
JOIN score b ON a.student_id=b.student_id AND b.score<60
WHERE b.course_id=1
ORDER BY b.score DESC;
SELECT
a.student_id,
b.score as 01_score,
c.score as 02_score,
d.score as 03_score,
ROUND ((b.score+c.score+d.score)/3,2) as avg_score
FROM score a
join score b on a.student_id=b.student_id and b.course_id=1
join score c on a.student_id=c.student_id and c.course_id=2
join score d on a.student_id=d.student_id and d.course_id=3
GROUP BY a.student_id,b.score,c.score,d.score
ORDER BY ROUND ((b.score+c.score+d.score)/3,2) DESC ;
SELECT
a.course_id,
b.course_name,
MAX(a.score) AS max_score,
MIN(a.score) AS min_score,
ROUND (AVG(a.score),2) AS avg_score,
round(count(if(a.score>=60,a.score,null))/count(a.score)*100,2) as jige,
round(count(if(a.score>=70 and a.score<80,a.score,null))/count(a.score)*100,2) as zd,
round(count(if(a.score>=80 and a.score<90,a.score,null))/count(a.score)*100,2) as yl,
round(count(if(a.score>=90,a.score,null))/count(a.score)*100,2) as yx
FROM score a
JOIN course b ON a.course_id=b.course_id
GROUP BY a.course_id,b.course_name;
select student_id,score,row_number() over(partition by course_id order by score desc )as win1
from score ;
select b.student_id,row_number() over(order by b.win1 desc)
from
(
select distinct a.student_id,a.win1
from
(select student_id,sum(score) over(partition by student_id) as win1
from score) a) b;
select avg(score) b
from score
group by course_id
order by b desc;
select a.*,s1.*
from
(select s1.*,row_number() over(partition by course_id order by score desc) as win1
from score s1) a join student s1 on a.student_id=s1.student_id
where a.win1 in (2,3);
select distinct b.course_id,b.score_group,b.c1
from (
select a.course_id,a.score_group,(count(student_id) over(partition by course_id,a.score_group))/(count(student_id) over(partition by course_id)) c1
from (
select s1.*,case when s1.score between 0 and 60 then 1 when s1.score between 61 and 70 then 2 when s1.score between 71 and 85 then 3 when s1.score between 86 and 100 then 4 end as score_group
from score s1) a) b;
with a1 as (select student.student_name student_name,avg(score.score) over(distribute by score.student_id) as avgscore from score
join student on student.student_id=score.student_id)
select DISTINCT a1.student_name,a1.avgscore,row_number()over(order by a1.avgscore desc)from a1
with a1 as (select course.course_name course_name,student.student_name student_name,score.score score,row_number()over(partition by score.course_id)as paiming from score
join student on student.student_id=score.student_id
join course on course.course_id=score.course_id)
select a1.course_name,a1.student_name,a1.score,a1.paiming from a1 distribute by a1.course_name limit 3
select course_id,count(1)
from score
group by course_id;
with
t1 as
(select score.student_id from score group by score.student_id having count(*)=2)
select t2.student_id,t2.student_name from student t2 right join t1 on t1.student_id=t2.student_id;
select student_sex,count(*) from student group by student_sex;
select * from student where student_name like '%风%';
select student_name,count(*)
from student
group by student_name
having count(*)>1;
select student_name
from student
where year(student_birth)=1990;
select course_id,avg(score) as a
from score
group by course_id
order by a desc,course_id asc;
with t1 as
(select student_id,round(avg(score),2)as a from score group by student_id having avg(score)>=85)
select t2.student_id,t2.student_name,t1.a
from student t2 right join t1 on t2.student_id=t1.student_id;
with
t3 as
(select t2.student_id,t2.score from score t2 right join (select course_id from course where course_name='数学')as t1
on t1.course_id=t2.course_id where t2.score<60)
select t4.student_name,t3.score from student t4
right join t3 on t4.student_id=t3.student_id;
select s1.student_name,s2.course_id,s2.score
from student s1 left join score s2 on s1.student_id=s2.student_id ;
with
t1 as
(select s.*,c.course_name
from score s join course c on c.course_id=s.course_id
where score>70)
select distinct s1.student_name,t1.course_name,t1.score
from t1 join student s1 on t1.student_id = s1.student_id;
with
t1 as(
select distinct student_id
from score
where score <60)
select s.*
from student s join t1 on t1.student_id=s.student_id;
select *
from score
where course_id=1;
select distinct *
from(
select course_id,count(student_id) over(partition by course_id)
from score) a;
select c.student_id,c.student_name,c1.course_name,c.score
from (
select s1.*,s.score,s.course_id,row_number() over(order by s.score desc) as win1
from score s join(
select c.course_id
from course c join (
select teacher_id
from teacher
where teacher_name='张三')a on c.teacher_id=a.teacher_id) b on s.course_id=b.course_id
join student s1
on s1.student_id=s.student_id) c join course c1 on c1.course_id=c.course_id
where c.win1=1;
select s1.student_id,s1.course_id,s2.course_id,s1.score
from score s1 ,score s2
where s1.score=s2.score and s1.course_id!=s2.course_id;
select a.*
from(
select s.*,row_number() over(partition by s.course_id order by s.score desc)as win1
from score s ) a
where a.win1<=3;
select course_id,count(student_id) over(partition by course_id) as win1
from score
order by win1 desc ,course_id asc;
select distinct a.student_id
from(
select student_id,count(course_id) over(partition by student_id) as win1
from score) a
where a.win1>=2;
select distinct s.*
from (
select a.student_id
from(
select student_id ,count(course_id) over(partition by student_id) as win1
from score )a
join
(select count(course_id) c
from course) b on a.win1=b.c ) d join student s on s.student_id=d.student_id;
select *, cast(date_format(current_date(),'yyyy') as int)-cast(date_format(student_birth,'yyyy') as int)
from student;
select *
from student
where unix_timestamp(cast(concat_ws('-',date_format(current_date(),'yyyy'),date_format(student_birth,'MM'),date_format(student_birth,'dd')) as date),'yyyy-MM-dd')
between unix_timestamp(current_date())
and unix_timestamp(date_sub(next_day(current_date(),'MON'),1),'yyyy-MM-dd');
select *
from student
where unix_timestamp(cast(concat_ws('-',date_format(current_date(),'yyyy'),date_format(student_birth,'MM'),date_format(student_birth,'dd')) as date),'yyyy-MM-dd')
between unix_timestamp(next_day(current_date(),'MON'),'yyyy-MM-dd')
and unix_timestamp(date_add(next_day(current_date(),'MON'),6),'yyyy-MM-dd');
select *
from student
where month(student_birth) = month(current_date());
select *
from student
where month(student_birth)=12;