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 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. -- Exercise 1
  2. SELECT COUNT(order_id) AS order_count, SUM(tax_amount) AS tax_total
  3. FROM orders
  4. -- Exercise 2
  5. SELECT category_name, COUNT(product_id) AS product_count, MAX(list_price) AS most_expensive_product
  6. FROM categories c JOIN products p
  7. ON c.category_id = p.category_id
  8. GROUP BY category_name
  9. ORDER BY category_name DESC
  10. -- Exercise 3
  11. SELECT c.email_address, SUM(item_price) * quantity AS item_price_total, SUM(discount_amount) * quantity AS discount_amount_total
  12. FROM orders o
  13. JOIN order_items i
  14. ON o.order_id = i.order_id
  15. JOIN customers c
  16. ON o.customer_id = c.customer_id
  17. GROUP BY c.customer_id
  18. ORDER BY item_price_total DESC
  19. -- Exercise 4
  20. SELECT c.email_address, COUNT(i.order_id) AS order_count, SUM(item_price - discount_amount) * quantity AS order_total
  21. FROM orders o JOIN order_items i
  22. ON i.order_id = o.order_id
  23. JOIN customers c
  24. ON o.customer_id = c.customer_id
  25. GROUP BY email_address
  26. HAVING COUNT(i.order_id) > 1
  27. ORDER BY SUM(item_price - discount_amount) * COUNT(i.order_id) DESC
  28. -- Exercise 5
  29. -- Exercise 6
  30. -- Exercise 7