**HQL经典练习50题之(一)**

后面25题: https://blog.csdn.net/qq_35954433/article/details/85276223

学生表
create table if not exists t_stu_info(
   stu_id int,
   stu_name string,
   birthday string,
   gender string
   )
   row format delimited
   fields terminated by ' ';

load data local inpath '/home/testdata/stu_info.txt' into table t_stu_info;

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 女

注!以下两条数据为第30题查询同名同性学生时添加,即前29题的操作数据只有前面8条,
后面的21题产生数据结果才会受到以下两条数据的影响

09 郑竹 1989-07-02 女
10 王菊 1990-02-09 女
课程表
create table if not exists t_course(
stu_id int,
course string,
teach_id int
)
row format delimited
fields terminated by '\t';

load data local inpath '/home/testdata/course.txt' into table t_course;

01      语文    02
02      数学    01
03      英语    03
教师表
create table if not exists t_teach(
teach_id int,
teach_name string
)
row format delimited
fields terminated by '\t';

load data local inpath '/home/testdata/teach_info.txt' into table t_teach;

01      张三
02      李四
03      王五
成绩表
create table if not exists t_score(
stu_id int,
course_id int,
score int
)
row format delimited
fields terminated by '\t';

load data local inpath '/home/testdata/score.txt' into table t_score;

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.

load表数据时可根据自己的实际数据路径进行导入

~~~~ 数据就位 进入正题 ~~~~

1、 查询"01"课程比"02"课程成绩高的学生的信息及课程分数:

select stu.*,sc.score,a.score
from t_stu_info stu
join t_score sc on sc.course_id = '01' and sc.stu_id = stu.stu_id
left join t_score a on a.course_id = '02' and a.stu_id = stu.stu_id
join t_score b on b.stu_id = stu.stu_id
where sc.score > a.score or a.score is null
group by stu.stu_id,stu.stu_name,stu.birthday,stu.gender,sc.score,a.score
;

运行结果(02课程未考试也算作低于01课程):

2	钱电		1990-12-21	男	70	60
4	李云		1990-08-06	男	50	30
6	吴兰		1992-03-01	女	31	NULL

2、 查询"01"课程比"02"课程成绩低的学生的信息及课程分数:

select stu.*,sc.score,a.score
from t_stu_info stu
full outer join
  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值