Skip to content
This repository was archived by the owner on Aug 17, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion 1-reading/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
2. Study the PostgreSQL documentation that covers provided functions and operators in SQL (https://www.postgresql.org/docs/current/functions.html):
1. Read the String Functions and Operators section.
2. Read the Date/Time Functions and Operators section.
3. Study the build-hotel.sql script used to create the sample database. Compare it with the struture of the tables themselves.
3. Study the build-hotel.sql script used to create the sample database. Compare it with the structure of the tables themselves.

43 changes: 33 additions & 10 deletions 2-classes-db/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,49 @@ 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 BETWEEN 30000 AND 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 LIKE LOWER('%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 date,supplier_id,description,amount
FROM spends
INNER JOIN expense_areas
ON spends.id = expense_areas.id
WHERE expense_areas.id = (SELECT id FROM expense_areas WHERE 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 date,supplier_id,description,amount
FROM spends
INNER JOIN suppliers
ON spends.id = suppliers.id
WHERE suppliers.id = (SELECT id FROM suppliers WHERE supplier = '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
Expand All @@ -56,11 +74,14 @@ To submit this homework write the correct commands after each question.
the expense area is 'ICT Contingency' (id 18)
for £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, 32);
```
### 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
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:
```
Expand All @@ -73,7 +94,9 @@ To submit this homework write the correct commands after each question.
Can you work out how to do this?

```sql

SELECT MONTH(date) AS Month, SUM(amount) AS Monthly Spend
FROM spends
GROUP BY MONTH(date);
```

When you have finished all of the questions - open a pull request with your answers to the `SQL-Coursework-Week1` repository.