weighter

SQL Assignment 2 ch 4-5

Dec 13th, 2021 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.81 KB | None | 0 0
  1. USE my_guitar_shop;
  2.  
  3. -- Chapter 4 Exercise 1
  4.  
  5. SELECT category_name, product_name, list_price
  6. FROM categories c INNER JOIN products p
  7.     ON c.category_id = p.category_id
  8. ORDER BY category_name, product_name ASC;
  9.  
  10. -- Chapter 4 Exercise 2
  11.  
  12. SELECT first_name, last_name, line1, city, state, zip_code
  13. FROM customers c INNER JOIN addresses a
  14.     ON c.customer_id = a.customer_id
  15. WHERE email_address = 'allan.sherwood@yahoo.com';
  16.  
  17. -- Chapter 4 Exercise 3
  18.  
  19. SELECT first_name, last_name, line1, city, state, zip_code
  20. FROM customers c INNER JOIN addresses a
  21.     ON c.customer_id = a.customer_id
  22. WHERE shipping_address_id = address_id;
  23.  
  24.  
  25. -- Chapter 4 Exercise 4
  26.  
  27. SELECT c.last_name, c.first_name, o.order_date, p.product_name, oi.item_price, oi.discount_amount, oi.quantity
  28. FROM customers c
  29.     INNER JOIN orders o ON c.customer_id = o.customer_id
  30.     INNER JOIN order_items oi on o.order_id = oi.order_id
  31.     INNER JOIN products p on oi.product_id = p.product_id
  32. ORDER BY c.last_name, o.order_date, p.product_name;
  33.  
  34.  
  35. -- Chapter 4 Exercise 5
  36.  
  37. SELECT p1.product_name, p1.list_price, p2.list_price
  38. FROM products p1
  39.     JOIN products p2 ON p1.list_price = p2.list_price
  40. WHERE p1.product_id != p2.product_id ;
  41.  
  42.  
  43. -- Chapter 4 Exercise 6
  44.  
  45. SELECT category_name, p.product_name
  46. FROM categories
  47.     left join products p on categories.category_id = p.category_id
  48. WHERE p.product_name IS NULL;
  49.  
  50.  
  51. -- Chapter 4 Exercise 7
  52.  
  53. SELECT 'SHIPPED' AS 'ship_status', order_id, order_date
  54. FROM orders
  55. WHERE ship_date IS NOT NULL
  56. UNION
  57. SELECT 'NOT SHIPPED' AS 'ship_status', order_id, order_date
  58. FROM orders
  59. WHERE ship_date IS NULL
  60. ORDER BY order_date;
  61.  
  62.  
  63.  
  64. --===================================
  65.  
  66.  
  67. USE my_guitar_shop;
  68.  
  69.  
  70. -- Chapter 5: Exercise 1
  71.  
  72. INSERT INTO categories (category_name)
  73. VALUES ('Brass');
  74.  
  75.  
  76. -- Chapter 5: Exercise 2
  77.  
  78. UPDATE categories
  79. SET category_name = 'Woodwinds'
  80. WHERE category_id = 5;
  81.  
  82.  
  83. -- Chapter 5: Exercise 3
  84.  
  85. DELETE FROM categories
  86. WHERE category_id = 5;
  87.  
  88.  
  89. -- Chapter 5: Exercise 4
  90.  
  91. INSERT INTO products (category_id, product_code, product_name, description, list_price, discount_percent, date_added)
  92. VALUES ('4','dgx_640','Yamaha DGX 640 88-Key Digital Piano','Long description to come.',799.99,0,NOW());
  93.  
  94.  
  95. -- Chapter 5: Exercise 5
  96.  
  97. UPDATE products
  98. SET discount_percent = 35
  99. WHERE product_id = 11;
  100.  
  101.  
  102. -- Chapter 5: Exercise 6
  103.  
  104. DELETE FROM products WHERE category_id = 4;
  105. DELETE FROM categories WHERE category_name = 'Keyboards';
  106.  
  107.  
  108. -- Chapter 5: Exercise 7
  109.  
  110. INSERT INTO customers (email_address, password, first_name, last_name)
  111. VALUES ('rick@raven.com', '', 'Rick', 'Raven');
  112.  
  113.  
  114. -- Chapter 5: Exercise 8
  115.  
  116. UPDATE customers
  117. SET password = 'secret'
  118. WHERE customer_id = 9;
  119.  
  120.  
  121. -- Chapter 5: Exercise 9
  122.  
  123. UPDATE customers
  124. SET password = 'reset'
  125. WHERE customer_id IS NOT NULL;
Add Comment
Please, Sign In to add comment