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

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