## Lecture 1 (13:00) - More SQL ### Attribute Alias ```sql SELECT driverID AS DriverNumber FROM Drivers; ``` ### Distinct ```sql SELECT address FROM Drivers WHERE DriverID IN (D020, D030) ``` #### Output: | address | |---------| | Dundee | | Dundee | ```sql SELECT DISTINCT address FROM Drivers WHERE DriverID IN (D020, D030) ``` #### Output: | address | |---------| | Dundee | ### Aggregate Functions - **sum()** - Adds all inputs - **avg()** - Mean average of all inputs - **count()** - Count total records of input - **max()** - Get maximum value of inputs - **min()** - Get minimum value of inputs ### Group By ```sql SELECT address, COUNT(niNo) FROM Employees GROUP BY address; ``` #### Output: | Address | count(niNo) | |---------|--------------| | Aberdeen | 2 | | Dundee | 2 | | Stirling | 1 | ### Having Clause - Used with group by to filter groups / rows ```sql SELECT , FROM WHERE //Optional GROUP BY HAVING ; ``` ![](Pasted%20image%2020231003133326.png) ```sql SELECT address, AVG(salary) FROM Employees GROUP BY address HAVING AVG(salary) > 21000; ``` #### Output: | address | avg(salary) | |---|----| | Dundee | 22500 | ### Having Vs. Where - **WHERE** removes rows *before* grouping - **HAVING** filters groups - Aggregate functions cannot be used with the **WHERE** clause. ### Update Queries - Allows you to change the values of attributes of existing rows ( tuples ) of a table ```sql UPDATE SET WHERE ; //optional ``` ```sql UPDATE Drivers SET points = 6 WHERE driverID = "D040"; ``` #### Output: ![](Pasted%20image%2020231003134123.png) ```sql UPDATE Drivers SET address = "Salford" WHERE driverID = "D020"; ``` ![](Pasted%20image%2020231003134231.png) ### Delete Queries - Deletes whole rows ( tuples ), not individual values ```sql DELETE FROM
WHERE ; //optional ``` ```sql DELETE FROM Drivers WHERE points > 10; ``` #### Output ![](Pasted%20image%2020231003134528.png) ### Insert Queries - Adds data to existing table without deleting it or any records ```sql INSERT INTO
//optional SELECT | VALUES ; ``` ```sql INSERT INTO Drivers VALUES ("D060", "Betty", "Inverness", 6); ``` #### Output ![](Pasted%20image%2020231003134918.png) ```sql INSERT INTO Drivers (driverID, name) VALUES ("D070", "Jeannie") ``` #### Output ![](Pasted%20image%2020231003135010.png) ## Lecture 2 (15:00) - More SQL ### Data Types ```sql CHARACTER(L) ``` - Fixed length character string containing L letters. Fewer characters are used as padding. ```sql CHARACTER VARYING(L) ``` - Variable length character string that holds up to L characters. No padding. ```sql INTEGER(L) ``` - Signed whole number. ```sql NUMERIC(P,S) ``` - P = Precision. Total number of digits - S = Scale. Number of digits to correct decimal place. - Ex. NUMERIC(5,2) is `-999.99` to `999.99` ```sql DATE YYYY-MM-DD ``` - YYYY = `1` to `9999` - MM = `1` to `12` - DD = `1` to `31 ```sql TIME HH:MM:SS ``` - HH = Hours - MM = Minutes - SS = Seconds ```sql TIMESTAMP YYYY-MM-DD HH:MM:SS ``` - Combination of DATE and TIME ```sql INTERVAL ``` - Refers to period of time. May have value such as `90 days`. Also known as a span ```sql BINARY LARGE OBJECT(L) ``` - Large, variable length binary string that can hold up to L bytes. Also known as `BLOB` ```sql BOOLEAN ``` - Value of TRUE or FALSE ```sql NULL ``` - Value of unknown, valid for any data type. #### Exercise 1. CHAR 2. DATE 3. BOOLEAN 4. INTEGER 5. NUMERIC 6. CHAR(10) = Bryant____ 7. VARCHAR(10) = Bryant 8. INTEGER can have the value NULL ### Exercise on Data Types and Keys 1. Book 1. ISBN - VARCHAR 2. Title - VARCHAR 3. Author - VARCHAR 2. Members 1. MemID - CHAR 2. Name - VARCHAR 3. Age - INTEGER 4. Address - VARCHAR 5. Phone - VARCHAR 3. Loan 1. LoanID - CHAR 2. ISBN ( Foreign Key ) 3. MemID ( Foreign Key ) 4. LoanDate - DATE 5. ReturnDate - DATE ### Exercise on Adding, Deleting and Modifying Data 1. Append tuple to pirates table ```SQL INSERT INTO Pirates VALUES ("p5", "Pegleg", 1, 2, "Spain"); ``` ```SQL INSERT INTO Boats (boat-code, boat-name, origin) VALUES ("b9", "Swift", "British"); ``` ```SQL INSERT INTO Booty VALUES ("bt13", "Flour", 8); ``` 1. Change the name of Booty "bt8" from "nothing" to "infected animals" ```sql UPDATE Booty SET booty-name = "Infected Animals" WHERE booty-code = "bt8"; ``` 1. Remove b7 from the boats table ```sql DELETE FROM Boats WHERE boat-code = "b7"; ``` ### Exercise on Aggregating Results #### Exercise 1. Calculate total value of all booty in booty table ```sql SELECT SUM(value) FROM booty; ``` 1. List each type of boat, with the number of boats with that type ```sql SELECT boat-type, COUNT (boat-code) FROM boats; ``` - Remember comma between select and aggregate functions ## Workshop 1 (10:00) ![](Pasted%20image%2020231006102147.png) 1. List the unit price of the cheapest product. ![](Pasted%20image%2020231006102426.png) 2. What is the total value of all the products? ![](Pasted%20image%2020231006102826.png) 3. How many products have been discontinued? ![](Pasted%20image%2020231006102943.png) 4. What is the average unit price of the products which have been discontinued? ![](Pasted%20image%2020231006103041.png) 5. Display the name of every supplier and the total number of products supplied by that company. ![](Pasted%20image%2020231006104220.png) 6. Display total number of orders for each employee. ![](Pasted%20image%2020231006104753.png)