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. poj-2342-简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10388   Accepted: 594 ...

  2. UVA-1615 Highway (贪心,区间选点)

    题目大意:有一条沿x轴正方向,长为L的高速公路,n个村庄,要求修建最少的公路出口数目,使得每个村庄到出口的距离不大于D. 题目分析:区间选点问题.在x轴上,到每个村庄距离为D的点有两个(超出范围除外) ...

  3. 【收藏】SQL多行变一列

    CREATE TABLE DEPT (DeptNo INT IDENTITY(1, 1)NOT NULL ,  Country VARCHAR(50) ,  Location VARCHAR(50) ...

  4. Nginx启动/重启失败

    解决方案: Nginx启动或重启失败,一般是因为配置文件出错了,我们可以使用nginx -t方法查看配置文件出错的地方.也可以通过查看Nginx日志文件定位到Nginx重启失败的原因,Nginx日志文 ...

  5. linux命令权限

    linux-命令权限 1)   新建用户natasha,uid为1000,gid为555,备注信息为“master” 2)   修改natasha用户的家目录为/Natasha 3)   查看用户信息 ...

  6. MYSQL-实现ORACLE 和SQLserver数据中- row_number() over(partition by ) 分组排序功能

    网上看见了好多例子都基本上是一样的,没有过多的解释,对于一个初学MySQL来说有点难,我把部分转摘过来如下 原文:http://www.cnblogs.com/buro79xxd/archive/20 ...

  7. 【WebGL】3. 相机

    相机的种类:WebGL中的相机有两种:正投影相机和透视相机 1. 正投影相机OrthographicCamera:类似于工程图纸中的视角,忽略远近距离,远近的物体比例不变,多用于科学研究,工程图纸的应 ...

  8. 复位compiz和unity

    compiz是最最不稳定的组件....绝大部分死机(图形界面没反应)都是由于这货. 所以为了我们系统的稳定,最好不要蛋疼去修改compiz的配置添加神马3D特效,这样导致更不稳定,这样做之后估计你一整 ...

  9. 关于PHP页面显示乱码问题的解决

    关于PHP页面显示乱码问题的解决 网页乱码一直是网络编程高手都头痛的问题,我是一个PHP Web编程的初学者,学习当中也遇到了这个问题,查找了相关的资源,总结如下: 一般的中文编码:gb2312,gb ...

  10. python marshal 对象序列化和反序列化

    有时候,要把内存中的一个对象持久化保存到磁盘上,或者序列化成二进制流通过网络发送到远程主机上.Python中有很多模块提供了序列化与反序列化的功能,如:marshal, pickle, cPickle ...