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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. -- Exercise 1 --
  2. SELECT
  3. category_name,
  4. product_name,
  5. list_price
  6. FROM products p JOIN categories c
  7. ON p.category_id = c.category_id
  8. ORDER BY category_name, product_name;
  9. -- Exercise 2 --
  10. SELECT
  11. first_name,
  12. last_name,
  13. line1,
  14. city,
  15. state,
  16. zip_code
  17. FROM customers c JOIN addresses a
  18. ON c.customer_id = a.customer_id
  19. WHERE email_address LIKE 'allan.sherwood@yahoo.com';
  20. -- Exercise 3 --
  21. SELECT
  22. first_name,
  23. last_name,
  24. line1,
  25. city,
  26. state,
  27. zip_code
  28. FROM addresses a JOIN customers c
  29. ON c.customer_id = a.customer_id AND c.shipping_address_id = address_id;
  30. -- Exercise 4 --
  31. SELECT
  32. last_name AS 'Last Name',
  33. first_name AS 'Fist Name',
  34. order_date AS 'Order Date',
  35. product_name AS 'Product Name',
  36. item_price AS 'Item Price',
  37. discount_amount AS 'Discount Amount',
  38. quantity AS 'Quantity'
  39. FROM customers c
  40. JOIN orders o
  41. ON c.customer_id = o.customer_id
  42. JOIN order_items oi
  43. ON oi.order_id = o.order_id
  44. JOIN products p
  45. ON p.product_id = oi.product_id
  46. ORDER BY last_name, order_date, product_name;
  47. -- Exercise 5 --
  48. SELECT
  49. p.list_price,
  50. p.product_name
  51. FROM products p INNER JOIN products p2
  52. ON p.list_price = p2.list_price
  53. WHERE p.product_id != p2.product_id
  54. ORDER BY product_name;
  55. -- Exercise 6 --
  56. SELECT
  57. category_name,
  58. product_id
  59. FROM categories c LEFT OUTER JOIN products p
  60. ON c.category_id = p.category_id
  61. WHERE p.product_id IS NULL;
  62. -- Exercise 7 --
  63. SELECT
  64. 'SHIPPED' AS ship_status,
  65. order_id,
  66. order_date
  67. FROM orders o
  68. WHERE ship_date IS NOT NULL
  69. UNION
  70. SELECT
  71. 'NOT SHIPPED' AS ship_status,
  72. order_id,
  73. order_date
  74. FROM orders o
  75. WHERE ship_date IS NULL
  76. ORDER BY order_date;