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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 24 additions & 1 deletion week-1/mandatory/2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,41 @@ 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 '2023-01-01'and '2023-01-31') and (checkOut_date - checkIn_date > 3);
3. List all customers from cities that begin with the letter 'M'.
SELECT city FROM customers
WHERE 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
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');
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) VALUES ('501','185','PENTHOUSE')
INSERT INTO rooms (room_no, rate, room_type) VALUES ('502','185','PENTHOUSE');
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) VALUES ('503','143','PREMIER PLUS');

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(id) FROM reservations WHERE (checkIn_date between '2022-12-01'and '2022-12-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 total_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?
SELECT COUNT(total) from invoices WHERE total > 300;
SELECT SUM(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**)
Floor 1- 54 - select sum(checkOut_date -checkIn_date) from reservations where room_no between 100 and 199;

Floor 2- 63 - select sum(checkOut_date -checkIn_date) from reservations where room_no between 200 and 299;

Floor 3- 46- select sum(checkOut_date -checkIn_date) from reservations where room_no between 300 and 399;

Floor 4- 40 - select sum(checkOut_date -checkIn_date) from reservations where room_no between 400 and 499;
51 changes: 48 additions & 3 deletions week-2/mandatory/2-ecommerce-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,61 @@ 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 ILIKE '%socks%';

Choose a reason for hiding this comment

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

This would be a case insensitive statement; e.g. socks and Socks and SoCKs would all meet this. The question is a little ambiguous as to whether this should be the case, but just something to be aware of.


4. Retrieve all the products which cost more than 100 showing product id, name, unit price and supplier id.
5. Retrieve the 5 most expensive products
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, 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`.

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

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;

11 changes: 11 additions & 0 deletions week-2/mandatory/cyf-hotels-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "server.js",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
42 changes: 42 additions & 0 deletions week-2/mandatory/cyf-hotels-api/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const express = require("express");
const app = express();

app.listener = app.listen(port, function () {
console.log(`Server is listening on port ${port}`);
});
const { Pool } = require("pg");

const db = new Pool({
user: "mickeyhaile", // replace with you username
host: "localhost",
database: "cyf_hotels",
password: "",
port: 5432,
});

app.get("/customers", function (req, res) {
db.query("SELECT id, name, city, phone FROM customers")
.then((result) => {
res.json(result.rows);
})
.catch((error) => {
console.log(error);
});
});
app.get("/products", function (req, res) {
db.query("SELECT id, name, city, phone FROM customers")
.then((result) => {
res.json(result.rows);
})
.catch((error) => {
console.log(error);
});
});
app.get("/suppliers", function (req, res) {
db.query(
"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 (s.id = pa.supp_id )",
(error, result) => {
console.log(result);
res.json(result.rows);
});
});
1 change: 1 addition & 0 deletions week-2/mandatory/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading