From 78842c60d951c7a8da11cf71cf7017bd3ad8013a Mon Sep 17 00:00:00 2001 From: aishaathmanlali Date: Wed, 29 May 2024 12:30:41 +0100 Subject: [PATCH 1/5] create file queries.sql --- Big-Spender/readme.md | 6 +++--- queries.sql | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 queries.sql diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index dc6cf9a2..7b4f2059 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -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? @@ -68,7 +68,7 @@ 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? @@ -76,7 +76,7 @@ INSERT YOUR QUERY HERE **You:** No worries. Here's the query for that: ```sql -INSERT YOUR QUERY HERE +SELECT * FROM spends WHERE expense_area_id = 'Better Hospital Food'; ``` **Claire:** Great, that's very helpful. How about the total amount spent for each month? diff --git a/queries.sql b/queries.sql new file mode 100644 index 00000000..e002bbcb --- /dev/null +++ b/queries.sql @@ -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; + + + + From 7b3e5854f7c8fd5dbd27373602648a64d4bb20bd Mon Sep 17 00:00:00 2001 From: aishaathmanlali Date: Thu, 30 May 2024 13:20:37 +0100 Subject: [PATCH 2/5] add queries --- Big-Spender/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index dc6cf9a2..256f2118 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -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? @@ -68,7 +68,7 @@ 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? From b9aef19ca93da17fb3d3bf54b413a2ab6f4e7fc6 Mon Sep 17 00:00:00 2001 From: aishaathmanlali Date: Thu, 30 May 2024 14:09:29 +0100 Subject: [PATCH 3/5] add queries --- Big-Spender/readme.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index 256f2118..a8326c1a 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -84,7 +84,10 @@ 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 DATE_TRUNC('month', date) +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 +95,10 @@ CREATE YOUR QUERY HERE **You:** Sure thing. Here's the query for that: ```sql -INSERT YOUR QUERY HERE +SELECT supplier_id, SUM(amount) AS total_spent +FROM spends +GROUP BY supplier_id +ORDER BY total_spent DESC; ``` **Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here. @@ -112,7 +118,11 @@ 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? From bec26aea8b3e5521edd9241902d92285d1d81371 Mon Sep 17 00:00:00 2001 From: aishaathmanlali Date: Tue, 4 Jun 2024 14:44:01 +0100 Subject: [PATCH 4/5] add queries for big-spender --- Big-Spender/readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index 7b4f2059..98919dfa 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -76,7 +76,7 @@ SELECT description FROM spends WHERE description ILIKE '%fee%'; **You:** No worries. Here's the query for that: ```sql -SELECT * FROM spends WHERE expense_area_id = 'Better Hospital Food'; +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? @@ -84,7 +84,7 @@ SELECT * FROM spends WHERE expense_area_id = 'Better Hospital Food'; **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? @@ -92,7 +92,7 @@ CREATE YOUR QUERY HERE **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. @@ -100,7 +100,7 @@ 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 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)? @@ -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? @@ -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'); ``` From 7b0a72f516d7f78a1496564086cd4be234d3ac7f Mon Sep 17 00:00:00 2001 From: aishaathmanlali Date: Tue, 4 Jun 2024 14:53:06 +0100 Subject: [PATCH 5/5] edit shortform of suppliers table name --- Big-Spender/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index 98919dfa..c522b7da 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -100,7 +100,7 @@ SELECT supplier_id, SUM(amount) AS cost_per_supplier FROM spends GROUP BY suppli **You:** Whoops! I gave you ids to key the totals, but let me give you names instead. ```sql -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; +SELECT sp.supplier_id, sup.supplier, SUM(sp.amount) AS cost_per_supplier FROM spends AS sp JOIN suppliers AS sup ON sp.supplier_id = sup.id GROUP BY sp.supplier_id, sup.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)?