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

SQL Aggregate Functions

Uploaded by

Dr. SATHIYA M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

SQL Aggregate Functions

Uploaded by

Dr. SATHIYA M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL Aggregate Functions

An aggregate function is a function that performs a calculation on a set of


values, and returns a single value.

Aggregate functions are often used with the GROUP BY clause of


the SELECT statement. The GROUP BY clause splits the result-set into groups of
values and the aggregate function can be used to return a single value for each
group.

The most commonly used SQL aggregate functions are:

 MIN() - returns the smallest value within the selected column


 MAX() - returns the largest value within the selected column
 COUNT() - returns the number of rows in a set
 SUM() - returns the total sum of a numerical column
 AVG() - returns the average value of a numerical column

Aggregate functions ignore null values (except for COUNT()).

1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
Syntax

1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )
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 Cpm1 3 25 75

Item7 Com1 5 30 150

Item8 Com1 3 10 30

Item9 Com2 2 25 50

Item10 Com3 4 30 120

Example: COUNT()

1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:

10

Example: COUNT with WHERE

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

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

1. SELECT COMPANY, COUNT(*)


2. FROM PRODUCT_MAST
3. GROUP BY COMPANY
4. HAVING COUNT(*)>2;
Output:

Com1 5
Com2 3

2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.

Syntax

1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )
Example: SUM()

1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Output:

670

Example: SUM() with WHERE

1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. 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

Anomalies means problems or inconsistency which happened during the


operations performed on the table. There can be many reasons that anomaly
occur for example,It occurs when data is stored multiple times unnecessarily in
the database i.e. redundant data is present or it occur when all the data is
stored in a single table. normalization is used to overcome the anomalies. the
different type of anomalies are insertion,deletion and updation anomaly.

Input

The same input is used for all three anomalies.

Student

ID Name Age Branch Branch_Code Hod_name

1 A 17 Civil 101 Aman

2 B 18 Civil 101 Aman

3 C 19 Civil 101 Aman

4 D 20 CS 102 Monu

5 E 21 CS 102 Monu

6 F 22 Electrical 103 Rakesh


With the help of this table, we are going to show the working of different
anomalies.

Insertion Anomaly
When certain data or attributes cannot be inserted into the database without
the presence of other data, it's called insertion anomaly.

For example, let's take a branch name petroleum, now the data regarding
petroleum cannot be stored in the table unless we insert a student which is in
petroleum. Practically, branch existence is not dependent on student existence
i.e. we must have the capability that we can store the data of branch whether
there are any student of that branch or not, but this can't be done because of
insertion anomaly.

Code
Insert into student values(7, ‘G’,16, ‘PETROLEUM’,104, ‘NAMAN’)#Values get
inserted
Select * from Student;#Data selected
Output
ID Name Age Branch Branch_Code Hod_name

1 A 17 Civil 101 Aman

2 B 18 Civil 101 Aman

3 C 19 Civil 101 Aman

4 D 20 CS 102 Monu

5 E 21 CS 102 Monu

6 F 22 Electrical 103 Rakesh

7 G 16 Petroleum 104 Naman


Deletion anomaly
If we delete any data from the database and any other information which is
required also gets deleted with that deletion, then it is called deletion anomaly.

For example, suppose a student of the electrical branch is leaving so now we


have to delete the data of that student, but the problem is if we delete the
student data, then branch data will also get deleted along with that as there is
only one student present through which branch data is present.

Code
Delete from STUDENT WHERE BRANCH= ‘ELECTRICAL’;#data get deleted
Select * from STUDENT;#data selected
Output
ID Name Age Branch Branch_Code Hod_name

1 A 17 Civil 101 Aman

2 B 18 Civil 101 Aman

3 C 19 Civil 101 Aman

4 D 20 CS 102 Monu

5 E 21 CS 102 Monu

Explore our latest online courses and learn new skills at your own pace. Enroll
and become a certified expert to boost your career.

Updation/modification anomaly
If we want to update any single piece of data then we have to update all other
copies, it comes under insertion anomaly.
For example, suppose we need to change the hod name for civil branch, now as
per requirement, only single data is to be changed, but we have to change the
data at every other part so as to not make an inconsistent table

Algorithm
 Step 1 − Use update to make changes in the table
 Step 2 − Provide changes that are to be made
 Step 3 − Provide condition to where the task get performed
 Step 4 − Use select to check the output
Code
Update STUDENT #Table selected to preform task
Set HOD_NAME= ‘RAHUL’#changes to be made
WHERE BRANCH= ‘CIVIL’;#condition given
Select * from STUDENT;#Data selected
Output
ID Name Age Branch Branch_Code Hod_name

1 A 17 Civil 101 Aman

2 B 18 Civil 101 Aman

3 C 19 Civil 101 Aman

4 D 20 CS 102 Monu

5 E 21 CS 102 Monu

6 F 22 Electrical 103 Rakesh

You might also like