Data Base Lab 6
Data Base Lab 6
DATABASE SYSTEMS
INTRODUCTION
Aggregate functions perform a calculation on a set of values and return a single value. With
the exception of COUNT, aggregate functions ignore null values. Aggregate functions are often
used with the GROUP BY clause of the SELECT statement.
Function Description
Returns the average of the values in a group. Null values are ignored.
ALL Applies the aggregate function to all values. ALL is the default.
DISTINCT Specifies that AVG be performed only on each unique
AVG ( [ ALL | DISTINCT] instance of a value, regardless of how many times the value occurs.
expression )
SUM ( [ ALL | DISTINCT ] Returns the sum of all the values, or only the DISTINCT values, in the
expression) expression . SUM can be used with numeric columns only. Null
ignored.
MAX ( [ ALL | DISTINCT ] Returns the maximum value in the expression.
expression)
MIN ( [ ALL | Returns the minimum value in the expression.
DISTINCT]expression)
Returns the number of items in a group. * Specifies that all rows
COUNT ( [ ALL | should be counted to return the total number of rows in a table.
DISTINCT] expression) COUNT(*) takes no parameters and cannot be used with DISTINCT.
USE pubs
SELECT AVG(advance), SUM(ytd_sales) FROM titles
WHERE type='business'
The example below shows SUM function giving summary data only.
USE PUBS
SELECT TYPE, SUM(PRICE), SUM(ADVANCE) FROM TITLES
WHERE TYPE LIKE '%COOK'
GROUP BY TYPE
This example returns the book with the highest (maximum) year-to-
date sales.
USE pubs
SELECT MAX(ytd_sales) FROM titles
This example returns the book with the lowest (minimum) year-to-date sales.
USE PUBS
SELECT MIN(YTD_SALES)
FROM TITLES
This example finds the number of different cities in which authors live.
USE PUBS
SELECT COUNT(DISTINCT CITY) FROM AUTHORS
The example shows that COUNT(*) can be combined with other aggregate functions in
the select list.
By itself, an aggregate function produces a single summary value for all rows in a
column. If you want to generate summary values for a column, use aggregate functions
with the GROUP BY clause. Use the HAVING clause with the GROUP BY clause to
restrict the groups of rows that are returned.
Use the GROUP BY clause on columns or expressions to organize rows into groups and
to summarize those groups. For example, use the GROUP BY clause to determine the
quantity of each product that was ordered for all orders.
This example produces summary values for each type of book that include the average
advance for each type of book and the sum of year-to-date sales for each type of book.
USE pubs
SELECT type, AVG(advance), SUM(ytd_sales) FROM titles
GROUP BY type
This example calculates the sum of the prices and advances for each type of book.
USE pubs
SELECT type, SUM(price), SUM(advance) FROM titles
GROUP BY type
Having Clause
Use HAVING clause on columns or expressions to set conditions on the group included
in a result set. The HAVING clause sets condition on the GROUP BY clause in much the
same way that the WHERE clause interacts with the SELECT clause. Consider following
guidelines when using HAVING clause:
USE pubs
SELECT type, AVG(advance), SUM(ytd_sales) FROM titles
GROUP BY type
Having AVG(advance) > 1000
Exercise
1. Display Maximum and Minimum hire date of all the employees. Rename
columns to Max and Min.
2. Display Average, Max, Min and Sum of Unit Price for all the Products.
5. Each Employee reports to some person. Write a query to display the Reportsto
field along with the number of employees who report to this employee.
7. Count all the publishers who live in USA. (Use publishers table) [use group by
and having clause]