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. Oracle登录被拒绝; 权限不足或用户名/口令无效

    第一步: 打开CMD命令窗,输入如下命令:sqlplus "/as sysdba",回车 第二步: 输入命令:alter user sys identified by Orcl12 ...

  2. HDU-2196-树形dp/计算树上固定起点的最长路

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. Centos7 使用Dockerfile 制作自己的Dotnetcore程序镜像

    准备Centos7环境及Docker环境 从Docker hub拉取 Microsoft/dotnet 基础镜像(可以使用国内加速) 向Centos7指定目录上传Dotnet Core程序,目录: / ...

  4. OpenStack Mitaka Neutron SR-IOV配置

    ### 一.在所有节点(控制节点.计算节点) 1.修改BIOS ``` BOIS里面开启SR-IOV功能 开启 VT-d (inter virtualization technology)和 SR-I ...

  5. 使用aidl的项目结构以及小的注意事项

    在app的build.gradle里面添加: sourceSets{ main{ java.srcDirs = ['src/main/java','src/main/aidl'] } }

  6. Django中构造响应对象的方式

    1 HttpResponse 可以使用django.http.HttpResponse来构造响应对象. HttpResponse(content=响应体, content_type=响应体数据类型, ...

  7. LINUX系统下的数据库的管理

    环境:配置好IP和YUM源  一.数据库的安装及密码的修改 [1]yum  install  mariadb-server  -y       ##安装mariadb数据库 [2]systemctl  ...

  8. 用JQuery怎么去一次性获取 aspTextBox 文本框的值

    如:<asp:TextBox ID="txtSubject" runat="server">aa</asp:TextBox> 第一次用服 ...

  9. React-Native基础_4.View组件

    View组件 对应ios 的UIView android 中的view 使用要先导入View import { View } from 'react-native'; 使用就是View标签,可以添加S ...

  10. NodeJs 基础知识

    1.网站 http://nodejs.cn/ 下载最新NodeJs并且安装2. 你可以输入一个新命令“node”.使用该“node”命令有两种不同的方法.第一种不带任何参数,将打开一个交互式Shell ...