1 more sql aggregates, ordering, grouping, subqueries and data definition lecture 6
TRANSCRIPT
1
More SQL
Aggregates, Ordering, Grouping, Subqueries and Data Definition
Lecture 6
2
Aggregates
SELECT MAX(Fine) as maxfine
FROM Loan;
Loan
Loan# catno Memno LoanDate DueDate Fine
L0002 B0001 M0001 05/10/97 04/12/97 £62.10
L0003 B0002 M0001 05/12/97 05/03/98 £53.00
L0004 B0003 M0001 05/12/97 05/03/98 £53.00
L0006 B0004 M0002 13/12/97 13/03/98 £52.20
L0008 B0000 M0002 16/01/98 16/04/98 £48.80
L0009 B0005 M0003 18/08/99 18/11/99 £75.00
L0010 B0006 M0004 19/08/99 20/11/99 NULL
SELECT Count(*) FROM Loan;
maxfine £75.00
£354:10
£59.02
6
7
SELECT SUM(Fine) FROM Loan;
SELECT AVG(Fine) FROM Loan;
SELECT Count(Fine) FROM Loan;
3
Ordering
SELECT Memno, Fine
FROM Loan
ORDER BY Memno, Fine;
Memno Fine
M0001 £53.00
M0001 £53.00
M0001 £62.10
M0002 £48.80
M0002 £52.20
M0003 £75.00
Memno Fine
M0001 £62.10
M0001 £53.00
M0001 £53.00
M0002 £52.20
M0002 £48.80
M0003 £75.00
SELECT Memno, Fine FROM LoanORDER BY Memno, Fine DESC;
4
Grouping
SELECT Memno, COUNT(*) AS num_loans
FROM Loan
GROUP BY Memno;
Loan# Book# Memno L0002 B0001 M0001
L0003 B0002 M0001
L0004 B0003 M0001
L0006 B0004 M0002
L0008 B0000 M0002
Memno num_loans M0001 3 M0002 2
How many loans does each member have?
5
Grouping
SELECT memno, SUM(fine) AS total_fine
FROM Loan
GROUP BY memno ;
memno catno fine
M0001 B0002 £53.00
M0001 B0003 £53.00
M0002 B0004 £52.20
M0003 B0005 £75.00
memno total_fineM0001 £106.00M0002 £52.20M0003 £75.00
What is the total fine paid by each member?
6
Warning about Grouping
The Select attributes can only contain the attribute grouped on + aggregate functions
memno catno fine
M0001 B0002 £53.00
M0001 B0003 £53.00
M0002 B0004 £52.20
M0003 B0005 £75.00
memno memfine
M0001 £106.00
M0002 £52.20
M0003 £75.00
CatnoB002, B003B004B005
SELECT memno, sum(fine) as memfine, catnoFROM Loans GROUP BY memno
7
Condition on the group - Having
SELECT memno, sum(fine) as memfine
FROM Loans
GROUP BY memno
HAVING sum(fine) > 100 ;
memno catno fine
M0001 B0002 £53.00
M0001 B0003 £53.00
M0002 B0004 £52.20
M0003 B0005 £75.00
memno memfine
M0001 £106.00
What is the total fine paid by each member? Only display members with total fine > £100.
8
Subqueries
9
Subqueries
Bookscatno title author publisher categoryC100 Physics Handbook Jones Wiley PhysicsC200 Simply the Best Advacaat Rangers FootballC300 Database Design Wilson McCall ComputingC400 Business Society Neal Wiley BusinessC500 The Metro Abbey Wiley LeisureC600 Graphics Sedge Maxwell ComputingC700 Cell Biology Norton West Biology
Membersmemno name address ageM100 Fred Aberdeen 22M150 Colin Stirling 31M200 Dave Dundee 21M250 Betty Aberdeen 67M300 Jean Dundee 17
Loanscatno memno borrowed date_ret fineC100 M100 12/09/01 20/09/01 NULLC300 M100 01/09/01 NULL NULLC400 M200 04/06/01 16/09/01 £16.30C500 M200 04/08/01 16/09/01 £16.30C600 M250 02/10/01 24/10/01 £30.00C700 M300 10/09/01 19/10/01 NULL
Consider the following tables from which we will do subqueries:
10
Subqueries
catno title author publisher category catno memno borrowed date_ret fine memno name address age C100 Physics Handbook Jones Wiley Physics C100 M100 12/09/01 20/09/01 NULL M100 Fred Aberdeen 22C300 Database Design Wilson McCall Computing C300 M100 01/09/01 NULL NULL M100 Fred Aberdeen 22C400 Business Society Neal Wiley Business C400 M200 04/06/01 16/09/01 £16.30 M200 Dave Dundee 21C500 The Metro Abbey Wiley Leisure C500 M200 04/08/01 16/09/01 £16.30 M200 Dave Dundee 21C600 Graphics Sedge Maxwell Computing C600 M250 02/10/01 24/10/01 £30.00 M250 Betty Aberdeen 67 C700 Cell Biology Norton West Biology C700 M300 10/09/01 19/10/01 NULL M300 Jean Dundee 17
Let’s say you wanted the names of all members who have borrowed abusiness or a computing book - a possible query is as follows:
SELECT nameFROM Books, Members, Loans WHERE Books.catno = Loans.catno AND Members.memno = Loans.memno
AND category IN (“Business”, “Computing”);
The problem here is that the join in the query (i.e Book.catno = Loan.catno AND Member.memno = Loan.memno) creates the intermediate table as shown below:
With more loans the above table can become huge - this is inefficient - better to use subqueries
11
Subqueries
• Subqueries are SELECT statements embedded within another SELECT statement– the results of the inner SELECT statement (or
subselect) are used in the outer statement to help determine the contents of the final result
• inner to outer means evaluating statements from right to left
• a subselect can be used in the WHERE and HAVING clauses of an outer SELECT statement
12
Subqueries
• Subqueries can be used with a number of operators:– Relational operators (=, <, >, <=, >=, < >)– IN, NOT IN– ALL – SOME, ANY – EXISTS, NOT EXISTS
13
Relational and Aggregate OperatorsRelational Operators (=, <, >, <=, >=, <>) can only be used if the result of the subquery returns a single value i.e subquery must be a scalar subquery.
What is the name of the oldest Member
SELECT name FROM MembersWHERE age = (SELECT MAX(age)
FROM Members);
SELECT name FROM MembersWHERE age = 67;
Which equates to:
SELECT MAX(age)FROM Members;
Scalar subquery returns 67
In general, relational operators are used in conjunction with aggregate operators i.e sum, avg, count, max, min
EXAMPLE
14
IN and NOT IN Operators
Let us again say you wanted the names of all members who have borrowed a business or a computing book - a possible solution using subqueries and the IN operator
SELECT nameFROM MembersWHERE memno IN (SELECT memno
FROM Loans WHERE catno IN (SELECT catno
FROM Books WHERE category IN (‘Business’, ‘Computing’)));
Works backwardsi.e from inner toouter statements
15
IN and NOT IN Operators
SELECT catno FROM BookWHERE category IN (‘Business’, ‘Computing’);
SELECT nameFROM MembersWHERE memno IN {M100, M200, M250};
SELECT memno FROM LoanWHERE catno IN {C300, C400, C600};
Table subquery returns {C300,C400,C600}
Table subquery returns {M100, M200, M250}
The previous query works as follows:
16
ALL OperatorThe ALL operator may be used with subqueries that produce a singlecolumn of numbers.If the subquery is preceded by the keyword ALL, the condition willonly be TRUE if it is satisfied by all the values produced by the subquery
EXAMPLEWhat is the name of the oldest member
SELECT name FROM MemberWHERE age >= ALL (SELECT age FROM Member);
SELECT name FROM MemberWHERE age >= ALL {22, 31, 21, 67, 17};
SELECT age FROM Member;
look for the rows in Members whose ageis greater than or equal to all the values in list
17
ExampleStaff (staffNo, staffName, salary, branchNo*)Branch (branchNo, branchAddress)
• What does this query do?
Select staffName, salaryFrom StaffWhere salary > ALL (Select salary
From Staff Where branchNo = ‘B003’);
List all staff whose salary is larger than the salary of every member of staff at branch B003.
18
SOME/ANY OperatorThe SOME operator may be used with subqueries that produce a single column of numbers. SOME and ANY can be used interchangeably.
If the subquery is preceded by the keyword SOME, the condition will only be TRUE if it is satisfied by any (one or more) values produced by the subquery
EXAMPLEList the names of members who have borrowed books (i.e., members who appear in the Loan table)
SELECT name FROM MemberWHERE memno = ANY (SELECT DISTINCT memno FROM Loans);
SELECT name FROM MemberWHERE memno = ANY (“M100”, “M200”, “M250”, “M300”);
SELECT DISTINCT memno FROM Loans
19
ExampleStaff (staffNo, staffName, salary, branchNo*)Branch (branchNo, branchAddress)
• What does this query do?
Select staffName, salaryFrom StaffWhere salary > ANY (Select salary
From Staff Where branchNo = ‘B003’);
List all staff whose salary is larger than the salary of at least one member of staff at branch B003.
20
EXISTS and NOT EXISTS Operators -Correlated Queries
EXISTS and NOT EXISTS produce a simple TRUE/FALSE result.
EXISTS is TRUE if and only if there exists at least one row in the result table returned by the subquery; it is FALSE if the subquery returns an empty result table. NOT EXISTS is the opposite of EXISTS
EXAMPLE
List the titles that have been borrowed by members
SELECT titleFROM Book BWHERE EXISTS (SELECT *
FROM Loan L WHERE L.catno = B.catno);
The outer query iterates through all the books, testing if each book appears in the Loan table
21
Features of correlated queries
• A table in the outer query is referenced in the WHERE field of the inner query.
• The query runs by iterating though the records of the outer FROM table.
• The inner query is evaluated once for each record, and passes the result back to the outer query.
22
Some questions can be answered using joins or queries
SELECT Member.name
FROM Book, Member, Loan
WHERE Book.catno =Loan.catno
AND Member.memno=Loan.memno
AND Book.category IN ("History", "Computing") ;
memno memno catno catno
Member Loan Book List names of members who
have borrowed books on History or Computing
SELECT name FROM MemberWHERE memno IN (SELECT memno FROM Loan
WHERE catno IN (SELECT catno FROM BookWHERE category IN (“History”, “Computing”)));
23
Equivalent ways of using Subqueries
SELECT memno, fine
FROM Loan
WHERE fine <= ALL (SELECT fine FROM Loan);
SELECT memno, fine
FROM Loan
WHERE fine = (SELECT Min(fine) FROM Loan);
memno catno fine
M0001 B0002 £53.00
M0002 B0004 £52.00
M0003 B0005 £75.00
M0004 B0007 £26.00
Which member paid the smallest fine?
24
Exercise (May 2004 Exam)
Employee(empid, name)Project(projid, name)WorkLoad(empid*, projid*, duration)
• List the number of projects that each employee (empid) is working on.
• List, in descending order, employee names that are working on the project called “Databases”.
• List the employee (empid) who spent the longest duration working on a project.
• List the employees (name) who have not worked on any project.
25
SolutionSelect empid, count(projid)From WorkLoadGroup by empid;
Select E.nameFrom Employee E, Project P, WorkLoad LWhere E.empid=L.empid AND L.projid=P.projid AND P.name=’Databases’Order By E.name Desc;
Select empidFrom WorkLoad Where duration = ( Select Max(duration)
From WorkLoad);
Select nameFrom Employee EWhere Not Exists (Select *
From WorkLoad LWhere E.empid = L.empid);
26
SQL (Data Definition)
27
SQL Data Types
Data Type Declarations
Boolean BOOLEANCharacter CHAR VARCHARExact Numeric NUMERIC DECIMAL INTEGER
...Approx. Numeric FLOAT REAL …Date/Time DATE TIME …
28
Creating Domains and Tables
CREATE TABLE Dept (deptcode CHAR(3),
deptname CHAR(12));
CREATE TABLE Driver (first_name CHAR(12),
second_name CHAR(12),
age INTEGER(2)
);
CREATE DOMAIN name AS CHAR(12);
name
name,name,
29
Creating Domains and Tables
CREATE TABLE Student (identity student_id,
extension integer(4) UNIQUE,
student_name name NOT NULL);
CREATE DOMAIN student_id AS CHAR(5)CHECK (VALUE LIKE ‘S%’);
CREATE DOMAIN student_id AS CHAR (5)CHECK (VALUE LIKE ‘S_ _ _ _’);
S followed byany number of
characters
S followed by exactly four characters
Attribute name
Attribute domain
Constraint
30
Constraints in tablesCREATE TABLE Dept (
deptcode CHAR(3) CONSTRAINT dep_con1 PRIMARY KEY,deptname CHAR(12)
);
CREATE TABLE Dept (deptcode CHAR(3),deptname CHAR(12),CONSTRAINT dep_con1 PRIMARY KEY (deptcode)
);
CREATE TABLE Loan (bookcode CHAR(5),memname CHAR(15),constraint dep_con1 PRIMARY KEY (bookcode, memcode)
);Composite primary key must be entered as table constraint, i.e., separately from the attributes
Name of the constraint
31
More Constraints in tables
CREATE TABLE Staff (Staffcode CHAR(4),StaffTitleCHAR(3) CONSTRAINT s1_con
CHECK (StaffTitle IN (‘Mr’, ‘Ms’, ‘Dr’)) );
CREATE TABLE Staff (Staffcode CHAR(4),StaffTitleCHAR(3),CONSTRAINT s1_con CHECK (StaffTitle IN (‘Mr’, ‘Ms’, ‘Dr’))
);
32
Foreign Keys
CREATE TABLE Staff (Staffcode CHAR(4),StaffTitleCHAR(3),Dept CHAR(4) REFERENCES Department(Deptcode)
);
CREATE TABLE Department (Deptcode CHAR(4),Deptname CHAR(3),CONSTRAINT dep_con1 PRIMARY KEY (Deptcode)
);
CREATE TABLE Staff (Staffcode CHAR(4),StaffTitleCHAR(3),Dept CHAR(4),FOREIGN KEY (Dept) REFERENCES Department
);
Optional, if it’s the primary key
Can be multiple valued, to match composite primary key
33
Link Properties: On Delete, On Update
DID Name D1 Art D2 Computing
DeptSID Name S1 Fred S2 Bill S3 Jim
Staff
DID*D1D1D2
On delete: Cascade
NULL
On delete: Set Default
On delete: Set Null
Link properties
34
DID Name D1 Art D2 Computing
DeptSID Name S1 Fred S2 Bill S3 Jim
Staff
DID*D1D1D2
On update: Cascade
NULL
On delete: Set Default
On update: Set Null
D42D42D42 D79
Link properties
Link Properties: On Delete, On Update
35
Setting link properties in SQL
CREATE TABLE Department (Deptcode CHARACTER(4),Deptname CHARACTER(3),CONSTRAINT dep_con1 PRIMARY KEY (Deptcode)
);
CREATE TABLE Staff (Staffcode CHARACTER(4),StaffTitleCHARACTER(3),FOREIGN KEY (Dept) REFERENCES Department
ON DELETE SET NULLON UPDATE SET NULL
);
36
Modifying tables.• You can
– Add a column– Add/Delete rows– Add/Drop constraints
• This is not part of the syllabus, but you can refer to Lab 3 for more details…