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 ...
随机推荐
- Java的优势
Java是一种跨平台,适合于分布式计算环境的面向对象编程语言. 具体来说,它具有如下特性: 简单性.面向对象.分布式.解释型.可靠.安全.平台无关.可移植.高性能.多线程.动态性等. 下面我们将重点介 ...
- grunt-init 默认模板目录更正
grunt-init是依赖grunt项目管理的脚手架工具,各种优点无须赘述,默认的template路径的作用:可以把自定义或其他模板放置其中,应用的时候直接调用模板名,不用每次寻找路径: 在安装gru ...
- CSS技巧和经验
如何清除图片下方出现几像素的空白间隙 方法1 img { display: block; } 方法2 除了top值,还可以设置为text-top | middle | bottom | text-bo ...
- ASP.NET经典权限解决方案,适用于OA、CRM、ERP、HR等应用系统
经典权限解决方案 1.权限简介 一般的管理系统都需要对用户的操作进行一定的限制,有的用户可以有许多操作,有的则有少量的操作.这样就需要一个授权机制,基于角色的授权机制描述了某个角色拥有一定数量的操作授 ...
- MVC和EF中的 Model First 和 Code First
准备:先引入MVC和EF的dll包 *命令方法:打开工具——库程序包管理器——程序包管理器控制台,选择自己的项目 a) Install-Package EntityFramework -Ver ...
- log4cpp总结
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- C++11_ Variadic Templates
版权声明:本文为博主原创文章,未经博主允许不得转载. 这次主要介绍C++11的又一个新特性 Variadic Templates (可变模板参数) 它的实现类似于initializer_list< ...
- windows7 下安装python3.6开发环境
所有的软件都放在百度云盘里: 链接: https://pan.baidu.com/s/1rux8sDK9thhbZ1qjwQg6kA 密码: iq4c 1. 安装python3.6.5 安装的时候要把 ...
- Android程序员学WEB前端(5)-HTML(5)-框架集-Sublime
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76576279 觉得博文有用,请点赞,请评论,请关注,谢谢!~ 框架集: index7. ...
- python3精简笔记(二)——函数
函数 下面的地址可以查看函数: https://docs.python.org/3/library/functions.html 也可以在交互式命令行通过help()查看函数的帮助信息. 如: > ...