Hive -------- hive常见查询练习

版权声明:个人原创,转载请标注! https://blog.csdn.net/Z_Date/article/details/83997924

表名和字段

–1.学生表 
Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 
–2.课程表 
Course(c_id,c_name,t_id) – –课程编号, 课程名称, 教师编号 
–3.教师表 
Teacher(t_id,t_name) –教师编号,教师姓名 
–4.成绩表 
Score(s_id,c_id,s_score) –学生编号,课程编号,分数

测试数据

--建表--学生表
CREATE TABLE test.Student(s_id int,s_name string,s_birth string,s_sex string) row format delimited fields terminated by ',';
--课程表
CREATE TABLE test.Course(c_id  int,c_name string ,t_id int) row format delimited fields terminated by ',';
--教师表
CREATE TABLE test.Teacher(t_id int,t_name string)row format delimited fields terminated by ',';
--成绩表
CREATE TABLE test.Score(s_id int,c_id  int, s_score int)row format delimited fields terminated by ',';
--学生表测试数据
01,zhaolei,1990-01-01,male
02,qianfeng,1990-12-21,male
03,sunfeng,1990-05-20,male
04,liyun,1990-08-06,male
05,zhoumei,1991-12-01,female
06,wulan,1992-03-01,female
07,zhengzu,1989-07-01,female
08,wangju,1990-01-20,female
 
--课程表测试数据
01,chinese,02
02,math,01
03,english,03
 
--教师表测试数据
01,laochang
02,dazhao
03,laoli
 
--成绩表测试数据
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

练习题和sql语句

基础sql语句

  1. 统计每个学生的总分。
  1. 统计出学号和总分
   select s_id , sum(s_score) as zf  from  score  group  by s_id

上表结果和stuent做join连接

select stu.s_name,zf.ss from

(select s_id , sum(s_score) as ss from score group by s_id) as  zf

Join

student  as stu

on  stu.s_id=zf.s_id;

2.查询表中所有学生的姓名和对应的英语成绩。

select stu.s_name,c.c_name,s.s_score from

     score as s 

     join

     course as c

     on s.c_id=c.c_id

     join

     student as stu

     on stu.s_id = s.s_id

     where

     c.c_name = 'english';

3. 在所有学生总分数上加10分特长分。

关键代码

  select s_id,sum(s_score)+10 from score group by s_id;
  1. 查询姓名为liyun的学生总成绩
 select sum(s_score) from score where  s_id in (select s_id from student where        s_name='liyun');

查询英语成绩大于90分的同学

 

8.查询总分大于270分的所有同学

 

  1. 查询英语分数在 80-90之间的同学。

 

  1. 查询数学分数为89,90,91的同学。

 

  1. 查询所有姓li的学生平均成绩。

 

  1. 查询数学分>80,语文分>80的同学。

 

  1. 对数学成绩排序后输出。

 

  1. 对总分排序后输出,然后再按从高到低的顺序输出

 

  1.  对姓li的学生数学成绩排序输出

 

 

猜你喜欢

转载自blog.csdn.net/Z_Date/article/details/83997924
今日推荐