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_retrive_data_from_two_or_more_tables.sql 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*1*/
  2. SELECT
  3. category_name,
  4. product_name,
  5. list_price
  6. FROM
  7. products p
  8. JOIN
  9. categories c
  10. ON
  11. c.category_id = p.category_id
  12. ORDER BY
  13. category_name,
  14. product_name;
  15. /*2*/
  16. SELECT
  17. first_name,
  18. last_name,
  19. line1,
  20. city,
  21. state,
  22. zip_code
  23. FROM
  24. addresses a
  25. JOIN
  26. customers c
  27. ON
  28. a.customer_id = c.customer_id
  29. WHERE
  30. email_address LIKE "allan.sherwood%";
  31. /*3*/
  32. SELECT
  33. first_name,
  34. last_name,
  35. line1,
  36. city,
  37. state,
  38. zip_code
  39. FROM
  40. addresses a
  41. JOIN
  42. customers c
  43. ON
  44. a.customer_id = c.customer_id
  45. WHERE
  46. address_id = shipping_address_id;
  47. /*4*/
  48. SELECT
  49. last_name,
  50. first_name,
  51. order_date,
  52. product_name,
  53. item_price,
  54. discount_amount,
  55. quantity
  56. FROM
  57. customers c
  58. JOIN
  59. orders o
  60. ON
  61. c.customer_id = o.customer_id
  62. JOIN
  63. order_items i
  64. ON
  65. o.order_id = i.order_id
  66. JOIN
  67. products p
  68. ON
  69. i.product_id = p.product_id
  70. ORDER BY
  71. last_name, order_date, product_name;
  72. /*5*/
  73. SELECT
  74. p1.product_name,
  75. p2.list_price
  76. FROM
  77. products p1
  78. JOIN
  79. products p2
  80. ON
  81. p1.product_id <> p2.product_id
  82. AND p1.list_price = p2.list_price
  83. ORDER BY product_name;
  84. /*6*/
  85. SELECT
  86. category_name,
  87. product_id
  88. FROM
  89. categories c
  90. LEFT OUTER JOIN
  91. products p
  92. ON
  93. c.category_id = p.category_id
  94. WHERE
  95. product_id IS NULL;
  96. /*7*/
  97. SELECT
  98. 'SHIPPED' AS ship_status,
  99. order_id,
  100. order_date
  101. FROM
  102. orders
  103. WHERE
  104. ship_date IS NOT NULL
  105. UNION
  106. SELECT
  107. 'NOT SHIPPED' AS ship_status,
  108. order_id,
  109. order_date
  110. FROM
  111. orders
  112. WHERE
  113. ship_date IS NULL
  114. ORDER BY
  115. order_date;