5.5 KiB
5.5 KiB
Lecture 1 (13:00) - More SQL
Attribute Alias
SELECT driverID AS DriverNumber
FROM Drivers;
Distinct
SELECT address FROM Drivers WHERE DriverID IN (D020, D030)
Output:
address |
---|
Dundee |
Dundee |
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
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
SELECT <attributes>, <column functions>
FROM <tables>
WHERE <conditions> //Optional
GROUP BY <attributes>
HAVING <conditions>;
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
UPDATE <table>
SET <attribute assignments>
WHERE <conditions>; //optional
UPDATE Drivers
SET points = 6
WHERE driverID = "D040";
Output:
UPDATE Drivers
SET address = "Salford"
WHERE driverID = "D020";
Delete Queries
- Deletes whole rows ( tuples ), not individual values
DELETE FROM <table name>
WHERE <conditions>; //optional
DELETE FROM Drivers
WHERE points > 10;
Output
Insert Queries
- Adds data to existing table without deleting it or any records
INSERT INTO <table name> <attribute list> //optional
SELECT <statement> | VALUES <value list>;
INSERT INTO Drivers
VALUES ("D060", "Betty", "Inverness", 6);
Output
INSERT INTO Drivers (driverID, name)
VALUES ("D070", "Jeannie")
Output
Lecture 2 (15:00) - More SQL
Data Types
CHARACTER(L)
- Fixed length character string containing L letters. Fewer characters are used as padding.
CHARACTER VARYING(L)
- Variable length character string that holds up to L characters. No padding.
INTEGER(L)
- Signed whole number.
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
to999.99
DATE YYYY-MM-DD
- YYYY =
1
to9999
- MM =
1
to12
- DD =
1
to `31
TIME HH:MM:SS
- HH = Hours
- MM = Minutes
- SS = Seconds
TIMESTAMP YYYY-MM-DD HH:MM:SS
- Combination of DATE and TIME
INTERVAL
- Refers to period of time. May have value such as
90 days
. Also known as a span
BINARY LARGE OBJECT(L)
- Large, variable length binary string that can hold up to L bytes. Also known as
BLOB
BOOLEAN
- Value of TRUE or FALSE
NULL
- Value of unknown, valid for any data type.
Exercise
- CHAR
- DATE
- BOOLEAN
- INTEGER
- NUMERIC
- CHAR(10) = Bryant____
- VARCHAR(10) = Bryant
- INTEGER can have the value NULL
Exercise on Data Types and Keys
- Book
- ISBN - VARCHAR
- Title - VARCHAR
- Author - VARCHAR
- Members
- MemID - CHAR
- Name - VARCHAR
- Age - INTEGER
- Address - VARCHAR
- Phone - VARCHAR
- Loan
- LoanID - CHAR
- ISBN ( Foreign Key )
- MemID ( Foreign Key )
- LoanDate - DATE
- ReturnDate - DATE
Exercise on Adding, Deleting and Modifying Data
- Append tuple to pirates table
INSERT INTO Pirates
VALUES ("p5", "Pegleg", 1, 2, "Spain");
INSERT INTO Boats (boat-code, boat-name, origin)
VALUES ("b9", "Swift", "British");
INSERT INTO Booty
VALUES ("bt13", "Flour", 8);
- Change the name of Booty "bt8" from "nothing" to "infected animals"
UPDATE Booty
SET booty-name = "Infected Animals"
WHERE booty-code = "bt8";
- Remove b7 from the boats table
DELETE FROM Boats
WHERE boat-code = "b7";
Exercise on Aggregating Results
Exercise
- Calculate total value of all booty in booty table
SELECT SUM(value)
FROM booty;
- List each type of boat, with the number of boats with that type
SELECT boat-type, COUNT (boat-code)
FROM boats;
- Remember comma between select and aggregate functions
Workshop 1 (10:00)
- List the unit price of the cheapest product.
- What is the total value of all the products?
- How many products have been discontinued?
- What is the average unit price of the products which have been discontinued?
- Display the name of every supplier and the total number of products supplied by
that company.
- Display total number of orders for each employee.