diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index dc6cf9a2..bd5eefe5 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -47,9 +47,12 @@ 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 -``` +````sql + +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? @@ -68,15 +71,22 @@ INSERT YOUR QUERY HERE **You:** Then here's the query for that: ```sql -INSERT YOUR QUERY HERE -``` + +SELECT * +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 * +FROM expense_areas +WHERE expense_area = 'Better Hospital Food'; + ``` **Claire:** Great, that's very helpful. How about the total amount spent for each month? @@ -84,7 +94,12 @@ INSERT YOUR QUERY HERE **You:** You can get that by using the GROUP BY clause. Here's the query: ```sql -CREATE YOUR QUERY HERE +SELECT DATE_TRUNC('month', date) AS month, + SUM(amount) AS total_spent +FROM spends +GROUP BY month +ORDER 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? @@ -92,7 +107,13 @@ CREATE YOUR QUERY HERE **You:** Sure thing. Here's the query for that: ```sql -INSERT YOUR QUERY HERE + +SELECT s.supplier_id, + SUM(s.amount) AS total_spent +FROM spends s +GROUP BY s.supplier_id +ORDER BY total_spent DESC; + ``` **Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here. @@ -100,7 +121,13 @@ INSERT YOUR QUERY HERE **You:** Whoops! I gave you ids to key the totals, but let me give you names instead. ```sql -INSERT YOUR QUERY HERE + +SELECT su.supplier AS supplier_name, + SUM(s.amount) AS total_spent +FROM spends s +JOIN suppliers su ON s.supplier_id = su.id +GROUP BY su.supplier +ORDER BY total_spent DESC; ``` **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)? @@ -112,7 +139,12 @@ 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 total_spent +FROM spends +WHERE date IN ('2021-03-01', '2021-04-01') +GROUP BY date +ORDER 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? @@ -124,7 +156,26 @@ 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, + transaction_no, + supplier_inv_no, + description, + amount +) +VALUES ( + 42, -- expense_type_id for 'Hardware' + 46, -- expense_area_id for 'IT' + 66, -- supplier_id for 'Dell' + '2021-08-19', -- transaction date + 38104091, -- transaction number + '3780119655', -- supplier invoice number + 'Computer Hardware Dell', -- description + 32000 -- amount +); ```