Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

How-to-retrieve-data-from-a-single-table.sql 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. -- Exercise 1
  2. SELECT
  3. product_code, product_name, list_price, discount_percent
  4. FROM
  5. products
  6. ORDER BY
  7. list_price DESC;
  8. -- Exercise 2
  9. SELECT
  10. concat(last_name, ', ', first_name) AS full_name
  11. FROM
  12. customers
  13. WHERE
  14. last_name >= 'M'
  15. ORDER BY
  16. last_name ASC;
  17. -- Exercise 3
  18. SELECT
  19. product_name, list_price, date_added
  20. FROM
  21. products
  22. WHERE
  23. list_price > 500 AND list_price < 2000;
  24. ORDER BY
  25. date_added DESC;
  26. -- Exercise 4
  27. SELECT
  28. product_name,
  29. list_price,
  30. discount_percent,
  31. ROUND(list_price * discount_percent / 100) AS discount_amount,
  32. ROUND(list_price - (list_price * discount_percent), 2) AS discount_price
  33. FROM
  34. products
  35. ORDER BY
  36. discount_price DESC
  37. LIMIT
  38. 5;
  39. -- Exercise 5
  40. SELECT
  41. item_id,
  42. item_price,
  43. discount_amount,
  44. quantity,
  45. item_price * quantity AS price_total,
  46. discount_amount * quantity AS discount_total,
  47. (item_price - discount_amount) * quantity AS item_total
  48. FROM
  49. order_items
  50. WHERE
  51. (item_price - discount_amount) * quantity >= 500
  52. ORDER BY
  53. item_total DESC;
  54. -- Exercise 6
  55. SELECT
  56. order_id,
  57. order_date,
  58. ship_date
  59. FROM
  60. orders
  61. WHERE
  62. ship_date IS NULL;
  63. -- Exercise 7
  64. SELECT
  65. NOW() AS today_unformatted,
  66. DATE_FORMAT(NOW(), "%d-%m-%Y") AS today_formatted;
  67. -- Exercise 8
  68. SELECT
  69. '100' AS 'price',
  70. '0.07' AS 'tax_rate',
  71. 100 * .07 AS tax_amount,
  72. 100 + (100 * .07) AS total;