首页 > 数据库 >sql 练习(hive,spqrk)

sql 练习(hive,spqrk)

时间:2023-07-20 21:47:02浏览次数:30  
标签:string degree spqrk hive score sql where select con

数据准备

表1 课程表(course)

*字段名* *数据类型*
课程编号(CNO) string
课程名称(CNAME) string
教室编号(TNO) string

表2 成绩表(score)

*字段名* *数据类型*
学生编号(SNO) string
课程标号(CNO) string
分数(DEGREE) int

表3 学生表 (student)

*字段名* *数据类型*
学生编号(SNO) string
学生姓名(SNAME) string
学生性别(SSEX) string
出生年月(SBIRTHDAY) string
班级(CLASS) string

表4 教师表(teacher)

*字段名* *数据类型*
教师编号(TNO) string
教师姓名(TNAME) string
性别(TSEX) string
出生年月(TBIRTHDAY) string
职称(PROF) string
系(DEPART) string

数据

course

3-105,计算机导论,825
3-245,操作系统,804
6-166,数据电路,856
9-888,高等数学,100

score

103,3-245,86
105,3-245,75
109,3-245,68
103,3-105,92
105,3-105,88
109,3-105,76
101,3-105,64
107,3-105,91
108,3-105,78
101,6-166,85
107,6-106,79
108,6-166,81

student

108,曾华,男,1977-09-01,95033
105,匡明,男,1975-10-02,95031
107,王丽,女,1976-01-23,95033
101,李军,男,1976-02-20,95033
109,王芳,女,1975-02-10,95031
103,陆君,男,1974-06-03,95031

teacher

804,李诚,男,1958-12-02,副教授,计算机系
856,张旭,男,1969-03-12,讲师,电子工程系
825,王萍,女,1972-05-05,助教,计算机系
831,刘冰,女,1977-08-14,助教,电子工程系

题目

1.在hive数据库里面建立一个名为school的数据库

create database school;

2.在school数据库中建立上面我们需要的表,数据是以‘,’分割的

课程表(course)

create table course(cno string,cname string,ton string)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','STORED AS textfilelocation '/user/hive/warehouse/school.db/course/';

成绩表(score)

create table score(sno string,con string,degree int)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','STORED AS textfilelocation '/user/hive/warehouse/school.db/score/';

学生表 (student)

create table student(sno string,sname string,tsex string,tbirthday string,clazz string)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','STORED AS textfilelocation '/user/hive/warehouse/school.db/student/';

教师表(teacher)

create table teacher(tno string,tname string,ssex string,sbirthday string,prof string,Depart string)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','STORED AS textfilelocation '/user/hive/warehouse/school.db/teacher/';

3.请将数据导入到相应的表里面

load data inpath '/user/course.txt' into table course;load data inpath '/user/score.txt' into table score;load data inpath '/user/student.txt' into table student;load data inpath '/user/teacher.txt' into table teacher;

4.查询Score表中成绩在60到80之间的所有记录

select * from score where degree > 60 and degree < 80;

5.查询Score表中成绩为85,86或88的记录。

select * from score where degree == 85 or degree == 88 or degree ==86;

6.以cno升序、degree降序查询score表的所有记录。

select * from score order by con,degree desc;

7.查询score表中的每门最高分的学生学号和课程号。

select con,sno from score where degree == (select max(degree) from score);

8.查询score表中至少有5名学生选修的并以3开头的课程的平均分数。

select avg(degree) as avg from score where con like '3%' group by con having count(*)>=5;

9.查询最低分大于70,最高分小于90的sno列。

select sno from score group by sno having max(degree)<90 and min(degree)>70;

10.查询所有学生的sname、cno和degree列

select sname,con,degree from score as a join student as b on a.sno=b.sno;

11.查询“95033”班所选课程的平均分。

select avg(degree) from score as a join student as b on a.sno=b.sno group by b.clazz having b.clazz == '95033';

12.查询 score 中选学一门以上课程的同学中分数为非最高分成绩的记录。

select * from (select sno,degree from score where sno in (select sno from (select sno,count(sno) as w from score group by sno having w>1) a)) wjc where degree not in (select max(degree) from score);

13.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

select * from score where degree > (select degree from score where sno'109' and con'3-105');

14.查询“张旭“教师任课的学生成绩。

select * from score where con in (select cno from course where ton in (select tno from teacher where tname == '张旭')) ;

15.查询选修 某 课程的同学人数多于5人的 教师姓名。

//错 select tname from teacher where tno in (select ton from course where cno in (select con from (select con,count(con) as s from score group by con having s>5) s));
//对(麻烦)select t.tname from teacher as t right join (select ton from course where cno in (select con from (select con,count(con) as s from score group by con having s>5) s)) e on t.tno= e.ton;

16.查询存在有85分以上成绩的课程 cno

select con from score where degree > 85;

17.查询各科成绩第一的学生信息

select * from student where sno in (select sno from score where degree in (select max(degree) from score group by con));

18.查询score里面以‘6’开头的所有信息

select * from score where con like '6%';

19.在教师表里面查询职称第二个字是教的信息

select * from teacher where prof like '_教%';

20.在教师表里面查询系这个字段里面含有程的信息

select * from teacher where depart like '%程%';

标签:string,degree,spqrk,hive,score,sql,where,select,con
From: https://www.cnblogs.com/wjc1234/p/17569755.html

相关文章

  • 两个字段相加的值排序 mysql
    实现“两个字段相加的值排序mysql”介绍在MySQL数据库中,我们经常会遇到需要对两个字段相加的值进行排序的需求。这个过程可以通过使用MySQL的ORDERBY语句来实现。在本文中,我将指导你实现这个功能的步骤,并提供相应的代码示例。实现步骤下面是实现“两个字段相加的值排序mysql......
  • 利用mysql存储过程备份数据
    使用MySQL存储过程备份数据的步骤为了实现使用MySQL存储过程备份数据,我们需要完成以下步骤:步骤说明步骤一创建一个存储过程步骤二定义变量和游标步骤三获取需要备份的数据步骤四将备份数据插入到备份表步骤五关闭游标和结束存储过程接下来,我将逐步......
  • 蓝凌OA用的是mySQL
    MySQL简介和应用于蓝凌OA的示例代码1.MySQL简介MySQL是一个开源的关系型数据库管理系统(RDBMS),广泛应用于各种网站和应用程序中。它是最受欢迎的数据库之一,具有以下特点:性能强大:MySQL通过优化查询引擎、索引和缓存等技术提供高效的数据处理能力。可扩展性:MySQL可以轻松......
  • 升级mysql客户端
    升级MySQL客户端MySQL是一种常用的关系型数据库管理系统,它的客户端用于与MySQL服务器进行通信。为了提高性能和功能,我们可能需要升级MySQL客户端。本文将介绍如何升级MySQL客户端以及一些常见的升级错误和解决方法。升级方式升级MySQL客户端有多种方式,最常见的是通过软件包管理......
  • 动态mysql语句
    动态MySQL语句的使用及示例引言在开发过程中,我们经常需要根据不同的条件来构建不同的SQL查询语句。这就需要我们掌握动态MySQL语句的使用方法。动态MySQL语句可以根据不同的情况动态地生成和执行SQL语句,使我们能够灵活地操作数据库。什么是动态MySQL语句动态MySQL语句是指在程......
  • 将其他sql语句转换为mysql
    将其他SQL语句转换为MySQL在日常的数据库开发中,我们经常会遇到需要将其他数据库系统的SQL语句转换为MySQL语句的情况。虽然大部分SQL语句在不同的数据库系统中都有相似的语法,但仍然存在一些差异和特定的函数或关键字。本文将简要介绍如何将其他数据库系统的SQL语句转换......
  • Avalonia 使用EFCore调用SQLite实现Singleton全局注册
    Avalonia使用EFCore调用SQLite实现Singleton全局注册本篇博客是我的开源项目TerraMours.Chat.Ava的更新的记录分享,本次更新使用EntityFrameWorkCore调用SQLite,实现数据的本地化和查询的优化,删除了dbpross类(直接调用SQLite的操作类)。大大提高了代码的简洁度和易读性。通过全局......
  • (_mysql_exceptions.OperationalError) (2061, 'RSA Encryption not supported -
    RSA加密与数据库操作的关系在进行数据库操作时,我们有时会遇到类似于“(_mysql_exceptions.OperationalError)(2061,'RSAEncryptionnotsupported'”的错误提示。这个错误提示通常表示我们正在尝试使用RSA加密算法进行数据库操作,但是数据库不支持RSA加密。本文将介绍RSA加密算......
  • ${hiveconf:dt}
    如何实现${hiveconf:dt}简介在Hive中,${hiveconf:dt}是一种变量的格式,用于表示当前的日期。它用于在Hive查询中动态地获取当前日期,并将其用作查询的一部分。本文将向您介绍如何在Hive中实现${hiveconf:dt}。实现步骤下表展示了实现${hiveconf:dt}的步骤:步骤描述步骤1......
  • MySql 8.0 安装和启动
    1、开发环境:Window10+mysql-8.0.33-win642、下载压缩包+解压(网上找适合自己的版本)3、创建my.ini文件 文件内容:注意安装目录[client]#设置mysql客户端默认字符集default-character-set=utf8[mysqld]#设置3306端口port=3306#设置mysql的安装目录basedir=F:......