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.

Solution Exercises.sql 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* How to retrieve data from a single table*/
  2. /*1*/
  3. SELECT
  4. product_code,
  5. product_name,
  6. list_price,
  7. discount_percent
  8. FROM
  9. products
  10. ORDER BY list_price DESC;
  11. /*2*/
  12. SELECT
  13. concat(last_name,', ', first_name) AS full_name
  14. FROM
  15. customers
  16. WHERE
  17. last_name >= 'M%'
  18. ORDER BY
  19. last_name;
  20. /*3*/
  21. SELECT
  22. product_name,
  23. list_price,
  24. date_added
  25. FROM
  26. products
  27. WHERE
  28. list_price > 500 AND list_price < 2000;
  29. /*4*/
  30. SELECT
  31. product_name,
  32. list_price,
  33. discount_percent,
  34. ROUND((list_price*discount_percent/100,2) AS discount_amount,
  35. ROUND(list_price - list_price*discount_percent/100,2)AS discount_price
  36. FROM
  37. products
  38. ORDER BY
  39. ROUND(list_price -(list_price*(discount_percent/100)),2 DESC
  40. LIMIT 5;
  41. /*5*/
  42. SELECT
  43. item_id,
  44. item_price,
  45. discount_amount,
  46. quantity,
  47. (item_price * quantity) AS price_total,
  48. (discount_amount * quantity) AS discount_total,
  49. ((item_price - discount_amount) * quantity) AS item_total
  50. FROM
  51. order_items
  52. WHERE
  53. ((item_price - discount_amount) * quantity) > 500
  54. ORDER BY
  55. item_total DESC;
  56. /*6*/
  57. SELECT
  58. order_id,
  59. order_date,
  60. ship_date
  61. FROM
  62. orders
  63. WHERE ship_date IS NULL;
  64. /*7*/
  65. SELECT
  66. now() AS today_unformatted,
  67. date_format(now(),'%d-%b-%Y') AS today_formatted;
  68. /*8*/
  69. SELECT
  70. 100 AS price,
  71. .07 AS tax_rate,
  72. (100 * 0.07) AS tax_amount,
  73. 100 + (100 * 0.07) AS total;
  74. /* HOW TO RETRIEVE DATA FROM TWO OR MORE TABLES */
  75. /*1*/
  76. SELECT
  77. category_name,
  78. product_name,
  79. list_price
  80. FROM
  81. products p
  82. JOIN
  83. categories c
  84. ON
  85. c.category_id = p.category_id
  86. ORDER BY
  87. category_name,
  88. product_name;
  89. /*2*/
  90. SELECT
  91. first_name,
  92. last_name,
  93. line1,
  94. city,
  95. state,
  96. zip_code
  97. FROM
  98. addresses a
  99. JOIN
  100. customers c
  101. ON
  102. a.customer_id = c.customer_id
  103. WHERE
  104. email_address LIKE "allan.sherwood%";
  105. /*3*/
  106. SELECT
  107. first_name,
  108. last_name,
  109. line1,
  110. city,
  111. state,
  112. zip_code
  113. FROM
  114. addresses a
  115. JOIN
  116. customers c
  117. ON
  118. a.customer_id = c.customer_id
  119. WHERE
  120. address_id = shipping_address_id;
  121. /*4*/
  122. SELECT
  123. last_name,
  124. first_name,
  125. order_date,
  126. product_name,
  127. item_price,
  128. discount_amount,
  129. quantity
  130. FROM
  131. customers c
  132. JOIN
  133. orders o
  134. ON
  135. c.customer_id = o.customer_id
  136. JOIN
  137. order_items i
  138. ON
  139. o.order_id = i.order_id
  140. JOIN
  141. products p
  142. ON
  143. i.product_id = p.product_id
  144. ORDER BY
  145. last_name, order_date, product_name;
  146. /*5*/
  147. SELECT
  148. p1.product_name,
  149. p2.list_price
  150. FROM
  151. products p1
  152. JOIN
  153. products p2
  154. ON
  155. p1.product_id <> p2.product_id
  156. AND p1.list_price = p2.list_price;
  157. /*6*/
  158. SELECT
  159. category_name,
  160. product_id
  161. FROM
  162. categories c
  163. LEFT OUTER JOIN
  164. products p
  165. ON
  166. c.category_id = p.category_id
  167. WHERE
  168. product_id IS NULL;
  169. /*7*/
  170. SELECT
  171. 'SHIPPED' AS ship_status,
  172. order_id,
  173. order_date
  174. FROM
  175. orders
  176. WHERE
  177. ship_date IS NOT NULL
  178. UNION
  179. SELECT
  180. 'NOT SHIPPED' AS ship_status,
  181. order_id,
  182. order_date
  183. FROM
  184. orders
  185. WHERE
  186. ship_date IS NULL
  187. ORDER BY
  188. order_date;
  189. /* HOW TO CODE SUMMARY QUERRIES*/
  190. /*1*/
  191. SELECT
  192. count(*) AS count_number_of_orders,
  193. SUM(tax_amount) AS sum_tax_amounts
  194. FROM
  195. orders;
  196. /*2*/
  197. SELECT
  198. category_name,
  199. COUNT(*) AS count_of_the_products,
  200. MAX(list_price)
  201. FROM
  202. products p
  203. JOIN
  204. categories c
  205. ON
  206. p.category_id = c.category_id
  207. GROUP BY
  208. category_name
  209. ORDER BY COUNT(*) DESC;
  210. /*3*/
  211. SELECT
  212. email_adress
  213. SELECT category_name FROM categories;