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.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. -- Exercise 1 --
  2. SELECT
  3. COUNT(order_id),
  4. SUM(tax_amount)
  5. FROM orders;
  6. -- Exercise 2 --
  7. SELECT
  8. category_name,
  9. COUNT(product_id),
  10. MAX(list_price)
  11. FROM categories c JOIN products p
  12. ON c.category_id = p.category_id
  13. GROUP BY category_name
  14. ORDER BY category_name DESC;
  15. -- Exercise 3 --
  16. SELECT
  17. email_address,
  18. SUM(item_price) * quantity,
  19. SUM(discount_amount) * quantity
  20. FROM orders ord JOIN order_items o
  21. ON o.order_id = ord.order_id
  22. JOIN customers c
  23. ON ord.customer_id = c.customer_id
  24. GROUP BY c.customer_id
  25. ORDER BY item_price DESC;
  26. -- Exercise 4 --
  27. SELECT
  28. c.email_address AS 'Email',
  29. COUNT(o.order_id) AS 'Order Count',
  30. SUM(item_price - discount_amount) * quantity AS 'Total Amount'
  31. FROM orders ord JOIN order_items o
  32. ON o.order_id = ord.order_id
  33. JOIN customers c
  34. ON ord.customer_id = c.customer_id
  35. GROUP BY email_address
  36. HAVING COUNT(o.order_id) > 1
  37. ORDER BY SUM(item_price - discount_amount) * COUNT(o.order_id) DESC;
  38. -- Exercise 5 --
  39. SELECT
  40. email_address,
  41. COUNT(o.order_id),
  42. SUM(item_price - discount_amount) * quantity AS 'Total Amount'
  43. FROM orders ord JOIN order_items o
  44. ON o.order_id = ord.order_id
  45. JOIN customers c
  46. ON ord.customer_id = c.customer_id
  47. WHERE o.item_price > 400
  48. GROUP BY email_address
  49. HAVING COUNT(o.order_id) > 1
  50. ORDER BY SUM(item_price - discount_amount) * COUNT(o.order_id) DESC;
  51. -- oef niet juist, 1 bedrag klopt niet. Ik vind de fout niet --
  52. -- Exercise 6 --
  53. SELECT
  54. product_name,
  55. SUM(item_price - discount_amount) * quantity AS 'Total Amount'
  56. FROM products p JOIN order_items ord
  57. ON p.product_id = ord.product_id
  58. GROUP BY product_name WITH ROLLUP;
  59. -- Exercise 7 --
  60. SELECT
  61. email_address,
  62. COUNT(product_id) AS number_of_products
  63. FROM customers c JOIN orders o
  64. ON c.customer_id = o.customer_id
  65. JOIN order_items ord
  66. ON o.order_id = ord.order_id
  67. GROUP BY email_address
  68. HAVING COUNT(o.order_id) > 1;