MYSQL Tutorial Part 3
AGGREGATE FUNCTIONS
Aggregate functions are used to collect the values from multiple rows as input and perform some calculations. It returns a single value.
TYPES OF AGGREGATE FUNCTIONS:
- count
- sum
- avg
- max
- min
COUNT():
- The COUNT() function counts the number of rows that matches a specified criterion.
- It returns the total number of records.
- It works for both numeric and non-numeric data types.
Syntax
SELECT COUNT(column_name) FROM table_name WHERE condition;
Example
SELECT COUNT(department) FROM Student WHERE department = 'CSE';
SUM():
- The SUM() function returns the sum of all the values in the selected column.
- It works for numeric data types only.
Syntax
SELECT SUM(column_name) FROM table_name WHERE condition;
Example
SELECT SUM(fees) FROM Student WHERE department = 'CSE';
AVG():
- The AVG() function returns the average value of a numeric column.
- It returns the average of non-null values of the column.
Syntax
SELECT AVG(column_name) FROM table_name WHERE condition;
Example
SELECT AVG(fees) FROM Student WHERE department = 'CSE';
MIN() and MAX():
- The MIN() function returns the smallest value of the selected column.
- The MAX() function returns the largest value of the selected column.
MIN() Syntax
SELECT MIN(column_name) FROM table_name WHERE condition;
MAX() Syntax
SELECT MAX(column_name) FROM table_name WHERE condition;
Example
SELECT MIN(fees) AS MinFees, MAX(fees) AS MaxFees FROM Student;
Comments
Post a Comment