MYSQL子查询例题以及答案
More Subqueries Quizzes
Above is the ERD for the database again - it might come in handy as you tackle the quizzes below. You should write your solution as a subquery or subqueries, not by finding one solution and copying the output. The importance of this is that it allows your query to be dynamic in answering the question - even if the data changes, you still arrive at the right answer.
Provide the name of the sales_rep in each region with the largest amount of total_amt_usd sales.
SELECT * FROM
(SELECT rname rname,MAX(totalsum) totalsum
FROM
(SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
FROM accounts a
JOIN orders o
ON o.account_id = a.id
JOIN sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON r.id = s.region_id
GROUP BY r.name,s.name
ORDER BY 1,3 desc ) sub
GROUP BY rname ) t1
JOIN
(SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
FROM accounts a
JOIN orders o
ON o.account_id = a.id
JOIN sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON r.id = s.region_id
GROUP BY r.name,s.name
ORDER BY 1,3 desc ) t2
ON t1.rname = t2.rname
WHERE t1.totalsum = t2.totalsum METHOD2WITH t1 AS (SELECT rname rname,MAX(totalsum) totalsum
FROM
(SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
FROM accounts a
JOIN orders o
ON o.account_id = a.id
JOIN sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON r.id = s.region_id
GROUP BY r.name,s.name
ORDER BY 1,3 desc ) sub
GROUP BY rname ),t2 AS (SELECT r.name rname,s.name sname,SUM(total_amt_usd) totalsum
FROM accounts a
JOIN orders o
ON o.account_id = a.id
JOIN sales_reps s
ON a.sales_rep_id = s.id
JOIN region r
ON r.id = s.region_id
GROUP BY r.name,s.name
ORDER BY 1,3 desc )SELECT *
FROM t1
JOIN t2
ON t1.rname = t2.rname
WHERE t1.totalsum = t2.totalsum
For the region with the largest (sum) of sales total_amt_usd, how many total (count) orders were placed?
For the name of the account that purchased the most (in total over their lifetime as a customer) standard_qty paper, how many accounts still had more in total purchases?
For the customer that spent the most (in total over their lifetime as a customer) total_amt_usd, how many web_events did they have for each channel?
What is the lifetime average amount spent in terms of total_amt_usd for the top 10 total spending accounts?
What is the lifetime average amount spent in terms of total_amt_usd for only the companies that spent more than the average of all orders.
MYSQL子查询例题以及答案的更多相关文章
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...
- Mysql子查询、关联查询
mysql中update.delete.install尽量不要使用子查询 一.mysql查询的五种子句 where(条件查询).having(筛选).group by(分组).orde ...
- Mysql子查询IN中使用LIMIT
学习下Mysql子查询IN中使用LIMIT的方法. 这两天项目里出了一个问题,mysql LIMIT使用后报错. 需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料 ...
- MySQL 子查询 EXISTS 和 NOT EXISTS(转)
MySQL EXISTS 和 NOT EXISTS 子查询 MySQL EXISTS 和 NOT EXISTS 子查询语法如下: SELECT ... FROM table WHERE EXISTS ...
- mysql子查询慢的问题
当你在用explain工具查看sql语句的运行计划时.若select_type 字段中出现"DEPENDENT SUBQUERY"时,你要注意了.你已经掉入了mysql子查询慢 ...
- MySQL子查询,派生表和通用表达式
一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...
- MySQL子查询慢现象的解决
当你在用explain工具查看sql语句的执行计划时,若select_type 字段中出现“DEPENDENT SUBQUERY”时,你要注意了,你已经掉入了mysql子查询慢的“坑". 相 ...
- MySQL子查询有哪五种形式?
MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,目前属于Oracle旗下产品.MySQL是最流行的关系型数据库管理系统之一,在web应用方面,MySQL是最好的RDBMS(Rela ...
- mysql子查询用法
mysql子查询用法 1 可以当值来用<pre>select id from hcyuyin_share where id=(select id from hcyuyin_share li ...
随机推荐
- C# 获取命名空间对应的程序集位置
由于同名命名空间会被多个程序集使用,C#没有提供直接的方法(对象浏览器也不行)通过命名空间获得程序集位置,这样就不方便找到那些引用文件时什么. 那么可以在立即窗口,中断某个代码的时候,去查询类所在程序 ...
- 《深入理解mybatis原理3》 Mybatis数据源与连接池
<深入理解mybatis原理> Mybatis数据源与连接池 对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构 ...
- hack games
记下,有时间玩玩~ wargame http://www.wechall.net/lang_ranking/en --------------- Monyer系列(黑客游戏) 1. http://mo ...
- 中国剩余定理——nyoj
中国剩余定理------解法如下:假设存在一个数M M%A=a , M%B=b , M%C=c并且A,B,C必须俩俩互质.满足这一条件下:存在一个R1使得 , K1=A*B*R1 ,K1%C==1.存 ...
- gradle 删除指定目录中的文件和目录
// 删除bakAPk下的所有非母包文件 task deleTask(type: Delete){ FileTree tree = fileTree(dir: bakPath) tree.each { ...
- 虚拟机下Redhat9 网络配置问题(转)
原文链接:http://www.programgo.com/article/38031929690/ edhat 9/redhat as 3装在虚拟机vmware上之后,连接网络是出现问题 Deter ...
- EasyPlayer Android基于ffmpeg实现播放(RTSP/RTMP/HTTP/HLS)同步录像功能
之前有博客专门介绍了EasyPlayer的本地录像的功能,简单来说,EasyPlayer是一款RTSP播放器,它将RTSP流里的音视频媒体帧解析出来,并用安卓系统提供的MediaMuxer类进行录像. ...
- js 以函数名作为参数动态执行 函数
function myFunc() { console.log(11111); } test("myFunc"); function test(funcName) { if(typ ...
- Mac安装三方软件
安装提示“xxx软件已损坏,打不开,您应该将它移到废纸篓”的提示,其实并不是软件本身有问题,而是Mac系统的一个安全机制问题步骤1:Spotlight搜索(快捷键:command+空格或右上角搜索的符 ...
- java之对象的前世今生
Tips 对象存在与堆上,实例变量的值存在于对象中.实例变量存在于对象所属的堆空间中. 局部变量与对象方法存在于栈中. 创建对象的时候有如下代码 Dog d = new Dog(); 其中的Dog() ...