Aggregate Functions in DBM
Aggregate Functions in DBM
Aggregate Functions in DBM
Product1 Company1 2 10 20
PRODUCT COMPANY QTY RATE COST
Product2 Company2 3 25 75
Product3 Company1 2 30 60
Product4 Company3 5 10 50
Product5 Company2 2 20 40
Product6 Company1 3 25 75
Product8 Company1 3 10 30
Product9 Company2 2 25 50
COUNT()
This COUNT() function is used to count the number of rows in a table or a
result set. It can also be used with a specific column to count the number of
non-null values in that column.
COUNT(*) OR COUNT(COLUMN_NAME)
SQL Query:
SELECT COUNT(*)
FROM PREP_TABLE;
Output:
10
SQL Query:
SELECT COUNT(*)
FROM PREP_TABLE;
WHERE RATE>=20;
Output:
SQL Query:
FROM PREP_TABLE;
Output:
SQL Query:
FROM PREP_TABLE
GROUP BY COMPANY;
Output:
Company1 5
Company2 3
Company3 2
SQL Query:
FROM PREP_TABLE
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output:
Company1 5
Company2 3
SUM()
The SUM() function in DBMS accepts a column name as an input and returns
the total of all non-NULL values in that column. It only works on numeric fields
(i.e the columns contain only numeric values). If this function is applied to
columns that include both non-numeric (like, strings) and numeric values, it
only considers the numeric values. If there are no numeric values, the method
returns 0.
SUM(COLUMN_NAME)
SQL Query:
SELECT SUM(COST)
FROM PREP_TABLE;
Output:
670
SQL Query:
SELECT SUM(COST)
FROM PREP_TABLE
WHERE QTY>3;
Output:
320
SQL Query:
SELECT SUM(COST)
FROM PREP_TABLE
WHERE QTY>3
GROUP BY COMPANY;
Output:
Company1 150
Company2 170
SQL Query:
FROM PREP_TABLE
GROUP BY COMPANY
HAVING SUM(COST)>=170;
Output:
Company1 335
Company3 170
AVG()
The AVG() aggregate function in DBMS takes the column name as an input
and returns the average of all non-NULL values in that column. It only works
on numeric fields (i.e the columns contain only numeric values).
AVG(COLUMN_NAME)
SELECT AVG(COST)
FROM PREP_TABLE;
Output:
67.00
MAX()
The MAX() function accepts the column name as a parameter and returns the
maximum value in the column. When no row is specified, MAX() function
returns NULL.
MAX(COLUMN_NAME)
SQL Query:
SELECT MAX(RATE)
FROM PREP_TABLE;
Output:
30
MIN()
The MIN() function accepts the column name as a parameter and returns the
minimum value in the column. When no row is specified, MIN() Function
returns NULL as result.
MIN(COLUMN_NAME)
Example of MIN() Function
SQL Query:
SELECT MIN(RATE)
FROM PREP_TABLE;
Output:
10
Conclusion
In this article, we learned about the Aggregate Functions in DBMS. The
Aggregate Functions in DBMS help us in dealing with large datasets. We have
discussed different types of Aggregate Functions in DBMS which include,
COUNT(), SUM(), AVG(), MAX(), and MIN(). Whether you are a data analyst,
a database administrator, or a developer, understanding how to use
aggregate functions in DBMS is essential for working with large datasets and
making informed decisions based on data analysis.
Ques 3. What is the difference between SUM() and AVG() aggregate functions in
DBMS?
Ans. SUM() function calculates the total sum of a column whereas the AVG()
function calculates the average of a column.
Ques 4. What is the difference between the MAX() and MIN() aggregate
functions in DBMS?
Ans. MAX() function returns the maximum value in a column whereas MIN()
function returns the minimum value in a column.
Ques 5. Can we use aggregate functions in DBMS with the DISTINCT keyword?
Ans. Yes, aggregate functions can be used with the DISTINCT keyword to
perform calculations on unique values of a column.
Aggregate functions are a vital component of database management systems. They allow us to
perform calculations on large data sets quickly and efficiently. For example, these functions
generate statistical reports, perform financial analysis, and manage inventory levels.
In addition, we can better understand the data we are working with by using aggregate functions.
For example, we can easily calculate the average price of all products in our inventory or find the
total sales for a particular time. Without aggregate functions, we would need to manually sort
through each data point, which would be time-consuming and error-prone.
Overall, aggregate functions are essential for anyone working with large amounts of data and
seeking to gain valuable insights from it.
COUNT() Function
Syntax:
COUNT(*)
or
We will use the ‘products’ table from the sample database for our demonstration.
The following SQL statement fetches the number of products in the table.
The below-given command will display those product ids where the unit price is greater than 4.
This will display the following result.
Let's look at how we can use GROUP BY and HAVING functions with the COUNT function.