Information Resource Engineering SQL4. Recap - Ordering Output Usually, the order of rows returned...
-
Upload
helena-franklin -
Category
Documents
-
view
220 -
download
0
Transcript of Information Resource Engineering SQL4. Recap - Ordering Output Usually, the order of rows returned...
Recap - Ordering Output Usually, the order of rows returned in a
query result is undefined.
The ORDER BY clause sets the sequence for outputting selected information.
This can either be: Ascending order ASC (default)
Descending order DESC.
If used the ORDER BY must always be the last clause in the SELECT command.
Aggregate (Group) Functions Aggregate functions perform an operation
on a group of rows and return one result. SUM () gives the total of a group of rows,
(satisfying any conditions) of the given column, where the given column is numeric.
AVG () gives the average of the given column. MAX () gives the largest figure in the given
column. MIN () gives the smallest figure in the given
column. COUNT(*) gives the number of rows satisfying
the conditions.
Aggregate (Group) Functions
This query returns how many books are in the Book table.
SELECT COUNT(*) AS CountFROM Book;
Book
Resulting Output
Count
5
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
We could also use an attribute name in the Count function and it would return the same result.
SELECT COUNT(BookName) AS CountFROM Book;
Book Resulting Output
Count
5
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
We could also a different alias and it would return the same result.
SELECT COUNT(BookName) AS NumofRecordsFROM Book;
Book Resulting Output
NumofRecords
5
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
This query returns the total price of all the books in the Book table.
SELECT SUM (Price) AS Sum
FROM Book;
Book
Resulting Output
Sum
44.00
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
This query returns the average price of a book in the Book table.
SELECT AVG (Price) AS AveragePrice
FROM Book;
Book
Resulting Output
AveragePrice
8.80
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
This query returns the Minimum price of a book in the Book table.
SELECT MIN (Price) AS MinimumPrice
FROM Book;
Book
Resulting Output
MinimumPrice
6.00
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
This query returns the Maximum price of a book in the Book table.
SELECT MAX (Price) AS MaximumPrice
FROM Book;
Book
Resulting Output
MaximumPrice
14.00
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
You can return more than one aggregate function on the same attribute.
SELECT SUM (Price) AS Total, AVG (Price) AS Ave
FROM Book;
Book Resulting Output
Total Ave
44.00 8.80
ISBN BookName Price
0747550999 Harry Potter 7.00
0575049802 Witches Abroad 6.00
0509856745 Guards Guards 7.00
0497389956 Bart’s World 10.00
0497563856 The Bone Collector
14.00
Aggregate (Group) Functions
You can return more than one aggregate function, and not all the aggregate functions need be on the same attribute.
SELECT SUM (NoInStock) AS TotalStock,
AVG (Price) AS AvePrice
FROM Book;
Book Resulting Output
TotalStock AvePrice
44 8.80
ISBN BookName PriceNoInStock
0747550999
Harry Potter 7.00 10
0575049802
Witches Abroad 6.00 12
0509856745
Guards Guards 7.00 7
0497389956
Bart’s World 10.00 10
0497563856
The Bone Collector
14.00 5
Aggregate (Group) Functions
We can use Where statements to limit the rows selected
SELECT SUM (Price) AS Total, AVG (Price) AS Ave
FROM Book
WHERE Type = ‘Hardback’;
Book
Resulting Output
Total Ave
24.00 12.00
ISBN BookName Price Type
0747550999
Harry Potter 7.00 Softback
0575049802
Witches Abroad 6.00 Softback
0509856745
Guards Guards 7.00 Softback
0497389956
Bart’s World 10.00
Hardback
0497563856
The Bone Collector
14.00
Hardback
Aggregate (Group) FunctionsUsing the following table, produce the SQL statement that would output the minimum, maximum and average monthly salary of Programmers.
Employee
EmpNo
NameDeptNo
JobMonthlySalary
E001 Fred D1 Salesperson
1,500.00
E002 BamBam
D1 Analyst 2,300.00
E003 Barney D3 Programmer
1,700.00
E004 Wilma D2 Manager 2,000.00
E006 Pebbles
D3 Programmer
1,200.00
E007 Betty D2 Salesperson
1,400.00
E009 Dino D4 Manager 2,500.00
Aggregate (Group) Functions
Trying to return the most expensive book by Type
SELECT Type, MAX (Price) AS Maximum
FROM Book;
Book
ERROR
The command is invalid because Type has a value for each row in the table, while MAX (Price) has a single value for the whole table.
Hardback
Hardback
Softback
Softback
Softback
Type
14.00
The Bone Collector
0497563856
10.00
Bart’s World0497389956 7.00Guards Guards0509856745 6.00Witches Abroad0575049802
7.00Harry Potter0747550999
PriceBookNameISBN
Aggregate (Group) Functions Therefore, if we want to use
aggregate functions on ‘groups’ of rows in the table, we need to use the GROUP BY clause.
This selects the maximum price of each type of book in the table.
SELECT Type, MAX (Price) AS Maximum
FROM Book
GROUP BY Type;
Aggregate (Group) Functions
Think of it as the table being sorted by the attribute we are ‘grouping’ by
Then each group is treated as a separate table.
THIS DOES NOT HAPPEN PHYSICALLY!!
Book
ISBN BookName Price Type
0747550999
Harry Potter 7.00 Softback
0575049802
Witches Abroad 6.00 Softback
0509856745
Guards Guards 7.00 Softback
0497389956
Bart’s World 10.00
Hardback
0497563856
The Bone Collector
14.00
Hardback
Aggregate (Group) Functions
Returning the most expensive book by Type
SELECT Type, MAX (Price) AS Maximum
FROM Book
GROUP BY Type;
TypeMaximum
Hardback
14.00
Softback 7.00
ResultBook
ISBN BookName Price Type
0747550999
Harry Potter 7.00 Softback
0575049802
Witches Abroad 6.00 Softback
0509856745
Guards Guards 7.00 Softback
0497389956
Bart’s World 10.00
Hardback
0497563856
The Bone Collector
14.00
Hardback
Aggregate (Group) Functions
Adding another column, Cat, to the table specifying whether the book was a children’s or adult book:
Book
Hardback
Hardback
Softback
Softback
Softback
Type
14.00
The Bone Collector
0497563856
10.00
Bart’s World0497389956 7.00Guards Guards0509856745 6.00Witches Abroad0575049802
7.00Harry Potter0747550999
PriceBookNameISBN
A
A
A
C
C
Cat
Aggregate (Group) Functions The Group By clause causes the table to be ‘split’
(not physically) into related chunks. I.e related by the attribute(s) specified in the Group By clause.
Book
ISBN BookName Price Type Cat
0747550999
Harry Potter 7.00 Softback C
0575049802
Witches Abroad 6.00 Softback A
0509856745
Guards Guards 7.00 Softback A
0497389956
Bart’s World 10.00
Hardback
C
0497563856
The Bone Collector
14.00
Hardback
A
Group By Type
Aggregate (Group) Functions The Group By clause causes the table to be ‘split’
(not physically) into related chunks. I.e related by the attribute(s) specified in the Group By clause.
Book
ISBN BookName Price Type Cat
0747550999
Harry Potter 7.00 Softback C
0497389956
Bart’s World 10.00
Hardback
C
0575049802
Witches Abroad 6.00 Softback A
0509856745
Guards Guards 7.00 Softback A
0497563856
The Bone Collector
14.00
Hardback
A
Group By Cat
Aggregate (Group) Functions
Book
Produce the SQL statement to display the maximum book price for each category.
Hardback
Hardback
Softback
Softback
Softback
Type
14.00
The Bone Collector
0497563856
10.00
Bart’s World0497389956 7.00Guards Guards0509856745 6.00Witches Abroad0575049802
7.00Harry Potter0747550999
PriceBookNameISBN
A
A
A
C
C
Cat
Aggregate (Group) Functions Therefore, if we try this SQL
statement:
We will receive an error message. We are trying to perform an aggregate function on an ‘ungrouped’ attribute.
SELECT Cat, MAX (Price) AS Maximum
FROM Book
GROUP BY Type;
Aggregate (Group) Functions However, if we wanted the maximum price of each
category of each type of book, we could use more than one attribute in the Group By clause:
SELECT Type,Cat MAX (Price) AS Maximum
FROM Book
GROUP BY Type,Cat;
Book
A
C
A
A
C
Cat
Hardback
Hardback
Softback
Softback
Softback
Type
14.00
The Bone Collector
0497563856
10.00
Bart’s World0497389956
7.00Guards Guards0509856745
6.00Witches Abroad0575049802
7.00Harry Potter0747550999
PriceBookNameISBN
Type Cat Maximum
Hardback
A 14.00
Hardback
C 12.00
Softback
A 7.00
Softback
C 7.00
Result
CHardback
12.00
The Amber Spyglass
0729863856 CSoftback 4.00Art Attack0997563856
Aggregate (Group) Functions However, if we wanted the maximum price of each
category of each type of book, we could use more than one attribute in the Group By clause:
SELECT Type,Cat,MAX (Price) AS Maximum
FROM Book
GROUP BY Type,Cat;
Book
A
C
A
A
C
Cat
HardbackHardback
Softback
Softback
Softback
Type
14.00
The Bone Collector
0497563856 10.0
0Bart’s World049738995
6
7.00Guards Guards0509856745
6.00Witches Abroad0575049802
7.00Harry Potter0747550999
PriceBookNameISBN
Type Cat Maximum
Hardback
A 14.00
Hardback
C 12.00
Softback
A 7.00
Softback
C 7.00
Result
CHardback
12.00
The Amber Spyglass
0729863856
CSoftback 4.00Art Attack0997563856
Aggregate (Group) Functions However, if we wanted the maximum price of each
category of each type of book, we could use more than one attribute in the Group By clause:
SELECT Type,Cat,MAX (Price) AS Maximum
FROM Book
GROUP BY Type,Cat;
Book
A
C
A
A
C
Cat
HardbackHardback
Softback
Softback
Softback
Type
14.00
The Bone Collector
0497563856 10.0
0Bart’s World049738995
6
7.00Guards Guards0509856745
6.00Witches Abroad0575049802
7.00Harry Potter0747550999
PriceBookNameISBN
12.00CHardback
Hardback
Softback
Type
A
C
Cat
14.00
7.00
Maximum
Result
CHardback
12.00
The Amber Spyglass
0729863856
CSoftback 4.00Art Attack0997563856
Softback
A 7.00
Aggregate (Group) FunctionsUsing the following table, produce the SQL statement that would output the minimum, maximum and average monthly salary of all job types.
Employee
EmpNo
Name JobMonthlySalary
E001 Fred Salesperson
1,500.00
E002 BamBam
Analyst 2,300.00
E003 Barney Programmer
1,700.00
E004 Wilma Manager 2,000.00
E006 Pebbles
Programmer
1,200.00
E007 Betty Salesperson
1,400.00
E009 Dino Manager 2,500.00
HAVING The HAVING clause is designed for use
with the GROUP BY clause. WHERE ‘filters’ individual rows. HAVING ‘filters’ groups.
I.e the HAVING clause allows you to restrict the groups you return.
It is usual, and logical, to include the GROUP BY clause before the HAVING clause.
Groups are formed and group functions calculated before the HAVING clause is applied to a SELECT group for output.
Aggregate (Group) Functions
EnrolmentNo
Name CourseCode
S01 Homer C001
S02 Marge C002
S03 Lisa C006
S04 Bart C003
S05 Maggie C002
S06 Mo C001
S07 Apu C003
S08 Skinner C001
S09 Millhouse
C004
S10 Lennie C002SELECT CourseCode, COUNT (EnrolmentNo) AS ‘NoOfStudents’
FROM Student
GROUP BY CourseCode;
ResultCourseCode
NoOfStudents
C001 3
C002 3
C006 1
C003 2
C004 1
Student
Aggregate (Group) Functions
EnrolmentNo
Name CourseCode
S01 Homer C001
S02 Marge C002
S03 Lisa C006
S04 Bart C003
S05 Maggie C002
S06 Mo C001
S07 Apu C003
S08 Skinner C001
S09 Millhouse
C004
S10 Lennie C002SELECT CourseCode, COUNT (EnrolmentNo) AS ‘NoOfStudents’
FROM Student
GROUP BY CourseCode
HAVING COUNT (EnrolmentNo) > 1;
ResultCourseCode
NoOfStudents
C001 3
C002 3
C003 2
Student
Returning courses with more than 1 student on them:
Aggregate (Group) FunctionsUsing the following table, produce the SQL statement that would output those departments that have only 1 person employed in that department
Employee
EmpNo NameDeptNo
JobMonthlySalary
E001 Fred D1 Salesperson
1,500.00
E002 BamBam
D1 Analyst 2,300.00
E003 Barney D3 Programmer
1,700.00
E004 Wilma D2 Manager 2,000.00
E006 Pebbles D3 Programmer
1,200.00
E007 Betty D2 Salesperson
1,400.00
E009 Dino D4 Manager 2,500.00
Aggregate (Group) FunctionsUsing the following table, produce the SQL statement that would output those departments whose monthly wage bill is greater than £3,000.
(Monthly wage bill = sum of all employees monthly salaries in that department)
Employee
EmpNo NameDeptNo
JobMonthlySalary
E001 Fred D1 Salesperson
1,500.00
E002 BamBam
D1 Analyst 2,300.00
E003 Barney D3 Programmer
1,700.00
E004 Wilma D2 Manager 2,000.00
E006 Pebbles D3 Programmer
1,200.00
E007 Betty D2 Salesperson
1,400.00
E009 Dino D4 Manager 2,500.00