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.

      1. 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 METHOD2

        WITH 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

    1. For the region with the largest (sum) of sales total_amt_usd, how many total (count) orders were placed?

    2. 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?

    3. 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?

    4. What is the lifetime average amount spent in terms of total_amt_usd for the top 10 total spending accounts?

    5. 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子查询例题以及答案的更多相关文章

  1. [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时

    案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...

  2. Mysql子查询、关联查询

    mysql中update.delete.install尽量不要使用子查询 一.mysql查询的五种子句         where(条件查询).having(筛选).group by(分组).orde ...

  3. Mysql子查询IN中使用LIMIT

    学习下Mysql子查询IN中使用LIMIT的方法. 这两天项目里出了一个问题,mysql LIMIT使用后报错. 需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料 ...

  4. MySQL 子查询 EXISTS 和 NOT EXISTS(转)

    MySQL EXISTS 和 NOT EXISTS 子查询 MySQL EXISTS 和 NOT EXISTS 子查询语法如下: SELECT ... FROM table WHERE EXISTS ...

  5. mysql子查询慢的问题

      当你在用explain工具查看sql语句的运行计划时.若select_type 字段中出现"DEPENDENT SUBQUERY"时,你要注意了.你已经掉入了mysql子查询慢 ...

  6. MySQL子查询,派生表和通用表达式

    一:子查询 1.介绍 在另一个查询(外部查询)中嵌套另一个查询语句(内部查询),并使用内部查询的结果值作为外部查询条件. 2.子查询在where中 SELECT customerNumber, che ...

  7. MySQL子查询慢现象的解决

    当你在用explain工具查看sql语句的执行计划时,若select_type 字段中出现“DEPENDENT SUBQUERY”时,你要注意了,你已经掉入了mysql子查询慢的“坑". 相 ...

  8. MySQL子查询有哪五种形式?

    MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,目前属于Oracle旗下产品.MySQL是最流行的关系型数据库管理系统之一,在web应用方面,MySQL是最好的RDBMS(Rela ...

  9. mysql子查询用法

    mysql子查询用法 1 可以当值来用<pre>select id from hcyuyin_share where id=(select id from hcyuyin_share li ...

随机推荐

  1. python 爬虫005-爬虫实例

    实例一:扒取猫眼电影TOP100 的信息 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 扒取猫眼电影TOP100 的 ...

  2. 在接口请求时报错Unrecognized field &quot;zZF1&quot

    这个问题是json序列化问题,当参数中出现大写字母组成的字段时(例如:ZZF1),此时需在字段上加入注解:@JsonProperty(value = "ZZF1")

  3. JS中call和apply区别有哪些 记录

    一.call和apply区别 传递参数的方式.用法上不同,主要是参数不完全同 (1).B.Function.call(A,arg,arg) 这个例子是讲A对象“调用”B对象的Function(某个具体 ...

  4. 20165202 学习基础和c语言基础调查

    你有什么技能比大多人(超过90%以上)更好? 我对自行车运动的兴趣始于初中时期,不敢说比大多数人更好,但在业余爱好者中相对来说还不错. 针对这个技能的获取你有什么成功的经验? 接触自行车运动几年里,我 ...

  5. 如何在 js 代码中使用 jsp 标签或 Java 代码

    JSP 标签还是很方便的,比如 Struts.Spring 等提供给我们的 JSP 标签,可以用它们来获取变量或进行一些计算.比如 struts2 的 <s:url value="/a ...

  6. Gcov 详解 + 内核函数覆盖率测试方法详述及产生错误解决办法

    http://blog.csdn.net/wangyezi19930928/article/details/42638345 http://www.uml.org.cn/Test/201208311. ...

  7. jmeter返回报文乱码问题

    返回的报文中存在乱码如下: 1.先改脚本里面的 content encoding为utf-8 然后response为utf-8 如果以上还是不可以,那就改配置文件jmeter.properties,里 ...

  8. HDU 2273

    http://acm.hdu.edu.cn/showproblem.php?pid=2273 N辆车排队过马路,不能相撞,问最短时间 ans=车的总长度/最小速度 #include <iostr ...

  9. OkHttp使用Get和Post访问服务器的工具类(一)

    首先来简单介绍一下okttp框架,类似于HttpUrlConnection,Android6.0以后,废弃了Apache Http Client,只有HttpUrlConnection和OkHttp了 ...

  10. "PEP:8 expected 2 blank lines ,found 1"

    pycharm shows "PEP:8 expected 2 blank lines ,found 1" 用pycharm写python的时候,总会在def function() ...