This repository was archived by the owner on Aug 17, 2024. It is now read-only.
generated from CodeYourFuture/CYF-Coursework-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 218
MANCHESTER NW5- ADIBA BELAYETE- MODULE-SQL _02- WEEK-02 #185
Open
Adibab
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
Adibab:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,16 +35,67 @@ 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 | ||
sql- SELECT name, address FROM customers where country ='United States'; | ||
2. Retrieve all the customers in ascending name sequence | ||
sql- select name from customers order by name; | ||
3. Retrieve all the products whose name contains the word `socks` | ||
sql- select product_name from products where product_name like '%socks%'; | ||
4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id. | ||
sql- select * from products inner join product_availability on products.id = product_availability.prod_id where unit_price > 100; | ||
5. Retrieve the 5 most expensive products | ||
sql-select * from products inner join product_availability on products.id = product_availability.prod_id order by 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` | ||
sql= select product_name, unit_price, supplier_name from products inner join product_availability on products.id = product_availability.prod_id inner join suppliers on products.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`. | ||
sql- | ||
|
||
select product_name, supplier_name from products inner join suppliers on products.id = suppliers.id where 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). | ||
sql- | ||
|
||
|
||
select orders.id as orders_id, orders.order_reference as ref, orders.order_date as date, product_availability.unit_price * order_items.quantity as total_cost 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` | ||
sql = | ||
select orders.id as orders_id, orders.order_reference as ref, orders.order_date as date, product_availability.unit_price * order_items.quantity as total_cost, products.product_name 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 inner join products on products.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`. | ||
|
||
sql- | ||
|
||
select products.product_name, product_availability.unit_price, order_items.quantity from products inner join product_availability on products.id = product_availability.prod_id inner join order_items on order_items.product_id = product_availability.prod_id inner join orders on orders.id = order_items.order_id where orders.order_reference = 'ORD006'; | ||
product_name | unit_price | quantity | ||
|
||
|
||
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`. | ||
sql - | ||
|
||
select customers.name, orders.order_reference, orders.order_date, products.product_name, suppliers.supplier_name, order_items.quantity from customers inner join orders on customers.id = orders.customer_id inner join order_items on orders.id = order_items.order_id inner join products on products.id = order_items.product_id inner join suppliers on suppliers.id = order_items.supplier_id; | ||
|
||
|
||
|
||
|
||
|
||
12. Retrieve the names of all customers who bought a product from a supplier based in China. | ||
sql - | ||
select distinct customers.name from customers inner join orders on orders.customer_id = customers.id inner join order_items on order_items.order_id = orders.id inner join suppliers on suppliers.id = order_items.supplier_id and suppliers.country = 'China'; | ||
|
||
|
||
|
||
|
||
|
||
13. List all orders giving customer name, order reference, order date and order total amount (quantity * unit price) in descending order of total. | ||
|
||
sql - | ||
select customers.name, orders.order_date, orders.order_reference, order_items.quantity * product_availability.unit_price as total_amount from customers inner join orders on orders.customer_id = customers.id inner join order_items on order_items.order_id = orders.id inner join product_availability on product_availability.prod_id = order_items.product_id order by total_amount desc; | ||
|
||
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. good job on completing these tasks Adiba :) separating your clauses onto new lines can help with readability |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
good to see you exploring distinct here