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

DBMS SQL Aggregate Function - Javatpoint

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
32 views8 pages

DBMS SQL Aggregate Function - Javatpoint

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Home DBMS SQL PL/SQL SQLite MongoDB Cassandra MySQL Oracle CouchDB Neo4j DB2 C Java Projects Interview Q

Ad

AACSB-accredited MBA

University of Adelaide Apply Now


SQL Aggregate Functions
SQL aggregation function is used to perform the calculations on multiple rows of a
single column of a table. It returns a single value.

It is also used to summarize the data.

Types of SQL Aggregation Function

1. COUNT FUNCTION

ADVERTISEMENT

ADVERTISEMENT

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.

COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.

Syntax

Leaders in Data Visualization

Araya Solutions - Data Open

COUNT(*)
or
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()

SELECT COUNT(*)
FROM PRODUCT_MAST;

Output:

10

Example: COUNT with WHERE

SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;

Output:

Example: COUNT() with DISTINCT

SELECT COUNT(DISTINCT COMPANY)


FROM PRODUCT_MAST;

Output:

Example: COUNT() with GROUP BY

SELECT COMPANY, COUNT(*)


FROM PRODUCT_MAST
GROUP BY COMPANY;

Output:

Com1 5
Com2 3
Com3 2

Example: COUNT() with HAVING


SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
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

SUM()
or
SUM( [ALL|DISTINCT] expression )

Example: SUM()

SELECT SUM(COST)
FROM PRODUCT_MAST;

Output:

670

Example: SUM() with WHERE

SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;

Output:
320

Example: SUM() with GROUP BY

SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;

Output:

Com1 150
Com2 170

Example: SUM() with HAVING

SELECT COMPANY, SUM(COST)


FROM PRODUCT_MAST
GROUP BY COMPANY
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

AVG()
or
AVG( [ALL|DISTINCT] expression )

Example:

SELECT AVG(COST)
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

MAX()
or
MAX( [ALL|DISTINCT] expression )

Example:

SELECT MAX(RATE)
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

MIN()
or
MIN( [ALL|DISTINCT] expression )

Example:

SELECT MIN(RATE)
FROM PRODUCT_MAST;

Output:

10

← Prev Next →

Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to feedback@javatpoint.com

Help Others, Please Share


Learn Latest Tutorials

Splunk SPSS tutorial Swagger T-SQL Tumblr


tutorial SPSS
tutorial tutorial tutorial
Splunk Swagger Transact-SQL Tumblr

React tutorial Regex R RxJS tutorial


tutorial Reinforcement Programming
ReactJS RxJS
learning tutorial
Regex
tutorial
R Programming
Reinforcement
Learning

React Native Python Python Python Keras


tutorial Design Patterns Pillow tutorial Turtle tutorial tutorial
React Native Python Design Python Pillow Python Turtle Keras
Patterns

Preparation

Aptitude Logical Verbal Interview Company


Aptitude
Reasoning Ability Questions Interview
Questions
Reasoning Verbal Ability Interview
Questions Company
Questions

Trending Technologies

Artificial AWS Tutorial Selenium Cloud Hadoop


Intelligence AWS
tutorial Computing tutorial
Artificial Selenium Cloud Computing Hadoop
Intelligence

ReactJS Data Science Angular 7 Blockchain Git Tutorial


Tutorial Tutorial Tutorial Tutorial Git
ReactJS Data Science Angular 7 Blockchain

Machine DevOps
Learning Tutorial
Tutorial DevOps
Machine
Learning

B.Tech / MCA

DBMS Data DAA tutorial Operating Computer


tutorial Structures DAA
System Network
DBMS
tutorial Operating
tutorial
Data Structures System Computer
Network

Compiler Computer Discrete Ethical Computer


Design tutorial Organization Mathematics Hacking Graphics
Compiler Design
and Tutorial Ethical Hacking
Tutorial
Architecture Discrete Computer
Computer Mathematics Graphics
Organization
Software html tutorial Cyber Automata C Language
Engineering Web Technology
Security Tutorial tutorial
Software
tutorial Automata C Programming
Engineering Cyber Security

C++ tutorial Java tutorial .Net Python List of


Framework tutorial Programs
C++ Java
tutorial Python Programs
.Net

Control Data Mining Data


Systems Tutorial Warehouse
tutorial Data Mining
Tutorial
Control System Data Warehouse

You might also like