大数据技术之Hive SQL题库-初级3

本文涉及的产品
云原生大数据计算服务 MaxCompute,5000CU*H 100GB 3个月
云原生大数据计算服务MaxCompute,500CU*H 100GB 3个月
简介: 大数据技术之Hive SQL题库-初级3

5.1.5 查询出每门课程的及格人数和不及格人数

hive>

select
    c.course_id,
    c.course_name,
    t1.`及格人数`,
    t1.`不及格人数`
from course_info c
join (
    select
        course_id,
        sum(if(score >= 60,1,0)) as `及格人数`,
        sum(if(score < 60,1,0)) as `不及格人数`
    from score_info
    group by course_id
    ) t1 on c.course_id = t1.course_id;

骚戴理解:这里注意`及格人数`的符号问题,是 ` 不是单引号‘


结果


c.course_id c.course_name t1.及格人数 t1.不及格人数

01 语文 12 7

02 数学 8 11

03 英语 13 6

04 体育 6 6

05 音乐 4 1


5.1.6 查询课程编号为03且课程成绩在80分以上的学生的学号和姓名及课程信息

hive>

select
    s.stu_id,
    s.stu_name,
    t1.score,
    t1.course_id,
    c.course_name
from student_info s
join (
    select
        stu_id,
        score,
        course_id
    from score_info
    where score > 80 and course_id = '03'
    ) t1
on s.stu_id = t1.stu_id
join course_info c on c.course_id = t1.course_id;
骚戴解法
select 
    si2.stu_id ,
    si2.stu_name,
    si.score,
    si.course_id ,
    ci.course_name 
from score_info si 
join student_info si2 on si.stu_id = si2.stu_id 
join course_info ci on si.course_id = ci.course_id 
where ci.course_id ='03' and si.score >=80; 


结果


s.stu_id s.stu_name t1.score t1.course_id c.course_name

002 胡歌 87 03 英语

004 刘德华 89 03 英语

005 唐国强 99 03 英语

013 蒋欣 93 03 英语

015 刘亦菲 84 03 英语

019 邓紫棋 93 03 英语

020 宋丹丹 81 03 英语


5.2 多表连接

5.2.1 课程编号为"01"且课程分数小于60,按分数降序排列的学生信息

hive>

select
    s.stu_id,
    s.stu_name,
    s.birthday,
    s.sex,
    t1.score
from student_info s
join (
    select
        stu_id,
        course_id,
        score
    from score_info
    where score < 60 and course_id = '01'
    ) t1
on s.stu_id=t1.stu_id
order by t1.score desc;
骚戴解法
select 
    si2.stu_id ,
    si2.stu_name,
    si2.birthday,
    si2.sex,
    si.score
from score_info si 
join student_info si2 on si.stu_id = si2.stu_id 
join course_info ci on si.course_id = ci.course_id 
where ci.course_id ='01' and si.score <60
order by si.score desc; 

结果

s.stu_id        s.stu_name      s.birthday      s.sex   t1.score
017           范冰冰        1992-07-04        女      58
008           吴京          1994-02-06        男      56
007           陈坤          1999-04-09        男      48
013           蒋欣          1997-11-08        女      47
019           邓紫棋        1994-08-31        女      46
012           杨紫          1996-12-21        女      44
018           李冰冰        1993-09-24        女      38


5.2.2 查询所有课程成绩在70分以上的学生的姓名、课程名称和分数,按分数升序排列

hive>

select
    s.stu_id,
    s.stu_name,
    c.course_name,
    s2.score
from student_info s
join (
    select
        stu_id,
        sum(if(score >= 70,0,1)) flage
    from score_info
    group by stu_id
    having flage =0
    ) t1
on s.stu_id = t1.stu_id
left join score_info s2 on s.stu_id = s2.stu_id
left join course_info c on s2.course_id = c.course_id;

骚戴理解:这里要有清晰的思路,首先通过子查询把所有课程都70分的学生id查出来,然后和student_info进行join,然后在和score_info进行left join,最后和course_info进行left join。注意顺序不能错!(因为子查询和student_info进行join的结果表只能先和score_info拼接),这里注意left join score_info s2 on s.stu_id = s2.stu_id语句的左边的驱动表是子查询和student_info进行join的结果表,然后我之前没有很好的理解多表联查的on后面的条件拼接,我以为在left join course_info c on s2.course_id = c.course_id;这里的on后面必须是student_info和course_info进行拼接,然而这两个表没发拼接!所以只要是join后的结果表和其他的表继续join,那么就可以使用join过的表的字段进行拼接,例如这里的left join course_info c on s2.course_id = c.course_id;语句的左边驱动表就是子查询和student_info进行join的结果表再和score_info进行left join的结果表,也就是能够和course_info进行join的on后面拼接的条件中可以使用之前的三个表的字段来连接,这里用的就是score_info和course_info拼接,拼接条件是 s2.course_id = c.course_id


结果


s.stu_id s.stu_name c.course_name s2.course

002 胡歌 语文 74

002 胡歌 数学 84

002 胡歌 英语 87

002 胡歌 体育 100

016 周冬雨 语文 71

016 周冬雨 数学 89

016 周冬雨 英语 71

016 周冬雨 体育 94


5.2.3 查询该学生不同课程的成绩相同的学生编号、课程编号、学生成绩

hive>

select
    sc1.stu_id,
    sc1.course_id,
    sc1.score
from score_info sc1 
join score_info sc2 on sc1.stu_id = sc2.stu_id
where sc1.course_id <> sc2.course_id
and sc1.score = sc2.score;

骚戴理解:这里用的是同一个表自己和自己join,理解一下这个自己join自己的场景


结果


sc1.stu_id sc1.course_id sc1.score

016 03 71

017 04 34

016 01 71

005 05 85

007 05 63

009 05 79

017 02 34

005 04 85

007 04 63

009 04 79


5.2.4 查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号

知识点:多表连接 + 条件

hive>

select
    s1.stu_id
from
(
    select
        sc1.stu_id,
        sc1.course_id,
        sc1.score
    from  score_info sc1
    where sc1.course_id ='01'
) s1
join
(
    select
        sc2.stu_id,
        sc2.course_id,
        score
    from score_info sc2
    where sc2.course_id ="02"
)s2
on s1.stu_id=s2.stu_id
where s1.score > s2.score;

结果

stu_id001005008010011013014015017019020


5.2.5 查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名

hive>

select
    t1.stu_id as `学号`,
    s.stu_name as `姓名`
from
(
    select
        stu_id
    from score_info sc1
    where sc1.course_id='01'
    and stu_id in (
          select
              stu_id
          from score_info sc2
          where sc2.course_id='02'
          )
)t1
join student_info s
on t1.stu_id = s.stu_id;

骚戴理解:这里用的where...in....来处理学过编号为“01”的课程并且也学过编号为“02”的课程这个条件筛选


结果


学号 姓名

001 彭于晏

002 胡歌

004 刘德华

005 唐国强

006 陈道明

007 陈坤

008 吴京

009 郭德纲

010 于谦

011 潘长江

012 杨紫

013 蒋欣

014 赵丽颖

015 刘亦菲

016 周冬雨

017 范冰冰

018 李冰冰

019 邓紫棋

020 宋丹丹


5.2.6 查询学过“李体音”老师所教的所有课的同学的学号、姓名

hive>

select
    t1.stu_id,
    si.stu_name
from
(
    select
        stu_id
    from score_info si
    where course_id in
    (
        select
           course_id
        from course_info c
        join teacher_info t
        on c.tea_id = t.tea_id
        where tea_name='李体音'      --李体音教的所有课程
    )
    group by stu_id
    having count(*)=2       --学习所有课程的学生
)t1
left join student_info si
on t1.stu_id=si.stu_id;
骚戴解法
select 
    si.stu_id ,
    si.stu_name 
from (
select 
    stu_id 
from score_info 
where course_id in (
    select 
        course_id 
    from course_info 
    where tea_id  in(
        select 
            tea_id 
        from teacher_info
        where tea_name = '李体音'
    )
  )
    group by stu_id
    having count(*)=2
)t 
join student_info si on si.stu_id = t.stu_id;

结果

s.stu_id s.stu_name005 唐国强007 陈坤009 郭德纲

5.2.7查询学过“李体音”老师所讲授的任意一门课程的学生的学号、姓名

hive>

select
    t1.stu_id,
    si.stu_name
from
(
    select
        stu_id
    from score_info si
    where course_id in
    (
        select
           course_id
        from course_info c
        join teacher_info t
        on c.tea_id = t.tea_id
        where tea_name='李体音'
    )
)t1
left join student_info si
on t1.stu_id=si.stu_id;
骚戴解法
SELECT 
    si.stu_id ,
    si.stu_name 
FROM (
SELECT 
    stu_id 
FROM score_info 
WHERE course_id in (
    SELECT 
        course_id 
    FROM course_info 
    WHERE tea_id  in(
        SELECT 
            tea_id 
        FROM teacher_info
        WHERE tea_name = '李体音'
    )
  )
)t 
join student_info si on si.stu_id = t.stu_id;

结果


s.stu_id s.stu_name

001 彭于晏

002 胡歌

004 刘德华

005 唐国强

007 陈坤

009 郭德纲

010 于谦

013 蒋欣

014 赵丽颖

015 刘亦菲

016 周冬雨

017 范冰冰

018 李冰冰

020 宋丹丹

5.2.8 查询没学过"李体音"老师讲授的任一门课程的学生姓名

hive>

select
    stu_id,
    stu_name
from student_info
where stu_id not in
(
    select
        stu_id
    from score_info si
    where course_id in
    (
        select
           course_id
        from course_info c
        join teacher_info t
        on c.tea_id = t.tea_id
        where tea_name='李体音'
    )
    group by stu_id
);

结果

stu_id stu_name003 周杰伦006 陈道明008 吴京011 潘长江012 杨紫019 邓紫棋


5.2.9查询至少有一门课与学号为“001”的学生所学课程相同的学生的学号和姓名

hive>

select
    si.stu_id,
    si.stu_name
from score_info sc
join student_info si
on sc.stu_id = si.stu_id
where sc.course_id in
(
    select
        course_id
    from score_info
    where stu_id='001'    --001的课程
) and sc.stu_id <> '001'  --排除001学生
group by si.stu_id,si.stu_name;
骚戴理解
select 
    stu_id ,
    stu_name 
from student_info 
where stu_id not in (
    select 
        stu_id 
    from score_info 
    where course_id not in (
        select 
            course_id 
        from score_info 
        where stu_id = '001'
    )
);

结果


s1.stu_id s2.stu_name

002 胡歌

004 刘德华

005 唐国强

006 陈道明

007 陈坤

008 吴京

009 郭德纲

010 于谦

011 潘长江

012 杨紫

013 蒋欣

014 赵丽颖

015 刘亦菲

016 周冬雨

017 范冰冰

018 李冰冰

019 邓紫棋

020 宋丹丹


5.2.10 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

hive>

select
    si.stu_name,
    ci.course_name,
    sc.score,
    t1.avg_score
from score_info sc
join student_info si
on sc.stu_id=si.stu_id
join course_info ci
on sc.course_id=ci.course_id
join
(
    select
        stu_id,
        avg(score) avg_score
    from score_info
    group by stu_id
)t1
on sc.stu_id=t1.stu_id
order by t1.avg_score desc;

结果


t2.stu_name t2.course_name t2.score t1.avg_score

胡歌 体育 100 86.25

胡歌 数学 84 86.25

胡歌 英语 87 86.25

胡歌 语文 74 86.25

刘德华 体育 59 81.5

刘德华 语文 85 81.5

刘德华 英语 89 81.5

刘德华 数学 93 81.5

周冬雨 英语 71 81.25

周冬雨 数学 89 81.25

周冬雨 体育 94 81.25

周冬雨 语文 71 81.25

唐国强 数学 44 75.4

唐国强 音乐 85 75.4

唐国强 语文 64 75.4

唐国强 体育 85 75.4

唐国强 英语 99 75.4

郭德纲 音乐 79 74.2

郭德纲 体育 79 74.2

郭德纲 英语 60 74.2

郭德纲 语文 75 74.2

郭德纲 数学 78 74.2

陈道明 语文 71 73.33333333333333

陈道明 数学 90 73.33333333333333

陈道明 英语 59 73.33333333333333

……

李冰冰 音乐 87 58.0

李冰冰 语文 38 58.0

李冰冰 英语 49 58.0

李冰冰 数学 58 58.0

赵丽颖 数学 39 48.0

赵丽颖 语文 81 48.0

赵丽颖 体育 40 48.0

赵丽颖 英语 32 48.0

范冰冰 英语 55 45.25

范冰冰 体育 34 45.25

范冰冰 数学 34 45.25

范冰冰 语文 58 45.25

吴京 语文 56 43.0

吴京 数学 34 43.0

吴京 英语 39 43.0

相关实践学习
基于MaxCompute的热门话题分析
本实验围绕社交用户发布的文章做了详尽的分析,通过分析能得到用户群体年龄分布,性别分布,地理位置分布,以及热门话题的热度。
SaaS 模式云数据仓库必修课
本课程由阿里云开发者社区和阿里云大数据团队共同出品,是SaaS模式云原生数据仓库领导者MaxCompute核心课程。本课程由阿里云资深产品和技术专家们从概念到方法,从场景到实践,体系化的将阿里巴巴飞天大数据平台10多年的经过验证的方法与实践深入浅出的讲给开发者们。帮助大数据开发者快速了解并掌握SaaS模式的云原生的数据仓库,助力开发者学习了解先进的技术栈,并能在实际业务中敏捷的进行大数据分析,赋能企业业务。 通过本课程可以了解SaaS模式云原生数据仓库领导者MaxCompute核心功能及典型适用场景,可应用MaxCompute实现数仓搭建,快速进行大数据分析。适合大数据工程师、大数据分析师 大量数据需要处理、存储和管理,需要搭建数据仓库?学它! 没有足够人员和经验来运维大数据平台,不想自建IDC买机器,需要免运维的大数据平台?会SQL就等于会大数据?学它! 想知道大数据用得对不对,想用更少的钱得到持续演进的数仓能力?获得极致弹性的计算资源和更好的性能,以及持续保护数据安全的生产环境?学它! 想要获得灵活的分析能力,快速洞察数据规律特征?想要兼得数据湖的灵活性与数据仓库的成长性?学它! 出品人:阿里云大数据产品及研发团队专家 产品 MaxCompute 官网 https://www.aliyun.com/product/odps&nbsp;
目录
相关文章
|
14天前
|
SQL JSON 分布式计算
ODPS SQL ——列转行、行转列这回让我玩明白了!
本文详细介绍了在MaxCompute中如何使用TRANS_ARRAY和LATERAL VIEW EXPLODE函数来实现列转行的功能。
|
20天前
|
SQL 分布式计算 MaxCompute
一种基于ODPS SQL的全局字典索引分布式计算思路
本文提供一种能充分利用分布式计算资源来计算全局字典索引的方法,以解决在大数据量下使用上诉方式导致所有数据被分发到单个reducer进行单机排序带来的性能瓶颈。
|
24天前
|
分布式计算 搜索推荐 物联网
大数据及AI典型场景实践问题之通过KafKa+OTS+MaxCompute完成物联网系统技术重构如何解决
大数据及AI典型场景实践问题之通过KafKa+OTS+MaxCompute完成物联网系统技术重构如何解决
|
26天前
|
存储 人工智能 算法
AI与大数据的结合:案例分析与技术探讨
【8月更文挑战第22天】AI与大数据的结合为各行各业带来了前所未有的机遇和挑战。通过具体案例分析可以看出,AI与大数据在电商、智能驾驶、医疗等领域的应用已经取得了显著成效。未来,随着技术的不断进步和应用场景的不断拓展,AI与大数据的结合将继续推动各行业的创新与变革。
|
28天前
|
SQL 存储 分布式计算
我在淘宝写SQL|ODPS SQL 优化总结
本文结合作者多年的数仓开发经验,结合ODPS平台分享数据仓库中的SQL优化经验。
|
29天前
|
SQL 分布式计算 DataWorks
DataWorks产品使用合集之未保存的ODPS SQL语句该如何找回
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
9天前
|
SQL 分布式计算 大数据
大数据开发SQL代码编码原则和规范
这段SQL编码原则强调代码的功能完整性、清晰度、执行效率及可读性,通过统一关键词大小写、缩进量以及禁止使用模糊操作如select *等手段提升代码质量。此外,SQL编码规范还详细规定了代码头部信息、字段与子句排列、运算符前后间隔、CASE语句编写、查询嵌套、表别名定义以及SQL注释的具体要求,确保代码的一致性和维护性。
11 0
|
1月前
|
消息中间件 监控 大数据
"探索Streaming技术:如何重塑大数据未来,实时处理引领数据价值即时转化新纪元"
【8月更文挑战第10天】信息技术高速发展,数据成为推动社会进步的关键。面对数据爆炸,高效实时处理成挑战。流处理(Streaming)技术应运而生,即时处理数据流,无需积累。应用于实时监控、日志分析等场景。例如,电商平台利用流处理分析用户行为,推送个性化推荐;智能交通系统预测拥堵。结合Apache Kafka和Flink,实现从数据收集到复杂流处理的全过程。流处理技术促进数据即时价值挖掘,与AI、云计算融合,引领大数据未来发展。
67 5
|
17天前
|
监控 Java 开发者
揭秘Struts 2性能监控:选对工具与方法,让你的应用跑得更快,赢在起跑线上!
【8月更文挑战第31天】在企业级应用开发中,性能监控对系统的稳定运行至关重要。针对流行的Java EE框架Struts 2,本文探讨了性能监控的工具与方法,包括商用的JProfiler、免费的VisualVM以及Struts 2自带的性能监控插件。通过示例代码展示了如何在实际项目中实施这些监控手段,帮助开发者发现和解决性能瓶颈,确保应用在高并发、高负载环境下稳定运行。选择合适的监控工具需综合考虑项目需求、成本、易用性和可扩展性等因素。
23 0
|
17天前
|
大数据 数据处理 分布式计算
JSF 逆袭大数据江湖!看前端框架如何挑战数据处理极限?揭秘这场技术与勇气的较量!
【8月更文挑战第31天】在信息爆炸时代,大数据已成为企业和政府决策的关键。JavaServer Faces(JSF)作为标准的 Java Web 框架,如何与大数据技术结合,高效处理大规模数据集?本文探讨大数据的挑战与机遇,介绍 JSF 与 Hadoop、Apache Spark 等技术的融合,展示其实现高效数据存储和处理的潜力,并提供示例代码,助您构建强大的大数据系统。
25 0