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.

      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.

转载于:https://www.cnblogs.com/kouryoushine/p/9313667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL数据库考试试题及答案 题目1:介绍一下MySQL数据库 MySQL是一个开源的关系型数据库管理系统,由瑞典MySQL AB公司开发。它支持多种操作系统,包括Windows、Linux和Unix等,同时也支持多种编程语言,如C、C++、Python和PHP等。MySQL数据库采用客户机/服务器架构,可以处理大量数据同时具有高效性能和可靠性。 题目2:MySQL数据库的数据类型有哪些?请分别举例说明。 MySQL数据库的数据类型包括整型、浮点型、日期时间型、字符型等。例如,整型包括TINYINT、SMALLINT、MEDIUMINT、INT、BIGINT,浮点型包括FLOAT和DOUBLE,日期时间型包括DATE、TIME和DATETIME,字符型包括CHAR、VARCHAR和TEXT等。 题目3:如何创建一个新的数据库? 可以通过MySQL的CREATE DATABASE语句来创建新的数据库,语法如下: CREATE DATABASE database_name; 题目4:如何创建一个新的数据表? 可以使用MySQL的CREATE TABLE语句来创建新的数据表,语法如下: CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ... ); 题目5:如何向数据库中插入新的数据? 可以使用MySQL的INSERT INTO语句向数据库中插入新的数据,语法如下: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); 题目6:如何更新数据库中的数据? 可以使用MySQL的UPDATE语句来更新数据库中的数据,语法如下: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; 题目7:如何删除数据库中的数据? 可以使用MySQL的DELETE FROM语句来删除数据库中的数据,语法如下: DELETE FROM table_name WHERE condition; 题目8:如何查询数据库中的数据? 可以使用MySQL的SELECT语句来查询数据库中的数据,语法如下: SELECT column1, column2, ... FROM table_name WHERE condition; 以上是关于MySQL数据库的考试试题及答案,希望能对大家理解MySQL数据库有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值