0% found this document useful (0 votes)
29 views8 pages

Data Base Lab 6

data base lab 6

Uploaded by

Jaweriya Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Data Base Lab 6

data base lab 6

Uploaded by

Jaweriya Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB SESSION 6

DATABASE SYSTEMS

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY


JINNAH UNIVERSITY FOR WOMEN
5-C NAZIMABAD, KARACHI 74600
LAB SESSION 6: AGGREGATE FUNCTIONS

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.

EXAMPLES OF AGGREGATE FUNCTIONS


This example calculates the average advance and the sum of year-to-date sales for all business
books. Each of these aggregate functions produces a single summary value for all of the
retrieved rows.

USE pubs
SELECT AVG(advance), SUM(ytd_sales) FROM titles
WHERE type='business'

This statement returns the average price of distinct business books.


USE pubs
SELECT AVG(DISTINCT price) FROM titles WHERE type = 'business'
Without DISTINCT, the AVG function finds the average price of all business titles in the
titles table.
USE pubs
SELECT AVG(price) 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

This example finds the total number of books and titles.


USE PUBS
SELECT COUNT(*) FROM TITLES

The example shows that COUNT(*) can be combined with other aggregate functions in
the select list.

SELECT COUNT(*), AVG(PRICE) FROM TITLES


WHERE ADVANCE > $1000
USE PUBS
Using SUM and AVG functions with a GROUP BY clause

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.

When using GROUP BY clause consider the following guidelines:

· SQL Server produces a column of values for each defined group.


· It returns only single rows for each group that you specify: it does not return
detail information.
· All columns in the Group By clause must be included in the select list.
· Do not use GROUP BY on columns that contain multiple null values because null
values are processed as a group.

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 HAVING clause only with the GROUP BY clause.


· Multiple conditions should be combined with logical operators.
· You can reference any of the column that appear in the select list.

USE pubs
SELECT type, AVG(advance), SUM(ytd_sales) FROM titles
GROUP BY type
Having AVG(advance) > 1000
Exercise

Use Northwind database for following queries.

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.

3. Write a query to count all the Employees.


4. Write a query to count the orders that have been placed by individual
customers.
[Hint: Use Group By clause]

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.

Use Pubs database for the following queries.


6. Display the average price for all the books of business type. (Use Titles table)

7. Count all the publishers who live in USA. (Use publishers table) [use group by
and having clause]

You might also like