diff --git a/2-classes-db/task.md b/2-classes-db/task.md index 477d3eca..3bc02b67 100644 --- a/2-classes-db/task.md +++ b/2-classes-db/task.md @@ -21,45 +21,53 @@ To submit this homework write the correct commands after each question. ### 1. Show the date, transaction_no, description and amount for those transactions whose amount is between £30,000 and £31,000. ```sql - +SELECT date, transaction_no, description, amount FROM spends WHERE amount > 29999 AND amount < 31000; ``` ### 2. Show the date, transaction_no, supplier_inv_no, description and amount for those transactions whose description includes the word 'fee'. ```sql - +SELECT date, transaction_no, supplier_inv_no, description, amount FROM spends WHERE description LIKE '%fee%'; ``` ### 3. Show the date, transaction_no, supplier_inv_no, description and amount for those transactions whose description includes the word 'Fee'. ```sql - +SELECT date, transaction_no, supplier_inv_no, description, amount FROM spends WHERE description LIKE '%Fee%'; ``` ### 4. Show the date, transaction_no, supplier_inv_no, description and amount for those transactions whose description includes the word 'fee' (case insensitive). You will need to search 'https://www.postgresql.org/docs/' to solve this. ```sql - +SELECT date, transaction_no, supplier_inv_no description, amount FROM spends WHERE description iLIKE lower(description) '%Fee%'; ``` ### 5. Show the date, transaction_no, supplier_inv_no, description and amount for those transactions whose amount is £25,000, £30,000, £35,000 or £40,000. ```sql - + SELECT date, transaction_no, supplier_inv_no, description, amount FROM spends WHERE amount IN (25000, 30000, 35000, 40000); ``` ### 6. Show the date, the supplier_id, the description and the amount for transactions with the expense area of 'Better Hospital Food'. You could do a query to get the expense_area_id first then do a query to find the dates, supplier_ids and amounts. But it would be better to do this all in one query by linking the tables together using INNER JOINs. ```sql - +SELECT spends.date, spends.supplier_id, spends.description, spends.amount, expense_areas.expense_area FROM spends INNER JOIN expense_areas ON spends.expense_area_id = expense_areas.id WHERE expense_areas.expense_area = 'Better Hospital Food'; ``` + ### 7. Show the date, supplier name, description and amount for transactions with the expense area of 'Better Hospital Food'. You will need to INNER JOIN another table to be able to do this. ```sql - +SELECT spends.date, suppliers.supplier, spends.description, spends.amount, expense_areas.expense_area +FROM spends +INNER JOIN expense_areas ON spends.expense_area_id = expense_areas.id +INNER JOIN suppliers ON spends.supplier_id = suppliers.id +WHERE expense_areas.expense_area = 'Better Hospital Food'; ``` ### 8. We have just received a late invoice for April! Add a new row to the spends table: - dated 1st April 2021 - with a description of 'Computer Hardware Dell' - transaction number is 38104091 and the supplier's inv no is '3780119655' - the supplier is 'COMPUTACENTER (UK) LTD' (id 16) - the expense type is 'Computer Hardware Purch' (id 7) - the expense area is 'ICT Contingency' (id 18) - for £32,000. + date is 01-04-2021 + description is 'Computer Hardware Dell' + transaction_no is 38104091 + supplier_inv_no is '3780119655' + supplier is 'COMPUTACENTER (UK) LTD' and supplier_id is 16 + expense_type is 'Computer Hardware Purch' expense_type_id is 7 + expense_area is 'ICT Contingency' expense_area_id is 18 + ammount is £32,000. ```sql - +INSERT INTO spends (date, description, transaction_no, supplier_inv_no, supplier_id, expense_type_id, expense_area_id, amount) +VALUES ('2021-04-01', 'Computer Hardware Dell', '38104091', '3780119655', 16, 7, 18, 32000); ``` ### 9. If you examine the dates in the data, you will see they all are dated either 1st march 2021 or 1st April 2021. So if we group on the the date, there will only be two groups. Show the date and the total amount spent on that date for these two dates by using a GROUP BY clause. ```sql +SELECT date, SUM(amount) FROM spends WHERE date = '2021-03-01' OR date='2021-04-01' GROUP BY date; ``` ### 10. (optional) Great we now know the monthly spend. But it didn't look that good. So I've changed my SELECT query to output this instead: