Skip to content
40 changes: 39 additions & 1 deletion Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ You are working with Claire and Farnoosh, who are trying to complete a missing r

**You:** Absolutely. Here's the SQL query you need:

<!-- INSERT YOUR QUERY HERE -->

<!-- This query returns all the amounts between 30 to 31 tousands in the spends table. -->

```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 @@ -75,32 +80,47 @@ INSERT YOUR QUERY HERE

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

<!-- This query returns all the words contain the word 'fee' , like 'fees' , 'FEES' , 'Fees' . -->

```sql
INSERT YOUR QUERY HERE
select * from spends where lower(description) like '%fee%';
```

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

<!-- This query add up amounts in the spends table and group them based on months; -->

```sql
CREATE YOUR QUERY HERE
select sum(amount) , to_char(date,'yyyy-mm') as month from spends group by month;
```

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

<!-- This query returns supplier_id's and the amount been spent on them, and ordered by supplier id's . -->

```sql
INSERT YOUR QUERY HERE
select supplier_id , sum(amount) as total from spends group by supplier_id order 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.

<!-- This query returns supplier names as well by joining supplier table, order then asc based on the supplier name. -->

```sql
INSERT YOUR QUERY HERE
select sd.supplier_id ,spl.supplier ,sum(sd.amount) as total from spends sd join suppliers spl on (spl.id=sd.supplier_id) group by spl.supplier, sd.supplier_id
order by spl.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 @@ -111,8 +131,12 @@ INSERT YOUR QUERY HERE

**You:** Then you need an extra clause. Here's the query:

<!-- It returns total of amount on first of March and first of April . -->

```sql
CREATE YOUR QUERY HERE
select date , sum(amount) from spends where (date='2021-03-01') or (date='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 @@ -125,6 +149,20 @@ CREATE YOUR QUERY HERE

```sql
INSERT YOUR QUERIES HERE
-- first we add a new supplier 'Dell' into suppliers:
Insert into suppliers(supplier) values('DELL');

-- adding Hardware to expense_types:
insert into expense_types(expense_type) values('Hardware');

-- adding IT to expense_areas as well:
insert into expense_areas(expense_area) values('IT');

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',32000.00)
;
-- After Insert this data and did some query on the table I realised another field with name 'Hardware Purch' is in the expense_types
-- also there wasn't an IT feild for expense areas so i created it, I hope I understood the questions correctly .

```

Expand Down