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

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