You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

How-to-code-summary-queries.sql 965B

123456789101112131415161718192021222324252627282930313233343536
  1. -- Exercise 1
  2. SELECT COUNT(*) AS orders, SUM(tax_amount) AS tax
  3. FROM orders;
  4. -- Exercise 2
  5. SELECT cat.category_name, list_price, COUNT(*) AS product_count
  6. FROM categories cat
  7. JOIN products pro ON cat.category_id = pro.category_id
  8. GROUP BY category_name
  9. ORDER BY product_count DESC
  10. LIMIT 1;
  11. -- Exercise 3
  12. SELECT email_address, SUM(item_price) * quantity AS sum_price, SUM(discount_amount) * quantity AS sum_discount
  13. FROM customers cus
  14. JOIN orders ord ON ord.customer_id = cus.customer_id
  15. JOIN order_items oi ON oi.order_id = ord.order_id
  16. GROUP BY email_address
  17. ORDER BY sum_price DESC;
  18. -- Exercise 4
  19. SELECT email_address, COUNT(*) AS order_count, (item_price - discount_amount) * quantity AS total_amount
  20. FROM customers cus
  21. JOIN orders ord ON cus.customer_id = ord.customer_id
  22. JOIN order_items oid ON ord.customer_id = oid.order_id
  23. GROUP BY email_address HAVING order_count > 1
  24. ORDER BY total_amount DESC;
  25. -- Exercise 5
  26. ???
  27. -- Exercise 6
  28. ???
  29. -- Exercise 7
  30. ???