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
91 changes: 74 additions & 17 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ 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
````sql
INSERT YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

select * 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 @@ -67,41 +73,70 @@ INSERT YOUR QUERY HERE

**You:** Then here's the query for that:

```sql
````sql
INSERT YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

SELECT * FROM spends WHERE LOWER(description) LIKE '%fee%' OR LOWER(description) LIKE '%fees%'

``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````


**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
````sql
INSERT YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

select * from spends join expense_areas
on(spends.expense_area_id= expense_areas.id)
where expense_areas.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
````sql
CREATE YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

SELECT date, SUM(amount) As amount 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
````sql
INSERT YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

SELECT supplier_id, SUM(amount) As amount 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
````sql
INSERT YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

SELECT suppliers.id, suppliers.supplier, SUM(amount) as total_per_month
FROM suppliers
FULL OUTER JOIN spends ON suppliers.id = spends.supplier_id
GROUP BY suppliers.id


``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

**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 @@ -113,7 +148,15 @@ INSERT YOUR QUERY HERE

```sql
CREATE YOUR QUERY HERE
```
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

SELECT date, SUM(amount) as total_per_month
FROM spends
WHERE date BETWEEN '2021-03-01' AND '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 @@ -123,10 +166,25 @@ 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
````sql
INSERT YOUR QUERIES HERE
``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

```
INSERT INTO expense_types(expense_type) VALUES('Hardware')
INSERT INTO expense_areas(expense_area) VALUES('IT')
INSERT INTO suppliers(supplier) VALUES('Dell')

INSERT INTO spends (expense_type_id, expense_area_id, supplier_id, date, transaction_no, supplier_inv_no, description, amount)
VALUES (42,
46,
66,
'2021-08-19',
38104091,
'3780119655',
'Computer Hardware Dell',
320000);

``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

**Claire:** Great, that's everything we need. Thanks for your help.

Expand All @@ -136,5 +194,4 @@ INSERT YOUR QUERIES HERE

- [ ] All user stories are satisfied
- [ ] All queries are written in SQL
- [ ] All queries are correct and I have tested them in the database
- [ ] I have opened a pull request with my answers written directly into this README.md file
- [ ] All queries are correct and I have tested them in the database
80 changes: 79 additions & 1 deletion E-Commerce/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,90 @@ erDiagram
Write SQL queries to complete the following tasks:

- [ ] List all the products whose name contains the word "socks"

SELECT product_name from products WHERE product_name ILIKE '%socks%';

``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````


- [ ] List all the products which cost more than 100 showing product id, name, unit price, and supplier id
SELECT p.id, pa.supp_id AS supplier_id, p.product_name, pa.unit_price
FROM products AS p
INNER JOIN product_availability AS pa
ON pa.prod_id = p.id
WHERE pa.unit_price > 100;


``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

- [ ] List the 5 most expensive products

SELECT p.id ,p.product_name
FROM products AS p INNER JOIN product_availability AS pa ON (pa.prod_id = p.id)
ORDER BY unit_price DESC LIMIT 5;


``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

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

``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

- [ ] List all orders, including order items, from customer named Hope Crosby

SELECT
o.id AS order_id,
o.order_date,
o.order_reference,
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 c ON o.customer_id = c.id
WHERE c.name = '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

SELECT
p.product_name,
pa.unit_price,
oi.quantity
FROM products AS p
INNER JOIN product_availability AS pa ON p.id = pa.prod_id
INNER JOIN order_items AS oi ON p.id = oi.product_id
INNER JOIN orders AS o ON o.id = oi.order_id
WHERE o.order_reference = 'ORD006';


``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

- [ ] 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
ord.order_reference,
ord.order_date,
p.product_name,
s.supplier_name,
oi.quantity
FROM products AS p
INNER JOIN order_items AS oi ON p.id = oi.product_id
INNER JOIN orders AS ord ON oi.order_id = ord.id
INNER JOIN suppliers AS s ON oi.supplier_id = s.id;



``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````

## Acceptance Criteria

Expand Down