Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.

Manchester-NW5-LeilaFaez-SQL-Week2 #179

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
37 changes: 37 additions & 0 deletions 2-exercises/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,53 @@ 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

select name,address from customers where country='United States';

2. Retrieve all the customers in ascending name sequence

select * from customers order by name ASC;

3. Retrieve all the products whose name contains the word `socks`

select * 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.

select p.id,p.product_name,pa.unit_price,pa.supp_id from products p inner join product_availability pa on (p.id=pa.prod_id) where pa.unit_price >100;

5. Retrieve the 5 most expensive products

select p.product_name, pa.unit_price from products p inner join product_availability pa on (p.id=pa.prod_id) order by pa.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`

select p.product_name, pa.unit_price,s.supplier_name from products p inner join product_availability pa on (p.id=pa.prod_id) inner join suppliers s on (s.id=pa.supp_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`.

select p.product_name, s.supplier_name from products p inner join product_availability pa on (p.id=pa.prod_id) inner join suppliers s on (s.id=pa.supp_id) where s.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).

select o.id,o.order_reference,o.order_date,oi.quantity*pa.unit_price as total from orders o inner join order_items oi on (o.id=oi.order_id) inner join product_availability pa on (pa.prod_id = oi.product_id) where o.customer_id=1;

9. Retrieve all orders, including order items, from customer named `Hope Crosby`

select o.id, oi.product_id,oi.supplier_id,oi.quantity from orders o inner join order_items oi on (o.id=oi.order_id) inner join customers on (customers.id=o.customer_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`.

select p.product_name, pa.unit_price, oi.quantity from products p inner join product_availability pa on (p.id = pa.prod_id) inner join order_items oi ON (pa.prod_id = oi.product_id) inner join orders on (oi.order_id = orders.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`.

select c.name, o.order_reference, o.order_date, p.product_name, s.supplier_name, oi.quantity FROM customers c inner Join orders o on (c.id = o.customer_id) inner join order_items oi on (o.id = oi.order_id) inner join product_availability a ON (a.prod_id = oi.product_id) inner JOIN products p ON (p.id = a.prod_id) inner join suppliers s ON (s.id = a.supp_id);

12. Retrieve the names of all customers who bought a product from a supplier based in China.

select c.name FROM customers c inner Join orders o on (c.id = o.customer_id) inner join order_items oi on (o.id = oi.order_id) inner join product_availability a ON (a.prod_id = oi.product_id) inner JOIN suppliers s ON (s.id = a.supp_id)where s.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.

select c.name, o.order_reference, o.order_date, (a.unit_price * oi.quantity) AS total_amount FROM customers c inner Join orders o on (c.id = o.customer_id) inner join order_items oi on (o.id = oi.order_id) inner join product_availability a ON (a.prod_id = oi.product_id);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the descending order is missing

Loading