Hql练习题

use myhive;
//1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select
st.,a.s_score,b.s_score
from student st
left join score a on st.s_id = a.s_id and a.c_id = ‘01’
left join score b on b.s_id = st.s_id and b.c_id = ‘02’
where a.s_score > b.s_score;
//2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select
st.
,a.s_score,b.s_score
from student st
left join score a on st.s_id = a.s_id and a.c_id = ‘01’
left join score b on st.s_id = b.s_id and b.c_id = ‘02’
where a.s_score < b.s_score;
// 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select
st.s_id,st.s_name,avg(sc.s_score)
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id,st.s_name
having AVG(sc.s_score) >= 60 ;
// 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
select
st.s_id,st.s_name,avg(sc.s_score)
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id,st.s_name
having AVG(sc.s_score) < 60
OR AVG(sc.s_score) is null;
// 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select
st.s_id,st.s_name,count(sc.c_id),sum(sc.s_score)
from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id,st.s_name;
// 6、查询"李"姓老师的数量
select
count()
from teacher t
where t.t_name like “李%”;
// 7、查询学过"张三"老师授课的同学的信息
select st.
from student st where st.s_id in (
select
sc.s_id
from score sc
inner join course c on c.c_id=sc.c_id
inner join teacher t on t.t_id=c.t_id and t.t_name=“张三”
);
// 8、查询没学过"张三"老师授课的同学的信息
select st.* from student st where st.s_id not in (
select
sc.s_id
from score sc
inner join course c on c.c_id=sc.c_id
inner join teacher t on t.t_id=c.t_id and t.t_name=“张三”
);
// 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
// 第一种方式:
select
st.*
from student st
inner join score sc on st.s_id = sc.s_id and sc.c_id = ‘01’
where st.s_id in (
select
st2.s_id
from student st2
inner join score sc2 on st2.s_id = sc2.s_id and sc2.c_id = ‘02’
);
// 第二种方式:
select
st.*
from student st
left join score sc1 on sc1.s_id = st.s_id and sc1.c_id = ‘01’
left join score sc2 on sc2.s_id = st.s_id and sc2.c_id = ‘02’
where sc1.s_id is not null and sc2.s_id is not null;
// 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
// 第一种方式:
select
st.*
from student st
inner join score sc on st.s_id = sc.s_id and sc.c_id = ‘01’
where st.s_id not in (
select
st2.s_id
from student st2
inner join score sc2 on st2.s_id = sc2.s_id and sc2.c_id = ‘02’
);
// 第二种方式:
select
st.*
from student st
left join score sc1 on sc1.s_id = st.s_id and sc1.c_id = ‘01’
left join score sc2 on sc2.s_id = st.s_id and sc2.c_id = ‘02’
where sc1.s_id is not null and sc2.s_id is null;
// 11、查询没有学全所有课程的同学的信息
select
st.*
from student st
left join score sc1 on sc1.s_id = st.s_id and sc1.c_id = ‘01’
left join score sc2 on sc2.s_id = st.s_id and sc2.c_id = ‘02’
left join score sc3 on sc3.s_id = st.s_id and sc3.c_id = ‘03’
where sc1.s_id is null or sc2.s_id is null or sc3.s_id is null;
// 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select
distinct st.*
from student st
left join score sc1 on st.s_id = sc1.s_id
where sc1.c_id in(
select
sc2.c_id
from score sc2
where sc2.s_id =‘01’
);
// 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
select *
from student s
join
(select concat_ws(’,’,collect_set(c_id)) concat
from
score
where s_id=‘01’)tmp1
left join
(select s_id,concat_ws(’,’,collect_set(c_id)) concat
from
score
where s_id!=‘01’
group by s_id)tmp2
on tmp2.s_id=s.s_id and tmp2.concat=tmp1.concat
where tmp2.concat is not null;
// 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select st.* from student st where st.s_id not in (
select
sc.s_id
from score sc
inner join course c on c.c_id=sc.c_id
inner join teacher t on t.t_id=c.t_id and t.t_name=“张三”
);
// 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select
st.s_id,st.s_name,avg(sc.s_score)
from student st
inner join score sc on st.s_id = sc.s_id
inner join course co on co.c_id = sc.c_id
where sc.s_score<60
group by st.s_id,st.s_name
having count() >=2;
// 16、检索"01"课程分数小于60,按分数降序排列的学生信息
select
st.
,sc.s_score
from student st
join score sc on st.s_id = sc.s_id
where sc.c_id = ‘01’ and sc.s_score < 60
order by sc.s_score desc;
// 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
// 第一种方式
select s.,
tmp.avg,
tmp1.s_score,
tmp2.s_score,
tmp3.s_score
from student s
left join (select s_id,AVG(s_score) avg from score group by s_id )tmp on tmp.s_id=s.s_id
left join (select s_id,s_score from score where c_id=‘01’)tmp1 on tmp1.s_id=s.s_id
left join (select s_id,s_score from score where c_id=‘02’)tmp2 on tmp2.s_id=s.s_id
left join (select s_id,s_score from score where c_id=‘03’)tmp3 on tmp3.s_id=s.s_id
order by tmp.avg desc;
// 第二种方式
select
st.s_id,
st.s_name,
sc.s_score,
sc2.s_score,
sc3.s_score,
avg(sc4.s_score) a
from student st
left join score sc on (sc.c_id=‘01’ and sc.s_id=st.s_id)
left join score sc2 on (sc2.c_id=‘02’ and sc2.s_id=st.s_id)
left join score sc3 on (sc3.c_id=‘03’ and sc3.s_id=st.s_id)
left join score sc4 on (sc4.s_id=st.s_id)
group by st.s_id,st.s_name,sc.s_score,sc2.s_score,sc3.s_score
order by a desc;
// 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
select
co.c_id,
co.c_name,
max(sc.s_score),
min(sc.s_score),
avg(sc.s_score),
sum(case when sc.s_score >=60 then 1 else 0 end )/count(sc.s_id),
sum(case when sc.s_score >=70 and sc.s_score<80 then 1 else 0 end )/count(sc.s_id),
sum(case when sc.s_score >=80 and sc.s_score<90 then 1 else 0 end )/count(sc.s_id),
sum(case when sc.s_score >=90 then 1 else 0 end )/count(sc.s_id)
from course co
left join score sc on co.c_id = sc.c_id
group by co.c_id,co.c_name;
// 19、按各科成绩进行排序,并显示排名:– row_number() over()分组排序功能(mysql没有该方法)
select
c_id,
s_score,
row_number() over (partition by c_id order by s_score desc)
from score;
// 20、查询学生的总成绩并进行排名
select
sc.s_id,
st.s_name,
sum(sc.s_score) sum
from score sc
join student st on sc.s_id=st.s_id
group by sc.s_id,st.s_name
order by sum desc;
// 21、查询不同老师所教不同课程平均分从高到低显示
select
t.t_name,
c.c_name,
AVG(sc.s_score) avg
from teacher t
left join course c on c.t_id=t.t_id
left join score sc on sc.c_id =c.c_id
group by t.t_name,c.c_name
order by avg desc;
// 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select
*
from (
select
st.
,
sc.s_score,
row_number() over (partition by sc.c_id order by sc.s_score desc) as rank
from score sc
join student st on sc.s_id=st.s_id) b
where (b. rank between 2 and 3);
// 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
select
co.c_id,
co.c_name,
concat(sum(case when sc.s_score >85 and sc.s_score<=100 then 1 else 0 end )/count(sc.s_id)100,"%"),
concat(sum(case when sc.s_score >70 and sc.s_score<=85 then 1 else 0 end )/count(sc.s_id)100,"%"),
concat(sum(case when sc.s_score >60 and sc.s_score<=70 then 1 else 0 end )/count(sc.s_id)100,"%"),
concat(sum(case when sc.s_score <=60 and sc.s_score>0 then 1 else 0 end )/count(sc.s_id)100,"%")
from course co
left join score sc on co.c_id = sc.c_id
group by co.c_id,co.c_name;
// 24、查询学生平均成绩及其名次
select
st.s_name,
sc.s_id,
avg(sc.s_score),
rank() over (order by avg(sc.s_score) desc)
from score sc
join student st on sc.s_id = st.s_id
group by st.s_name,sc.s_id;
// 25、查询各科成绩前三名的记录
select
*
from (select
st.
,
sc.s_score,
row_number() over (partition by sc.c_id order by sc.s_score desc) a
from score sc
join student st on sc.s_id=st.s_id) a
where a.a between 1 and 3;
// 26、查询每门课程被选修的学生数
select
c_id,count(
)
from score
group by c_id;
// 27、查询出只有两门课程的全部学生的学号和姓名
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(
)==2;
// 28、查询男生、女生人数
select
sum(case when s_sex = ‘男’ then 1 else 0 end ) man,
sum(case when s_sex = ‘女’ then 1 else 0 end ) women
from student;
// 29、查询名字中含有"风"字的学生信息
select
*
from student
where s_name like ‘%风%’;
// 30、查询同名同性学生名单,并统计同名人数
select
st.s_name,
count(
)
from student st,student st2
where st2.s_sex = st.s_sex and st2.s_name = st.s_name and st2.s_id <> st.s_id
group by st.s_name;
// 31、查询1990年出生的学生名单
// 第一种方式
select
*
from student
where s_birth like ‘1990%’;
// 第二种方式:
select
*
from student
where YEAR(s_birth) = 1990;
// 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select
c_id,
avg(s_score) avg
from score
group by c_id
order by avg desc,c_id ASC;
// 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
select
sc.s_id,
st.s_name,
avg(sc.s_score)
from student st
join score sc on st.s_id = sc.s_id
group by sc.s_id,st.s_name
having avg(sc.s_score) >= 85;
// 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
select
s_name,
sc.s_score
from student st
join score sc on st.s_id = sc.s_id and sc.s_score < 60
join course co on co.c_id = sc.c_id and co.c_name = ‘数学’;
// 35、查询所有学生的课程及分数情况
select
*
from student st
join score sc on st.s_id = sc.s_id;
// 36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数
select
st.s_name,
co.c_name,
sc.s_score
from student st
join score sc on st.s_id = sc.s_id
join course co on co.c_id = sc.c_id
where sc.s_score > 70;
// 37、查询课程不及格的学生
select
s.s_name,
tmp.*
from student s
join ( select sc.s_id,
c_name,
s_score
from score sc
join course c on sc.c_id=c.c_id
where s_score<60
)tmp
on tmp.s_id=s.s_id;
// 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
select
st.s_id,
st.s_name
from student st
join score sc on st.s_id = sc.s_id and sc.c_id = ‘01’ and sc.s_score >80;
// 39、求每门课程的学生人数
select
c_id,
count()
from score
group by c_id;
// 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
// 第一种方式:
select
st.

from student st,(select
sc.s_id
from score sc
inner join course c on c.c_id = sc.c_id
inner join teacher t on t.t_id = c.t_id and t.t_name = “张三”
order by sc.s_id
limit 1) sc2
where st.s_id = sc2.s_id;
// 第二种方式:
select
tmp.*
from (select
s.,
s_score,
RANK() over(order by s_score desc) b
from teacher t
join course c on t.t_name=‘张三’ and t.t_id=c.t_id
join score sc on c.c_id=sc.c_id
join student s on sc.s_id=s.s_id
) tmp
where tmp.b=1;
// 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select
sc1.s_id,
sc1.c_id,
sc2.s_id,
sc2.c_id,
sc1.s_score
from score sc1
join score sc2
where sc1.s_score=sc2.s_score and sc1.s_id!=sc2.s_id and sc1.c_id!=sc2.c_id
order by sc1.s_id;
// 42、查询每门课程成绩最好的前三名
select
*
from (select
s_id,
c_id,
s_score,
row_number() over (partition by c_id order by s_score) a
from score) a
where a.a between 1 and 3;
// 43、统计每门课程的学生选修人数(超过5人的课程才统计):
select
c_id,count(
)
from score
group by c_id
having count() > 5;
// 44、检索至少选修两门课程的学生学号
// 方式一:
select
st.s_id
from student st
left join score sc on st.s_id = sc.s_id and sc.c_id = ‘01’
left join score sc2 on st.s_id = sc2.s_id and sc2.c_id = ‘02’
left join score sc3 on st.s_id = sc3.s_id and sc3.c_id = ‘03’
where (sc.s_id is not null and sc2.s_id is not null) or
(sc.s_id is not null and sc3.s_id is not null) or
(sc3.s_id is not null and sc2.s_id is not null);
// 方式二:
select
s_id
from score
group by s_id
having count(c_id) >= 2;
// 45、查询选修了全部课程的学生信息
// 方式一:
select
st.

from student st
left join score sc on st.s_id = sc.s_id and sc.c_id = ‘01’
left join score sc2 on st.s_id = sc2.s_id and sc2.c_id = ‘02’
left join score sc3 on st.s_id = sc3.s_id and sc3.c_id = ‘03’
where sc.s_id is not null and sc2.s_id is not null and sc3.s_id is not null;
// 方式二:
select
sc.s_id,
st.s_birth,
st.s_name,
st.s_sex
from student st
left join score sc on st.s_id = sc.s_id
group by sc.s_id,st.s_birth,st.s_name,st.s_sex
having count(sc.c_id) = 3;
// 46、查询各学生的年龄(周岁)
// 方式一:
select
s_name,CONCAT(2020-YEAR(s_birth),‘岁’)
from student;
// 方式二:
select
s_name,
floor((unix_timestamp()-unix_timestamp(date(s_birth)))/60/60/24/365) 年龄
from student;
// 47、查询本周过生日的学生
select
*
from student
where WEEKOFYEAR(CURRENT_DATE)=WEEKOFYEAR(s_birth);
// 48、查询下周过生日的学生
select
*
from student
where WEEKOFYEAR(CURRENT_DATE)+1=WEEKOFYEAR(s_birth);
// 49、查询本月过生日的学生
select
*
from student
where MONTH(CURRENT_DATE)=MONTH(s_birth);
// 50、查询12月份过生日的学生
select
*
from student
where MONTH(s_birth)=12;

// 1、使用 over() 函数进行数据统计, 统计每个用户信息及表中数据的总条数
select
,
count(
) over ()
from test_window;
// 2、求用户明细并统计每天的用户总数
select
,
count(
) over(partition by day)
from test_window;
// 3、计算从第一天到现在的所有 score 大于80分的用户总数
select
,
count(
) over(order by day rows between unbounded preceding and current row)
from test_window
where score > 80;
//1、查询在2020年4月份购买过的顾客及总人数
select
,
count(
) over ()
from business
where substr(orderdate,1,7) = ‘2020-04’;
//2、查询顾客的购买明细及月购买总额
select
*,
sum(cost) over ()
from business;
//3、查询顾客的购买明细及到目前为止每个顾客购买总金额
select
* ,
sum(cost) over(distribute by name sort by orderdate)
from business;
//4、查询顾客上次的购买时间
select
*,
lag(orderdate,1,‘无’) over()
from business;
//5、查询前20%时间的订单信息
select
*
from (select
*,
ntile(5) over (order by orderdate desc) aa
from business) aa
where aa.aa = 1;
// 1、每门学科学生成绩排名(是否并列排名、空位排名三种实现)
select
*,
row_number() over (partition by subject order by score) rw,
rank() over (partition by subject order by score) rk,
dense_rank() over (partition by subject order by score desc) de
from window_score;
// 2、每门学科成绩排名top3的学生
select
*
from (select
*,
row_number() over (partition by subject order by score desc) rw
from window_score) rw
where rw <= 3;

Guess you like

Origin blog.csdn.net/weixin_44704605/article/details/110365534