IM 101 - Fundamentals of Database Systems - Unit 14
IM 101 - Fundamentals of Database Systems - Unit 14
All rights reserved. No part of this module may be reproduced, repurposed, distributed, or transmitted in
any form or by any means including photocopying, reprinting, or other electronic or mechanical
methods without the prior written permission of PLV and the individual developers of instructional
materials (IMs) except in the case of brief quotations embodied in critical and creative reviews and
certain other noncommercial uses permitted by the Copyright Law. For permission request, address your
written correspondence whether printed or electronic to the Chair of the University Committee on
Instructional Materials Development and Evaluation at the address below:
Essential Question
How do we order results in a query ?
What are aggregate functions ?
How are aggregate functions used ?
Lessons Input
Ordering a List
ORDER BY clause is especially useful when the listing order is important to you. Sample syntax is :
SELECT columnlist
FROM tablename
[WHERE condition]
ORDER BY columnname ASC/DESC
ORDER BY FirstName
2
We can also order using multiple column name with the column name being followed from left to right
SELECT * FROM Person
ORDER BY LastName, FirstName, MiddleName
--orders the data by last name, first name and middle name
DISTINCT clause produces a list of only those values that are different from one another. For example,
we want to get the list of all the unique Last name from the Person we can do :
SELECT DISTINCT LastName FROM Person
Aggregate Functions
COUNT function is used to tally the number of non-null values of an attribute. COUNT can be used in
conjunction with the DISTINCT clause. Example:
SELECT COUNT(DISTINCT LastName) FROM Person
--counts all distinct Last Name from Person
SUM example :
SELECT SUM(SALES) FROM tbl_Product
--add up all the values from SALES column
AVG example :
SELECT AVG(SALES) FROM tbl_Product
--gets the average of all the values from SALES column
Take note that SUM and AVG must be used for numeric values only.
References
Garcia-Molina, H.,Ullman, J.,Widom, J. (2008). Database Systems: The Complete Book (2nd ed., pp 189-
501). Pearson