Advertisement
weighter

SQL Quiz 1

Dec 13th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.81 KB | None | 0 0
  1. USE my_guitar_shop;
  2.  
  3. -- Chapter 6 Exercise 1
  4. -- ------------------------------
  5.  
  6. SELECT
  7.     COUNT(order_id) as number_of_orders,
  8.     SUM(tax_amount) as tax_amount
  9. FROM
  10.     orders;
  11.  
  12.  
  13. -- Chapter 6 Exercise 2
  14. -- ------------------------------
  15.  
  16. SELECT
  17.     category_name,
  18.     COUNT(*) as product_count,
  19.     MAX(list_price) as most_expensive
  20. FROM
  21.     categories
  22. INNER JOIN
  23.     products p on categories.category_id = p.category_id
  24. GROUP BY
  25.     category_name ;
  26.  
  27.  
  28. -- Chapter 6 Exercise 3
  29. -- -------------------------------
  30.  
  31. SELECT
  32.     email_address,
  33.     SUM(oi.item_price * oi.quantity) AS price,
  34.     SUM(discount_amount*oi.quantity) as discount
  35. FROM
  36.     customers
  37. INNER JOIN
  38.     orders o on customers.customer_id = o.customer_id
  39. INNER JOIN
  40.     order_items oi on o.order_id = oi.order_id
  41. GROUP BY
  42.     email_address
  43. ORDER BY
  44.     price DESC;
  45.  
  46.  
  47. -- Chapter 6 Exercise 4
  48. -- ----------------------------------
  49.  
  50.  
  51. SELECT
  52.     email_address,
  53.     COUNT(DISTINCT o.order_id) as 'orders',
  54.     SUM((item_price - discount_amount)*quantity) as OrderTotal
  55. FROM customers
  56. JOIN orders o on customers.customer_id = o.customer_id
  57. JOIN order_items oi on o.order_id = oi.order_id
  58. GROUP BY email_address
  59. HAVING orders > 1
  60. ORDER BY OrderTotal DESC;
  61.  
  62.  
  63. -- Chapter 6 Exercise 5
  64. -- ----------------------------------
  65.  
  66. SELECT
  67.     email_address,
  68.     COUNT(DISTINCT o.order_id) as 'orders',
  69.     SUM(item_price) - sum(discount_amount) as OrderTotal
  70. FROM customers
  71. JOIN orders o on customers.customer_id = o.customer_id
  72. JOIN order_items oi on o.order_id = oi.order_id
  73. WHERE item_price > 400
  74. GROUP BY email_address
  75. HAVING orders > 1
  76. ORDER BY orders DESC;
  77.  
  78.  
  79. -- Chapter 6 Exercise 6
  80. -- ----------------------------------
  81.  
  82. SELECT
  83.     product_name,
  84.     COUNT(*) as transactions, -- just added this for testing (not in question)
  85.     SUM((item_price - discount_amount)*quantity) as totals
  86. FROM products
  87. JOIN order_items oi on products.product_id = oi.product_id
  88. GROUP BY product_name WITH ROLLUP;
  89.  
  90.  
  91. -- Chapter 6 Exercise 7
  92. -- ----------------------------------
  93.  
  94. SELECT
  95.     email_address,
  96.     COUNT(DISTINCT product_id) as total_products
  97. FROM customers
  98. JOIN orders o on customers.customer_id = o.customer_id
  99. JOIN order_items oi on o.order_id = oi.order_id
  100. GROUP BY email_address
  101. HAVING total_products > 1
  102. ORDER BY email_address ASC;
  103.  
  104.  
  105.  
  106. -- Chapter 6 Exercise 8
  107. -- ----------------------------------
  108.  
  109. SELECT
  110.     IF (GROUPING(category_name) = 1, 'GRAND TOTAL', category_name) as Category,
  111.     IF (GROUPING(product_name) = 1, '-------------->', product_name) as Product,
  112.     SUM(quantity) as total
  113. FROM categories c
  114. JOIN products p on c.category_id = p.category_id
  115. JOIN order_items oi on p.product_id = oi.product_id
  116. GROUP BY c.category_name, p.product_name WITH ROLLUP
  117. HAVING GROUPING(p.product_name) =1;
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement