0% found this document useful (0 votes)
18 views

Aggregate Functions: CIS 331: Introduction To Database Systems

The document discusses aggregate functions such as COUNT, SUM, MIN, MAX, and AVG that can be used with SQL queries. It provides examples of using these functions to count the number of students, find the minimum and maximum grades for a class, and calculate a student's average grade (GPA). The document also covers using GROUP BY to group records that have something in common, like students in the same class, and HAVING to filter groups based on aggregate expressions, like only students with a GPA over 3.5.

Uploaded by

Nelu Badalan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Aggregate Functions: CIS 331: Introduction To Database Systems

The document discusses aggregate functions such as COUNT, SUM, MIN, MAX, and AVG that can be used with SQL queries. It provides examples of using these functions to count the number of students, find the minimum and maximum grades for a class, and calculate a student's average grade (GPA). The document also covers using GROUP BY to group records that have something in common, like students in the same class, and HAVING to filter groups based on aggregate expressions, like only students with a GPA over 3.5.

Uploaded by

Nelu Badalan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Aggregate functions

CIS 331: Introduction to Database Systems

Topics:
ORDER BY, ASC, DESC Aggregate functions COUNT SUM MIN MAX AVG GROUP BY and HAVING
Vladimir Vacic, Temple University
2

Order By, Asc


ORDER BY sorts results of a query:
SELECT first_name, middle_name, last_name FROM professors ORDER BY last_name ASC ;

Ascending (ASC) order is the default, so you do not have to specify it explicitly:
SELECT first_name, middle_name, last_name FROM professors ORDER BY last_name ;

Vladimir Vacic, Temple University

Order By, Desc


You can also sort the output in descending order (DESC): SELECT first_name, middle_name, last_name FROM professors ORDER BY first_name DESC ;

Vladimir Vacic, Temple University

Aggregate functions: Count


Suppose we would just like to know the number of students, without necessarily seeing their names, etc: SELECT COUNT(*) FROM students ; Or how many students attended a particular class: SELECT class, COUNT(*) FROM students_classes GROUP BY class ; GROUP BY groups together records which have something in common: in this case all students who attended the same class.
5

Vladimir Vacic, Temple University

Aggregate functions: Min, Max


We can also calculate the range of grades per class:
SELECT class, MIN(grade), MAX(grade) FROM students_classes GROUP BY class ;

Or a students GPA:
SELECT student, AVG(grade) FROM students_classes GROUP BY student ORDER BY AVG(grade) ;

Vladimir Vacic, Temple University

Having
Get a list of students who have a GPA over 3.5: SELECT student, AVG(grade) FROM students_classes GROUP BY student HAVING AVG(grade) > 3.5 ; Observe the HAVING clause: it is similar to WHERE, except that you can use aggregate expressions as conditions.

Vladimir Vacic, Temple University

Aggregate functions: Avg


GPA of students who took more than 3 classes: SELECT student, AVG(grade) FROM students_classes GROUP BY student HAVING COUNT(*) > 3 ORDER BY student DESC ;

Vladimir Vacic, Temple University

You might also like