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_querries.sql 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*1*/
  2. SELECT
  3. count(*) AS order_count,
  4. SUM(tax_amount) AS tax_total
  5. FROM
  6. orders;
  7. /*2*/
  8. SELECT
  9. category_name,
  10. COUNT(product_id) AS product_count,
  11. MAX(list_price) AS most_expensive_product
  12. FROM
  13. products p
  14. JOIN
  15. categories c
  16. ON
  17. p.category_id = c.category_id
  18. GROUP BY
  19. category_name
  20. ORDER BY COUNT(product_id) DESC, category_name DESC;
  21. /*3*/
  22. SELECT
  23. email_address,
  24. SUM(item_price) * quantity AS item_price_total ,
  25. SUM(discount_amount) * quantity AS discount_amount_total
  26. FROM
  27. orders o
  28. JOIN
  29. order_items oi
  30. ON
  31. o.order_id = oi.order_id
  32. JOIN
  33. customers c
  34. ON
  35. o.customer_id = c.customer_id
  36. GROUP BY
  37. c.customer_id
  38. ORDER BY
  39. SUM(item_price) * quantity DESC;
  40. /*4*/
  41. SELECT
  42. c.email_address,
  43. COUNT(oi.order_id) AS 'order_count',
  44. SUM(item_price - discount_amount) * quantity AS 'order_total'
  45. FROM orders ord JOIN order_items oi
  46. ON o.order_id = oi.order_id
  47. JOIN customers c
  48. ON o.customer_id = c.customer_id
  49. GROUP BY email_address
  50. HAVING COUNT(oi.order_id) > 1
  51. ORDER BY SUM(item_price - discount_amount) DESC, COUNT(oi.order_id);
  52. /*5*/
  53. SELECT
  54. email_address,
  55. COUNT(o.order_id) AS 'order_count',
  56. SUM(item_price - discount_amount) * quantity AS 'order_total'
  57. FROM orders o
  58. JOIN order_items oi
  59. ON o.order_id = oi.order_id
  60. JOIN customers c
  61. ON o.customer_id = c.customer_id
  62. WHERE oi.item_price > 400
  63. GROUP BY email_address
  64. HAVING COUNT(oi.order_id) > 1
  65. ORDER BY SUM(item_price - discount_amount) * COUNT(oi.order_id) DESC;
  66. /*6*/ /* kan het dat 1 bedrag niet klopt*/
  67. SELECT
  68. product_name,
  69. SUM(item_price - discount_amount) * quantity AS 'Total Amount'
  70. FROM
  71. products p
  72. JOIN
  73. order_items o
  74. ON
  75. p.product_id = o.product_id
  76. GROUP BY
  77. product_name WITH ROLLUP;
  78. /*7*/
  79. SELECT
  80. email_address,
  81. COUNT(product_id) AS 'number_of_products'
  82. FROM
  83. customers c
  84. JOIN
  85. orders o
  86. ON
  87. c.customer_id = o.customer_id
  88. JOIN
  89. order_items ord
  90. ON
  91. o.order_id = ord.order_id
  92. GROUP BY
  93. email_address
  94. HAVING
  95. COUNT(o.order_id) > 1;