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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. -- Exercise 1
  2. SELECT category_name, product_name, list_price
  3. FROM products p JOIN categories c
  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 ON c.customer_id = o.customer_id
  19. JOIN order_items i ON i.order_id = o.order_id
  20. JOIN products p ON p.product_id = i.product_id
  21. ORDER BY last_name, order_date, product_name;
  22. -- Exercise 5
  23. SELECT p1.product_name, p2.list_price
  24. FROM products p1 JOIN products p2
  25. ON p1.product_id <> p2.product_id
  26. AND p1.list_price = p2.list_price
  27. ORDER BY product_name;
  28. -- Exercise 6
  29. SELECT * FROM categories c JOIN products p
  30. ON p.category_id = c.category_id
  31. WHERE p.product_id IS NULL;
  32. -- Exercise 7
  33. SELECT 'SHIPPED' AS ship_status, order_id, order_date
  34. FROM orders
  35. WHERE ship_date IS NOT NULL
  36. UNION
  37. SELECT 'NOT SHIPPED' AS ship_status, order_id, order_date
  38. FROM orders
  39. WHERE ship_date IS NULL
  40. ORDER BY order_date;