sql语句练习:

https://blog.csdn.net/mrbcy/article/details/68965271 完成。

https://blog.csdn.net/flycat296/article/details/63681089

hive语句综合练习

以下网址中的表为背景表,完成以下练习题:

https://blog.csdn.net/mrbcy/article/details/68965271

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

SELECT a.* ,b.s_score AS 01_score,c.s_score AS 02_score FROM

student a

LEFT JOIN score b ON a.s_id=b.s_id AND b.c_id='01'

LEFT JOIN score c ON a.s_id=c.s_id AND c.c_id = '02'  WHERE b.s_score>c.s_score;

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

SELECT a.* ,b.s_score AS 01_score,c.s_score AS 02_score FROM

student a LEFT JOIN score b ON a.s_id=b.s_id AND b.c_id='01'

JOIN score c ON a.s_id=c.s_id AND c.c_id='02' WHERE b.s_score<c.s_score;

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT b.s_id,b.s_name,ROUND(AVG(a.s_score),2) AS avg_score FROM

student b

JOIN score a ON b.s_id = a.s_id

GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

-- (包括有成绩的和无成绩的)

SELECT b.s_id,b.s_name,ROUND(AVG(a.s_score),2) AS avg_score FROM

student b

LEFT JOIN score a ON b.s_id = a.s_id

GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60

UNION ALL

SELECT a.s_id,a.s_name,0 AS avg_score FROM

student a

WHERE a.s_id NOT IN (

SELECT DISTINCT s_id FROM score);

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT a.s_id,a.s_name,COUNT(b.c_id) AS sum_course,SUM(b.s_score) AS sum_score FROM

student a

LEFT JOIN score b ON a.s_id=b.s_id

GROUP BY a.s_id,a.s_name;

-- 6、查询"李"姓老师的数量

select count(t_id) from techer where t_name like '李%';

-- 7、查询学过"张三"老师授课的同学的信息

SELECT a.*

FROM student a LEFT JOIN score b ON a.s_id = b.s_id WHERE b.c_id  IN  (

SELECT c.c_id

FROM course c LEFT JOIN techer t ON c.t_id = t.t_id WHERE t.t_name = '张三'

) ;

-- 8、查询没学过"张三"老师授课的同学的信息

SELECT s.*

FROM student s LEFT JOIN (

SELECT a.s_id

FROM student a LEFT JOIN score b ON a.s_id = b.s_id WHERE b.c_id  IN  (

SELECT c.c_id

FROM course c LEFT JOIN techer t ON c.t_id = t.t_id WHERE t.t_name = '张三'

)

) ss ON s.s_id = ss.s_id WHERE ss.s_id IS NULL;

-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select a.* from

student a,score b,score c

where a.s_id = b.s_id  and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT qq.*

FROM (

SELECT s.*

FROM student s LEFT JOIN score sco ON s.s_id = sco.s_id LEFT JOIN course c ON sco.c_id = c.c_id WHERE c.c_id='01'

) qq

LEFT JOIN (

SELECT stu.*

FROM student stu LEFT JOIN score mysco ON stu.s_id = mysco.s_id LEFT JOIN course cou ON mysco.c_id = cou.c_id WHERE cou.c_id='02'

) pp

ON qq.s_id = pp.s_id WHERE pp.s_id IS NULL;

-- 11、查询没有学全所有课程的同学的信息

SELECT ss.s_id

FROM (

SELECT stu.s_id,COUNT(stu.s_id) AS num

FROM student stu LEFT JOIN score sco ON stu.s_id = sco.s_id LEFT JOIN course cou ON sco.c_id = cou.c_id

GROUP BY stu.s_id

) ss WHERE ss.num < 3

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT stu.*

FROM student stu LEFT JOIN

(

SELECT s.s_id

FROM score s WHERE s.c_id IN(

SELECT  c_id FROM score WHERE s_id = '01'

)GROUP BY s_id

) pp ON stu.s_id = pp.s_id WHERE pp.s_id IS NOT  NULL;

mysql的sql语句练习的2个网址的更多相关文章

  1. Mysql 常用 SQL 语句集锦

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  2. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  3. MySQL数据库sql语句的一些简单优化

    1.查询条件的先后顺序 有多个查询条件时,要把效率高能更精确筛选记录的条件放在后边.因为MySQL解析sql语句是从后往前的(不知是否准确). 例: select a.*,b.* from UsrIn ...

  4. mysql下sql语句 update 字段=字段+字符串

    mysql下sql语句 update 字段=字段+字符串   mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...

  5. MySQL数据库SQL语句基本操作

    一.用户管理: 创建用户: create user '用户名'@'IP地址' identified by '密码'; 删除用户: drop user '用户名'@'IP地址'; 修改用户: renam ...

  6. mysql执行sql语句过程

    开发人员基本都知道,我们的数据存在数据库中(目前最多的是mysql和oracle,由于作者更擅长mysql,所以这里默认数据库为mysql),服务器通过sql语句将查询数据的请求传入到mysql数据库 ...

  7. MySQL与SQL语句的操作

    MySQL与SQL语句的操作 Mysql比较轻量化,企业用的是Oracle,基本的是熟悉对数据库,数据表,字段,记录的更新与修改 1. mysql基本信息 特殊数据库:information_sche ...

  8. mysql 操作sql语句 目录

    mysql 操作sql语句 操作数据库 mysql 操作sql语句 操作数据表 mysql 操作sql语句 操作数据表中的内容/记录

  9. 不登录到MySQL执行SQL语句

    mysql -e 不登录到MySQL执行SQL语句 mysql -u root -p -e "SHOW DATABASES"

随机推荐

  1. 是时候开刷NOI了

    整天挨着毛爷爷,压力好大.. 看毛爷爷即将炖完NOI,我的确也该刷了 原则是从头到尾自己想(虽然看了一次题解),可以不A掉. NOI2009 day1: T1 题目略神,我还是不讲了...(就这题我W ...

  2. Vue-i18n实现语言切换

    方法1 Vue — i18n 国际化 全局配置 安 装 1.直接引入js文件 <script src="https://unpkg.com/vue/dist/vue.js"& ...

  3. 浅谈Thrift内部实现原理

    http://dongxicheng.org/tag/thrift/ http://dongxicheng.org/search-engine/thrift-internals/ Thrift由两部分 ...

  4. python __builtins__ classmethod类 (11)

    11.'classmethod', 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等. class cla ...

  5. video.py OpenCv例程阅读

    #!/usr/bin/env python ''' Video capture sample. Sample shows how VideoCapture class can be used to a ...

  6. stylus基础教程,stylus实例教程,stylus语法总结

    stylus特点富于表现力.具有健壮性.功能丰富.动态编码不需要写CSS的冒号.分号.大括号和LESS.SASS功能类似,会这些的入手很快stylus特点安装使用stylus语法(一)选择器(二)变量 ...

  7. Contextual Action bar(1) CAB in Android

    Contextual Action bar (CAB) in Android BY PARESH MAYANI - OCTOBER, 23RD 2013 Before getting into the ...

  8. 常见的问题:https://localhost:1158/em 无法打开

    常见的问题:https://localhost:1158/em 无法打开 安装了Oracle Database 11g,里面有个基于网页的管理工具“https://localhost:1158/em” ...

  9. AO-XXXX

    一 AO4419:应用于开关应用或PWM应用的场效应管.

  10. 在solr客户端删除库中的数据

    1.在solr客户端,访问你的索引库(我认为最方便的方法) 1)documents type 选择 XML 2)documents 输入下面语句<delete><query>* ...