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-retrieve-data-from-two-or-more-tables.sql 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- Exercise 1
  2. SELECT
  3. c.category_name,
  4. p.product_name,
  5. p.list_price
  6. FROM
  7. categories c JOIN products p
  8. ON c.category_id = p.category_id
  9. ORDER BY
  10. category_name, product_name ASC;
  11. -- Exercise 2
  12. SELECT
  13. first_name,
  14. last_name,
  15. line1,
  16. city,
  17. state,
  18. zip_code
  19. FROM
  20. customers c JOIN addresses a
  21. ON c.customer_id = a.customer_id
  22. WHERE
  23. c.email_address = 'allan.sherwood@yahoo.com';
  24. -- Exercise 3
  25. SELECT
  26. first_name,
  27. last_name,
  28. line1,
  29. city,
  30. state,
  31. zip_code
  32. FROM
  33. customers c JOIN addresses a
  34. ON c.customer_id = a.customer_id
  35. AND c.shipping_address_id = a.address_id;
  36. -- Exercise 4
  37. SELECT
  38. c.last_name,
  39. c.first_name,
  40. order_date,
  41. p.product_name,
  42. item_price,
  43. discount_amount,
  44. quantity
  45. FROM
  46. Customers c
  47. JOIN orders o ON c.customer_id = o.customer_id
  48. JOIN order_items oi ON o.order_id = oi.order_id
  49. JOIN products p ON oi.product_id = p.product_id
  50. ORDER BY
  51. last_name, order_date, product_name;
  52. -- Exercise 5
  53. SELECT
  54. p1.product_name,
  55. p1.list_price
  56. FROM
  57. products p1 JOIN products p2
  58. ON p1.product_id <> p2.product_id AND
  59. p1.list_price = p2.list_price
  60. ORDER BY
  61. product_name;
  62. -- Exercise 6
  63. SELECT
  64. category_name,
  65. product_id
  66. FROM
  67. categories c LEFT JOIN products p
  68. ON c.category_id = p.category_id
  69. WHERE
  70. product_id IS NULL;
  71. -- Exercise 7
  72. SELECT
  73. 'SHIPPED' AS ship_status,
  74. order_id,
  75. order_date
  76. FROM
  77. orders
  78. WHERE
  79. ship_date IS NOT NULL
  80. UNION
  81. SELECT
  82. 'NOT SHIPPED',
  83. order_id,
  84. order_date
  85. FROM
  86. orders
  87. WHERE
  88. ship_date IS NULL
  89. ORDER BY
  90. order_date;