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
44 changes: 42 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,59 @@ 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 \* FROM rooms WHERE rate > 100;

2. List the reservations that have a checkin date this month and are for more than three nights.

SELECT \* 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 Upper(city) 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, rate, room_type, no_guests)
VALUES (501, 185.00, 'PENTHOUSE' NULL),
(502, 185.00, 'PENTHOUSE', NULL)

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, rate, room_type, no_guests)
VALUES (503, 143.00, 'PREMIER PLUS' 2);

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(distinct room_no) 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) AS nights
FROM reservations
WHERE room_no BETWEEN 200 AND 299;

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**)

SELECT count(\*), sum(total), avg(total) FROM invoices WHERE total > 300.00;

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 FLOOR(room_no/100) AS floor,
sum(checkout_date - checkin_date) AS nights
FROM reservations
WHERE room_no IS NOT NULL
GROUP BY floor;
90 changes: 88 additions & 2 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,101 @@ 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;
or
select \* from customers order by name asc;

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

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.

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

5. Retrieve the 5 most expensive products

select p.product_name, a.unit_price
from products p join
product_availability a on (p.id=a.prod_id)
order by a.unit_price desc
slimit 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, a.unit_price, s.supplier_name
from products p join
product_availability a on (p.id = a.prod_id) join
selesuppliers s on(a.supp_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, supplier_name
from products p join
product_availability a on(p.id=a.prod_id) join
suppliers s on(a.supp_id=s.id)
where s.country='United Kingdom';se

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 c.id, o.id, o.order_reference, o.order_date, p.product_name, i.quantity,
i.quantity \* a.unit_price as total_cost
from customers c join
orders o on(c.id=o.customer_id) join
order_items i on (o.id = i.order_id) join
product_availability a on (i.product_id = a.prod_id) join
products p on(a.prod_id = p.id) where c.id = 1;

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

select c.name, o.order_reference, o.order_date, i.product_id, i.quantity, p.product_name
from customers c join orders o on(c.id=o.customer_id) join
order_items i on (o.id=i.order_id) join
products p on (i.product_id=p.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, a.unit_price, i.quantity
from products p join
product_availability a on(p.id=a.prod_id) join
order_items i on(a.prod_id=i.product_id) join
orders o on(i.product_id=o.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, i.quantity
from customers c join
orders o on (c.id = o.customer_id) join
order_items i on (o.id=i.order_id) join
products p on (i.product_id = p.id)
join product_availability a on (p.id=a.prod_id)
join suppliers s on(a.supp_id=s.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 distinct c.name, s.supplier_name, s.country
from customers c join orders o on(c.id = o.customer_id) join
order_items i on (o.id = i.order_id) join
suppliers s on(i.supplier.id = s.id)
where s.country ='China';

**\*Note** I cannot link order-items with suppliers as in (order_items.supplier_id = supplier.id)
when I do \dt order_items it only shows 4 colums: id, order_id, product_id, and quantity. I tried to link the two in questions 11 too but no success.from Also the product_availability table was not showing initially on the list of relations but I managed to sort that out. I am doing something wrong?

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,
i.quantity\*a.unit_price as total_amount
from customers c join orders o on(c.id=o.customer_id) join
order_items i on(o.id = i.order_id) join
product_availability a on (i.product_id = a.prod_id)
order by total_amount desc;
58 changes: 58 additions & 0 deletions week-2/mandatory/3-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//**Note** I created the NodeJS application on a separate directory
// and then copied and pasted server.js file here. I hope this is fine.
// It works as expected :)

const express = require("express");
const app = express();

const { Pool } = require("pg");

const db = new Pool({
user: "leida",
host: "localhost",
database: "cyf_ecommerce",
password: "*******",
port: 5432,
});

app.get("/customers", function (req, res) {
db.query(`select * from customers`, function (err, result) {
if (err) {
res.status(500).json("Could not retrive data from the database.");
} else {
res.status(200).json(result.rows);
}
});
});

app.get("/suppliers", function (req, res) {
db.query(`select * from suppliers`, function (err, result) {
if (err) {
res.status(500).json("Could not retrive data from the database.");
} else {
res.json(result.rows);
}
});
});

// Return all the product names along with their prices and supplier names.

app.get("/products", function (req, res) {
db.query(
`select p.product_name, a.unit_price, s.supplier_name
from products p join product_availability a on(p.id=a.prod_id) join
suppliers s on (a.supp_id=s.id)`,
function (err, result) {
if (err) {
res.status(500).json("Could not retrive data from the database.");
} else {
res.json(result.rows);
}
}
);
});

const PORT = 3004;
app.listen(PORT, function () {
console.log(`Server is listening on port ${PORT}`);
});