Beruflich Dokumente
Kultur Dokumente
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
AVG ( [ ALL | DISTINCT] default.
expression ) DISTINCT Specifies that AVG be performed only on each unique
instance of a value, regardless of how many times the value
occurs.
SUM ( [ ALL | DISTINCT ] Returns the sum of all the values, or only the DISTINCT values, in
expression) the 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'
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.
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 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.
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.
5. Display the average price for all the books of business type. (Use Titles table)
6. Count all the publishers who live in USA. (Use publishers table) [use group by and having
clause]
1.What is the difference among char, varchar, nchar and nvarchar?
Char
Varchar
If you are sure about the fixed length of the data that would be captured for any specific
column then go for CHAR data type and if the data may vary then go for VARCHAR.
nChar
nvarChar