Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause.

Similar presentations


Presentation on theme: "Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause."— Presentation transcript:

1 Information Resource Engineering SQL4

2 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.

3 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.

4 Aggregate (Group) Functions This query returns how many books are in the Book table. SELECT COUNT(*) AS Count FROM Book; Book Resulting Output Count 5 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

5 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 Count FROM Book; Book Resulting Output Count 5 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

6 Aggregate (Group) Functions We could also a different alias and it would return the same result. SELECT COUNT(BookName) AS NumofRecords FROM Book; Book Resulting Output NumofRecords 5 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

7 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 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

8 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 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

9 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 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

10 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 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

11 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 TotalAve 44.008.80 ISBNBookNamePrice 0747550999Harry Potter7.00 0575049802Witches Abroad6.00 0509856745Guards 7.00 0497389956Bart’s World10.00 0497563856The Bone Collector14.00

12 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 TotalStockAvePrice 448.80 ISBNBookNamePriceNoInStock 0747550999Harry Potter7.0010 0575049802Witches Abroad6.0012 0509856745Guards 7.007 0497389956Bart’s World10.0010 0497563856The Bone Collector14.005

13 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 TotalAve 24.0012.00 ISBNBookNamePriceType 0747550999Harry Potter7.00Softback 0575049802Witches Abroad6.00Softback 0509856745Guards 7.00Softback 0497389956Bart’s World10.00Hardback 0497563856The Bone Collector14.00Hardback

14 Aggregate (Group) Functions Using the following table, produce the SQL statement that would output the minimum, maximum and average monthly salary of Programmers. Employee EmpNoNameDeptNoJobMonthlySalary E001FredD1Salesperson1,500.00 E002BamBamD1Analyst2,300.00 E003BarneyD3Programmer1,700.00 E004WilmaD2Manager2,000.00 E006PebblesD3Programmer1,200.00 E007BettyD2Salesperson1,400.00 E009DinoD4Manager2,500.00

15 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 Softback Type 14.00The Bone Collector0497563856 10.00Bart’s World0497389956 7.00Guards 0509856745 6.00Witches Abroad0575049802 7.00Harry Potter0747550999 PriceBookNameISBN

16 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;

17 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 ISBNBookNamePriceType 0747550999Harry Potter7.00Softback 0575049802Witches Abroad6.00Softback 0509856745Guards 7.00Softback 0497389956Bart’s World10.00Hardback 0497563856The Bone Collector14.00Hardback

18 Aggregate (Group) Functions Returning the most expensive book by Type SELECT Type, MAX (Price) AS Maximum FROM Book GROUP BY Type; TypeMaximum Hardback14.00 Softback7.00 Result Book ISBNBookNamePriceType 0747550999Harry Potter7.00Softback 0575049802Witches Abroad6.00Softback 0509856745Guards 7.00Softback 0497389956Bart’s World10.00Hardback 0497563856The Bone Collector14.00Hardback

19 Aggregate (Group) Functions Adding another column, Cat, to the table specifying whether the book was a children’s or adult book: Book Hardback Softback Type 14.00The Bone Collector0497563856 10.00Bart’s World0497389956 7.00Guards 0509856745 6.00Witches Abroad0575049802 7.00Harry Potter0747550999 PriceBookNameISBN A A A C C Cat

20 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 ISBNBookNamePriceTypeCat 0747550999Harry Potter7.00SoftbackC 0575049802Witches Abroad6.00SoftbackA 0509856745Guards 7.00SoftbackA 0497389956Bart’s World10.00HardbackC 0497563856The Bone Collector14.00HardbackA Group By Type

21 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 ISBNBookNamePriceTypeCat 0747550999Harry Potter7.00SoftbackC 0497389956Bart’s World10.00HardbackC 0575049802Witches Abroad6.00SoftbackA 0509856745Guards 7.00SoftbackA 0497563856The Bone Collector14.00HardbackA Group By Cat

22 Aggregate (Group) Functions Book Produce the SQL statement to display the maximum book price for each category. Hardback Softback Type 14.00The Bone Collector0497563856 10.00Bart’s World0497389956 7.00Guards 0509856745 6.00Witches Abroad0575049802 7.00Harry Potter0747550999 PriceBookNameISBN A A A C C Cat

23 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;

24 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 Softback Type 14.00The Bone Collector0497563856 10.00Bart’s World0497389956 7.00Guards 0509856745 6.00Witches Abroad0575049802 7.00Harry Potter0747550999 PriceBookNameISBN TypeCatMaximum HardbackA14.00 HardbackC12.00 SoftbackA7.00 SoftbackC7.00 Result CHardback12.00The Amber Spyglass0729863856 CSoftback 4.00Art Attack0997563856

25 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 Softback Type 14.00The Bone Collector0497563856 10.00Bart’s World0497389956 7.00Guards 0509856745 6.00Witches Abroad0575049802 7.00Harry Potter0747550999 PriceBookNameISBN TypeCatMaximum HardbackA14.00 HardbackC12.00 SoftbackA7.00 SoftbackC7.00 Result CHardback12.00The Amber Spyglass0729863856 CSoftback 4.00Art Attack0997563856

26 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 Softback Type 14.00The Bone Collector0497563856 10.00Bart’s World0497389956 7.00Guards 0509856745 6.00Witches Abroad0575049802 7.00Harry Potter0747550999 PriceBookNameISBN 12.00CHardback Softback Type A C Cat 14.00 7.00 Maximum Result CHardback12.00The Amber Spyglass0729863856 CSoftback 4.00Art Attack0997563856 SoftbackA7.00

27 Aggregate (Group) Functions Using the following table, produce the SQL statement that would output the minimum, maximum and average monthly salary of all job types. Employee EmpNoNameJobMonthlySalary E001FredSalesperson1,500.00 E002BamBamAnalyst2,300.00 E003BarneyProgrammer1,700.00 E004WilmaManager2,000.00 E006PebblesProgrammer1,200.00 E007BettySalesperson1,400.00 E009DinoManager2,500.00

28 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.

29 Aggregate (Group) Functions EnrolmentNoNameCourseCode S01HomerC001 S02MargeC002 S03LisaC006 S04BartC003 S05MaggieC002 S06MoC001 S07ApuC003 S08SkinnerC001 S09MillhouseC004 S10LennieC002 SELECT CourseCode, COUNT (EnrolmentNo) AS ‘NoOfStudents’ FROM Student GROUP BY CourseCode; Result CourseCodeNoOfStudents C0013 C0023 C0061 C0032 C0041 Student

30 Aggregate (Group) Functions EnrolmentNoNameCourseCode S01HomerC001 S02MargeC002 S03LisaC006 S04BartC003 S05MaggieC002 S06MoC001 S07ApuC003 S08SkinnerC001 S09MillhouseC004 S10LennieC002 SELECT CourseCode, COUNT (EnrolmentNo) AS ‘NoOfStudents’ FROM Student GROUP BY CourseCode HAVING COUNT (EnrolmentNo) > 1; Result CourseCodeNoOfStudents C0013 C0023 C0032 Student Returning courses with more than 1 student on them:

31 Aggregate (Group) Functions Using the following table, produce the SQL statement that would output those departments that have only 1 person employed in that department Employee EmpNoNameDeptNoJobMonthlySalary E001FredD1Salesperson1,500.00 E002BamBamD1Analyst2,300.00 E003BarneyD3Programmer1,700.00 E004WilmaD2Manager2,000.00 E006PebblesD3Programmer1,200.00 E007BettyD2Salesperson1,400.00 E009DinoD4Manager2,500.00

32 Aggregate (Group) Functions Using 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 EmpNoNameDeptNoJobMonthlySalary E001FredD1Salesperson1,500.00 E002BamBamD1Analyst2,300.00 E003BarneyD3Programmer1,700.00 E004WilmaD2Manager2,000.00 E006PebblesD3Programmer1,200.00 E007BettyD2Salesperson1,400.00 E009DinoD4Manager2,500.00

33 In Conclusion  We have covered:  Aggregate Functions (Group)  ‘Filtering’ Groups


Download ppt "Information Resource Engineering SQL4. Recap - Ordering Output  Usually, the order of rows returned in a query result is undefined.  The ORDER BY clause."

Similar presentations


Ads by Google