Skip to content
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
24 changes: 22 additions & 2 deletions week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Submission

Below you will find a set of tasks for you to complete to consolidate and extend your learning from this week. You will find it beneficial to complete the reading tasks before attempting some of these.
Below you will find a set of tasks for you to complete to consolidate and extend your learning from this week. You will find it beneficial to complete the reading tasks before attempting some of these.

To submit this homework write the correct commands for each question here:

Expand All @@ -18,19 +18,39 @@ When you have finished all of the questions - open a pull request with your answ
If you haven't completed all the exercises from this lesson then do that first.

### Tasks

1. Which rooms have a rate of more than 100.00?
select room_no, rate from rooms where rate > 100.00;
2. List the reservations that have a checkin date this month and are for more than three nights.
select \*, checkin_date - checkout_date as night from reservations where checkin_date between '2020-09-01' and '2020-09-30' and checkout_date - checkin_date > 3
3. List all customers from cities that begin with the letter 'M'.
select \* from customers where name like 'M%';

Insert some new data into the room_types and rooms tables, querying after each stage to check the data, as follows:

4. Make a new room type of PENTHOUSE with a default rate of £185.00
INSERT INTO room_types (room_type, def_rate) VALUES('PENTHOUSE',185.00);
5. Add new rooms, 501 and 502 as room type PENTHOUSE and set the room rate of each to the default value (as in the new room type).
INSERT INTO rooms (room_no, room_type, rate) VALUES (501, 'PENTHOUSE', 185), (502,'PENTHOUSE', 185);
6. Add a new room 503 as a PREMIER PLUS type similar to the other PREMIER PLUS rooms in the hotel but with a room rate of 143.00 to reflect its improved views over the city.
INSERT INTO ROOMS (room_no, room_type, rate) VALUES (503,'PREMIER PLUS', 143.00);

Using what you can learn about aggregate functions in the w3schools SQL classes (or other providers), try:

7. The hotel manager wishes to know how many rooms were occupied any time during the previous month - find that information.
SELECT count(cust_id) from reservations where checkin_date >= '2020-08-01' and checkout_date < '2020-08-31';
8. Get the total number of nights that customers stayed in rooms on the second floor (rooms 201 - 299).
SELECT sum(checkout_date - checkin_date) FROM RESERVATIONS where room_no between 201 and 209;
9. How many invoices are for more than £300.00 and what is their grand total and average amount?
10. Bonus Question: list the number of nights stay for each floor of the hotel (floor no is the hundreds part of room number, e.g. room **3**12 is on floor **3**)
** More than £300.00 **
SELECT count(total) from invoices where total > 300
** Grand total **
SELECT sum(total) from invoices where total > 300
** Aver total **
SELECT avg(total) from invoices where total > 300
10. Bonus Question: list the number of nights stay for each floor of the hotel (floor no is the hundreds part of room number, e.g. room **3**12 is on floor **3**)
SELECT sum(checkout_date - checkin_date) from reservations WHERE room_no between 100 and 199;
SELECT sum(checkout_date - checkin_date) from reservations WHERE room_no between 200 and 299;
SELECT sum(checkout_date - checkin_date) from reservations WHERE room_no between 300 and 399;
SELECT sum(checkout_date - checkin_date) from reservations WHERE room_no between 400 and 499;
SELECT sum(checkout_date - checkin_date) from reservations WHERE room_no > 500;
19 changes: 16 additions & 3 deletions week-2/mandatory/2-ecommerce-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ In this homework, you are going to work with an ecommerce database. In this data
Below you will find a set of tasks for you to complete to set up a database for an e-commerce app.

To submit this homework write the correct commands for each question here:

```sql


Expand Down Expand Up @@ -35,16 +36,28 @@ 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;
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.product_name, p.id, pa.prod_id, pa.supp_id, pa.unit_price from products p 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 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 JOIN product_availability pa ON (p.id = pa.prod_id) JOIN suppliers s ON(pa.prod_id = s.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`.
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 p.product_name, s.supplier_name FROM products p JOIN suppliers s ON (p.id = s.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.order_date, o.order_reference, o.id, (or_item.quantity \* pa.unit_price) as "total cost" FROM orders o JOIN customers c on(o.customer_id = c.id) JOIN order_items or_item ON (o.id = or_item.order_id) JOIN product_availability pa ON (or_item.product_id = pa.prod_id) WHERE c.id = 1;
9. Retrieve all orders, including order items, from customer named `Hope Crosby`
SELECT o.order_date, o.order_reference, o.id, (or_item.quantity \* pa.unit_price) as "total cost" FROM orders o JOIN customers c on(o.customer_id = c.id) JOIN order_items or_item ON (o.id = or_item.order_id) JOIN product_availability pa ON (or_item.product_id = pa.prod_id) WHERE c.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, ori.quantity FROM products p JOIN product_availability pa ON (p.id = pa.prod_id) JOIN order_items ori ON (product_id = p.id) JOIN orders o ON (o.id = ori.order_id) WHERE o.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, ori.quantity FROM customers c JOIN orders o ON(c.id = o.customer_id) JOIN order_items ori ON (ori.order_id = o.id) JOIN products p ON(p.id = product_id) JOIN suppliers s ON(s.id = ori.supplier_id);
12. Retrieve the names of all customers who bought a product from a supplier based in 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, s.country FROM customers c JOIN orders o on (o.customer_id =c.id ) JOIN order_items i on (i.order_id = o.id) JOIN suppliers s ON(s.id = i.supplier_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, sum(ori.quantity \* pa.unit_price) AS "total amount" FROM customers c JOIN orders o ON(c.id = o.customer_id) JOIN order_items ori ON (ori.order_id = o.id) JOIN product_availability pa ON (pa.prod_id = ori.product_id and pa.supp_id = ori.supplier_id) GROUP by c.name, o.order_reference, o.order_date ORDER BY "total amount" DESC;
1 change: 1 addition & 0 deletions week-2/mandatory/3-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Loading