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

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. -- Exercise 1
  2. SELECT category_name, product_name, list_price
  3. FROM categories c JOIN products p
  4. ON c.category_id = p.category_id
  5. ORDER BY category_name, product_name
  6. -- Exercise 2
  7. SELECT first_name, last_name, line1, city, state, zip_code
  8. FROM customers c JOIN addresses a
  9. ON c.customer_id = a.customer_id
  10. WHERE email_address LIKE 'allan.sherwood@yahoo.com'
  11. -- Exercise 3
  12. SELECT first_name, last_name, line1, city, state, zip_code
  13. FROM customers c JOIN addresses a
  14. ON c.customer_id = a.customer_id
  15. AND c.shipping_address_id = address_id
  16. -- Exercise 4
  17. SELECT last_name, first_name, order_date, product_name, item_price, discount_amount, quantity
  18. FROM customers c JOIN orders o
  19. ON c.customer_id = o.customer_id
  20. JOIN order_items i
  21. ON i.order_id = o.order_id
  22. JOIN products p
  23. ON p.product_id = i.product_id
  24. ORDER BY last_name, order_date, product_name
  25. -- Exercise 5
  26. SELECT p.product_name, p.list_price
  27. FROM products p JOIN products p2
  28. ON p.list_price = p2.list_price AND p.product_id != p2.product_id
  29. ORDER BY product_name
  30. -- Exercise 6
  31. SELECT c.category_name, p.product_id
  32. FROM categories c LEFT JOIN products p
  33. ON c.category_id = p.category_id
  34. WHERE p.product_id IS NULL
  35. -- Exercise 7