Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 215
MANCHESTER -CLASS-5-Miguel-Cabral-SQL-Coursework-WEEK-2#189
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -35,16 +35,99 @@ Open the file `cyf_ecommerce.sql` in VSCode and examine the SQL code. Take a pie | ||
| Once you understand the database that you are going to work with, solve the following challenge by writing SQL queries using everything you learned about SQL: | ||
| 1. Retrieve all the customers' names and addresses who live in the United States | ||
| A = SELECT name, address FROM customers WHERE country = 'United States'; | ||
| 2. Retrieve all the customers in ascending name sequence | ||
| A = SELECT * FROM customers ORDER BY name ASC; | ||
| 3. Retrieve all the products whose name contains the word `socks` | ||
| A = SELECT * FROM products WHERE lower(product_name) LIKE '%socks%'; | ||
| 4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id. | ||
| A = SELECT products.id, products.product_name, product_availability.unit_price, product_availability.supp_id | ||
| FROM products | ||
| INNER JOIN product_availability | ||
| ON products.id = product_availability.prod_id; | ||
| 5. Retrieve the 5 most expensive products | ||
| A =SELECT products.id, products.product_name, product_availability.unit_price | ||
| FROM products | ||
| INNER JOIN product_availability | ||
| ON products.id = product_availability.prod_id | ||
| ORDER BY product_availability.unit_price DESC | ||
| LIMIT 5; | ||
| 6. Retrieve all the products with their corresponding suppliers. The result should only contain the columns `product_name`, `unit_price` and `supplier_name` | ||
| A = SELECT products.product_name, product_availability.unit_price, suppliers.supplier_name | ||
| FROM products | ||
| INNER JOIN product_availability | ||
| ON products.id = product_availability.prod_id | ||
| INNER JOIN suppliers | ||
| ON product_availability.supp_id = suppliers.id; | ||
| 7. Retrieve all the products sold by suppliers based in the United Kingdom. The result should only contain the columns `product_name` and `supplier_name`. | ||
| A = SELECT products.product_name, suppliers.supplier_name | ||
| FROM products | ||
| INNER JOIN product_availability | ||
| ON products.id = product_availability.prod_id | ||
| INNER JOIN suppliers | ||
| ON product_availability.supp_id = suppliers.id AND suppliers.country = 'United Kingdom'; | ||
| 8. Retrieve all orders, including order items, from customer ID `1`. Include order id, reference, date and total cost (calculated as quantity * unit price). | ||
| A = SELECT orders.id, orders.order_reference, orders.order_date, order_items.quantity * product_availability.unit_price AS total_cost | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting an explicit name for the calculated column as You've split the rest of the query on to one line per clause, which makes this easy to read. Sometimes it's good to split each field in the select clause into a separate line as well so I can quickly see exactly what fields are returned, without my eyes need to scan past commas. | ||
| FROM orders | ||
| INNER JOIN order_items | ||
| ON orders.id = order_items.order_id | ||
| INNER JOIN product_availability | ||
| ON order_items.product_id = product_availability.prod_id | ||
| WHERE orders.customer_id = 1; | ||
| 9. Retrieve all orders, including order items, from customer named `Hope Crosby` | ||
| A = SELECT orders.id, orders.order_reference, orders.order_date, order_items.quantity * product_availability.unit_price AS total_cost | ||
| FROM customers | ||
| INNER JOIN orders | ||
| ON customers.id = orders.customer_id | ||
| INNER JOIN order_items | ||
| ON orders.id = order_items.order_id | ||
| INNER JOIN product_availability | ||
| ON order_items.product_id = product_availability.prod_id | ||
| WHERE customers.name = 'Hope Crosby'; | ||
| 10. Retrieve all the products in the order `ORD006`. The result should only contain the columns `product_name`, `unit_price` and `quantity`. | ||
| A = SELECT products.product_name, product_availability.unit_price, order_items.quantity | ||
| FROM orders | ||
| INNER JOIN order_items | ||
| ON orders.id = order_items.order_id | ||
| INNER JOIN products | ||
| ON order_items.product_id = products.id | ||
| INNER JOIN product_availability | ||
| ON order_items.product_id = product_availability.prod_id | ||
| WHERE orders.order_reference = 'ORD006'; | ||
| 11. Retrieve all the products with their supplier for all orders of all customers. The result should only contain the columns `name` (from customer), `order_reference`, `order_date`, `product_name`, `supplier_name` and `quantity`. | ||
| A = SELECT customers.name, order_reference, order_date, products.product_name, suppliers.supplier_name, | ||
| order_items.quantity | ||
| FROM orders | ||
| INNER JOIN customers | ||
| ON orders.customer_id = customers.id | ||
| INNER JOIN order_items | ||
| ON orders.id = order_items.order_id | ||
| INNER JOIN products | ||
| ON order_items.product_id = products.id | ||
| INNER JOIN suppliers | ||
| ON order_items.supplier_id = suppliers.id | ||
| 12. Retrieve the names of all customers who bought a product from a supplier based in China. | ||
| A = SELECT customers.name | ||
| FROM orders | ||
| INNER JOIN customers | ||
| ON orders.customer_id = customers.id | ||
| INNER JOIN order_items | ||
| ON orders.id = order_items.order_id | ||
| INNER JOIN suppliers | ||
| ON order_items.supplier_id = suppliers.id AND suppliers.country = 'China' | ||
| GROUP by customers.name; | ||
| 13. List all orders giving customer name, order reference, order date and order total amount (quantity * unit price) in descending order of total. | ||
| A = SELECT customers.name, orders.order_reference, orders.order_date, | ||
| order_items.quantity * product_availability.unit_price AS total | ||
| FROM orders | ||
| INNER JOIN customers | ||
| ON orders.customer_id = customers.id | ||
| INNER JOIN order_items | ||
| ON orders.id = order_items.order_id | ||
| INNER JOIN product_availability | ||
| ON order_items.product_id = product_availability.prod_id | ||
| ORDER BY total DESC; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent that you've made the casing match on both sides, and correctly used
LIKEwith wildcards%at each end