Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WM5 | AISHA_ATHMAN_LALI | MODULE_DATABASES | WEEK_3 | CYF_E-COMMERCE #177

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
16 changes: 8 additions & 8 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ You are working with Claire and Farnoosh, who are trying to complete a missing r
**You:** Absolutely. Here's the SQL query you need:

```sql
INSERT YOUR QUERY HERE
SELECT amount FROM spends WHERE amount BETWEEN 30000 and 31000;
```

**Claire:** That's great, thanks. Hey, what about transactions that include the word 'fee' in their description?
Expand All @@ -68,39 +68,39 @@ INSERT YOUR QUERY HERE
**You:** Then here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT description FROM spends WHERE description ILIKE '%fee%';
```

**Farnoosh:** Hi, it's me again. It turns out we also need the transactions that have the expense area of 'Better Hospital Food'. Can you help us with that one?

**You:** No worries. Here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT sp.id, sp.transaction_no, ex_a.expense_area FROM expense_areas AS ex_a JOIN spends AS sp ON ex_a.id = sp.expense_area_id WHERE expense_area = 'Better Hospital Food';
```

**Claire:** Great, that's very helpful. How about the total amount spent for each month?

**You:** You can get that by using the GROUP BY clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
SELECT date, SUM(amount) AS monthly_costs FROM spends GROUP BY date;
```

**Farnoosh:** Thanks, that's really useful. We also need to know the total amount spent on each supplier. Can you help us with that?

**You:** Sure thing. Here's the query for that:

```sql
INSERT YOUR QUERY HERE
SELECT supplier_id, SUM(amount) AS cost_per_supplier FROM spends GROUP BY supplier_id;
```

**Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here.

**You:** Whoops! I gave you ids to key the totals, but let me give you names instead.

```sql
INSERT YOUR QUERY HERE
SELECT sp.supplier_id, su.supplier, SUM(sp.amount) AS cost_per_supplier FROM spends AS sp JOIN suppliers AS su ON sp.supplier_id = su.id GROUP BY sp.supplier_id, su.supplier;
```

**Claire:** Thanks, that's really helpful. I can't quite figure out...what is the total amount spent on each of these two dates (1st March 2021 and 1st April 2021)?
Expand All @@ -112,7 +112,7 @@ INSERT YOUR QUERY HERE
**You:** Then you need an extra clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
SELECT date, SUM(amount) AS monthly_costs FROM spends WHERE date IN ('2021-03-01', '2021-04-01') GROUP BY date;
```

**Farnoosh:** Fantastic. One last thing, looks like we missed something. Can we add a new transaction to the spends table with a description of 'Computer Hardware Dell' and an amount of £32,000?
Expand All @@ -124,7 +124,7 @@ CREATE YOUR QUERY HERE
**You:** Sure thing. To confirm, the date is August 19, 2021, the transaction number is 38104091, the supplier invoice number is 3780119655, the supplier is 'Dell', the expense type is 'Hardware' and the expense area is 'IT'. Here's the query for that:

```sql
INSERT YOUR QUERIES HERE
INSERT INTO spends (expense_type_id, expense_area_id, supplier_id, date, amount, transaction_no, supplier_inv_no, description) VALUES (7, 18, 16, '2021-08-19', 32000, 38104091, '3780119655', 'Computer Hardware Dell');

```

Expand Down
64 changes: 53 additions & 11 deletions E-Commerce/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,59 @@ erDiagram

Write SQL queries to complete the following tasks:

- [ ] List all the products whose name contains the word "socks"
- [ ] List all the products which cost more than 100 showing product id, name, unit price, and supplier id
- [ ] List the 5 most expensive products
- [ ] List all the products sold by suppliers based in the United Kingdom. The result should only contain the columns product_name and supplier_name
- [ ] List all orders, including order items, from customer named Hope Crosby
- [ ] List all the products in the order ORD006. The result should only contain the columns product_name, unit_price, and quantity
- [ ] List 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
- [x] List all the products whose name contains the word "socks"
SELECT * FROM products WHERE product_name ILIKE '%socks%';

- [x] List 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 AS p
JOIN product_availability AS pa
ON p.id = pa.prod_id
WHERE pa.unit_price > 100;

- [x] List the 5 most expensive products
SELECT p.product_name, pa.unit_price
FROM products AS p
JOIN product_availability AS pa
ON p.id = pa.prod_id
GROUP BY p.product_name, pa.unit_price
ORDER BY pa.unit_price DESC LIMIT 5;

- [x] List 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 AS p
JOIN product_availability AS pa ON p.id = pa.prod_id
JOIN suppliers AS s ON s.id = pa.supp_id
WHERE s.country = 'United Kingdom';

- [x] List all orders, including order items, from customer named Hope Crosby
SELECT c.name, o.id, o.order_date, o.order_reference, p.product_name, oi.quantity
FROM orders AS o
JOIN order_items AS oi ON o.id = oi.order_id
JOIN products AS p ON oi.product_id = p.id
JOIN customers AS c ON o.customer_id = c.id
WHERE c.name = 'Hope Crosby';

- [x] List 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 orders AS o
JOIN order_items AS oi ON o.id = oi.order_id
JOIN products AS p ON p.id = oi.product_id
JOIN product_availability AS pa ON pa.prod_id = oi.product_id
WHERE o.order_reference = 'ORD006';

- [x] List 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 orders AS o
JOIN customers AS c ON o.customer_id = c.id
JOIN order_items AS oi ON o.id = oi.order_id
JOIN products AS p ON oi.product_id = p.id
JOIN suppliers AS s ON oi.supplier_id = s.id
ORDER BY c.name, o.order_reference, p.product_name;

## Acceptance Criteria

- [ ] The `cyf_ecommerce` database is imported and set up correctly
- [ ] The database schema is drawn correctly to visualize relationships between tables
- [ ] The SQL queries retrieve the correct data according to the tasks listed above
- [ ] The pull request with the answers to the tasks is opened on the `main` branch of the `E-Commerce` repository
- [x] The `cyf_ecommerce` database is imported and set up correctly
- [x] The database schema is drawn correctly to visualize relationships between tables
- [x] The SQL queries retrieve the correct data according to the tasks listed above
- [x] The pull request with the answers to the tasks is opened on the `main` branch of the `E-Commerce` repository
13 changes: 13 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- psql -U aisha -f queries.sql
\c my_hotels;

SELECT * FROM reservations WHERE room_no = 204;

SELECT * FROM rooms WHERE room_no = 204;

SELECT rate, room_type, checkout_date, rooms.room_no, reservations.room_no FROM reservations
JOIN rooms ON reservations.room_no = rooms.room_no;