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

Aggregate functions

The document explains SQL aggregate functions, which perform calculations on multiple values and return a single value, while ignoring NULLs except for the COUNT function. It details various aggregate functions such as COUNT, SUM, AVG, MIN, and MAX, along with their syntax and examples using a sample table called PRODUCT_MAST. The document also illustrates how to use these functions with clauses like WHERE, GROUP BY, and HAVING.

Uploaded by

tvganesann
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)
2 views

Aggregate functions

The document explains SQL aggregate functions, which perform calculations on multiple values and return a single value, while ignoring NULLs except for the COUNT function. It details various aggregate functions such as COUNT, SUM, AVG, MIN, and MAX, along with their syntax and examples using a sample table called PRODUCT_MAST. The document also illustrates how to use these functions with clauses like WHERE, GROUP BY, and HAVING.

Uploaded by

tvganesann
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/ 5

Aggregate functions:

 An aggregate function in SQL performs a calculation on multiple values and returns a


single value
 An aggregate function ignores NULL values when it performs the calculation, except
for the count function.
 We often use aggregate functions with the GROUP BY and HAVING clauses of the
SELECT statement.
Various types of SQL aggregate functions are:

1. Count()

2. Sum()

3. Avg()

4. Min()

5. Max()
COUNT() Function: The COUNT() function returns the number of rows in a database
table. COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.

Syntax:COUNT(*)

Sample table:

PRODUCT_MAST

PRODUCT COMPANY QTY RATE COST

Item1 Com1 2 10 20

Item2 Com2 3 25 75

Item3 Com1 2 30 60

Item4 Com3 5 10 50

Item5 Com2 2 20 40

Item6 Com1 3 25 75

Item7 Com1 5 30 150

Item8 Com1 3 10 30
Item9 Com2 2 25 50

Item10 Com3 4 30 120

SELECT COUNT(*)
FROM PRODUCT_MAST;

Output:

10

Example: COUNT with WHERE

1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;

Output:

Example: COUNT() with DISTINCT

1. SELECT COUNT(DISTINCT COMPANY)


2. FROM PRODUCT_MAST;

Output:

Example: COUNT() with GROUP BY

1. SELECT COMPANY, COUNT(*)


2. FROM PRODUCT_MAST
3. GROUP BY COMPANY;

Output:

Com1 5
Com2 3
Com3 2

Example: COUNT() with HAVING

Example: COUNT with WHERE


1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;

Output:

Example: COUNT() with DISTINCT

1. SELECT COUNT(DISTINCT COMPANY)


2. FROM PRODUCT_MAST;

Output:

Example: COUNT() with GROUP BY

1. SELECT COMPANY, COUNT(*)


2. FROM PRODUCT_MAST
3. GROUP BY COMPANY;

Output:

Com1 5
Com2 3
Com3 2

Example: COUNT() with HAVING

670

Example: SUM() with WHERE

SELECT SUM(COST)

FROM PRODUCT_MAST

WHERE QTY>3;

Output:

320

Example: SUM() with GROUP BY


1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3
4. GROUP BY COMPANY;

Output:

Com1 150
Com2 170

Example: SUM() with HAVING

1. SELECT COMPANY, SUM(COST)


2. FROM PRODUCT_MAST
3. GROUP BY COMPANY
4. HAVING SUM(COST)>=170;

Output:

Com1 335
Com3 170

3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.

Syntax

1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )

Example:

1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;

Output:

67.00

4. MAX Function
MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
Syntax

1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )

Example:

1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30

5. MIN Function
MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column.

Syntax

1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )

Example:

1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;

Output:

10

You might also like