EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Aggregate Function
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL?Table?Size
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

MySQL Aggregate Function

By Priya PedamkarPriya Pedamkar

MySQL Aggregate Function

Introduction to MySQL Aggregate Function

Mostly in data query language, we use these aggregated functions with SELECT statements. An aggregate function performs a calculation on multiple values and returns a single value like the sum of all values, maximum and minimum among certain groups of values.

Example: If we have to calculate the total sales of a product in a month, then we have to use the ‘SUM’ function for adding up all the sales values. In the same manner, as using ‘MAX’ and ‘MIN’ functions, we can get the highest and lowest sale of that particular month. Aggregate functions ignore NULL values except ‘COUNT’ function. Count function returns the total number of observations. HAVING clause is used along with GROUP BY, for filtering query using aggregate values.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Top 11 Aggregate Functions

Here are some MySQL aggregate functions which are explained below:

  1. AVG() Function
  2. COUNT() Function
  3. Sum() Function
  4. Max() Function
  5. MIN() Function
  6. DISTINCT() Function
  7. GROUP_CONCAT() Function
  8. VAR() Function
  9. STDEV() Function
  10. BIT_AND() Function
  11. BIT_OR() Function
Section Name Marks
Sec-A Stewart 90
Sec-B Vince 86
Sec-C John 94
Sec-A Michelle 78
Sec-C Robin 60
Sec-A Sara 86
Sec-B Peter 92
Sec-C Ian 89
Sec-A David 76

1. AVG() Function

This is an average function. It function calculates the average value for a set of values. It ignores null values in calculation.

For getting average marks of all the students.

Query:

SELECT AVG(marks) AS avg_marks FROM student;

Output:

MySQL Aggregate Function -2

If we want the average mark of students for each section, then we can use AVG() with GROUP BY function.

Query:

SELECT section, AVG(marks) AS avg_marks FROM student GROUP BY section;

Output:

MySQL Aggregate Function -3

2. COUNT() Function

The COUNT() function returns the value of a total number of observations/total number of values in a set of values.

If we perform this function in the above set of example for getting the number of students,

Query:

SELECT COUNT(name) AS total_students FROM student;

Output:

count

For getting the student count in each section,

Query:

SELECT section, COUNT(name) AS total_students FROM student GROUP BY section;

Output:

student count

3. Sum() Function

The SUM() function returns the sum of all values in a set. To get the sum of marks of all the students,

Query:

SELECT SUM(marks) AS total_marks FROM student;

Output:

sum function

Sum of marks of all students section-wise,

Query:

SELECT section, SUM(marks) AS total_marks FROM student GROUP BY section;

Output:

sum student

4. Max() Function

The max() function returns the maximum value in a set of values. To find the highest scorer in the exam from student database, below query can give us the desired output:

Query:

SELECT name, MAX(mark) AS highest_mark FROM student;

Output:

Max() function

In the same process, we can find out the maximum mark secured from each section.

Query:

SELECT section, name, MAX(mark) AS highest_mark FROM student GROUP BY section;

5. MIN() Function

MIN() function returns the lowest value from the set of values. This doesn’t consider the null values. Lowest scorer among the students,

Query:

SELECT name, MIN(mark) AS lowest_mark FROM student;

Output:
MIN() function

This also can be used with GROUP BY function.

6. DISTINCT() Function

This function mostly we use with COUNT function to get the number of unique values in the set of values. We can also simply use this DISTINCT function to get the unique values.

Query:

SELECT DISTINCT (section) FROM student;

Output:

MySQL Aggregate Function -4

Query for using DISTINCT with COUNT() function.

Query:

SELECT COUNT(DISTINCT(section)) FROM student;

Output:

MySQL Aggregate Function -5

7. GROUP_CONCAT() Function

This is used to concat all the string values of one attribute, merge into one index.

Query:

SELECT GROUP_CONCAT(name SEPARATOR ‘;’) FROM student;

Output:

MySQL Aggregate Function -6

Like this GROUP_CONCAT() we also use another function CONCAT(), which merges 2 sets of string values into a new column.

Example:

MySQL Aggregate Function -7

If in this name database we use the function CONCAT(),

Query:

SELECT first_name, last_name, CONCAT(first_name,’ ‘,last_name) as full_name FROM name;

Output:

MySQL Aggregate Function -8

8. VAR() Function

This variance function returns the population standard variance of the specified column.

Variance is a measurement of the spread between the numbers in a dataset. It is calculated by how far each number is from the mean and therefore from each number of the set.

Variance:

Variance

Query:

SELECT VAR(mark) AS variance FROM student;

Output:

Variance output

9. STDEV() Function

Standard deviation is the measure of the amount of variation or dispersion of a set of values. This express how much the member of a group differ from the mean value of the group. This is calculated by taking the square root of the variance.

Variance square root

This function returns the population standard deviation of the specified column.

Query:

SELECT STDEV(mark) AS std_deviation FROM student;

Output:

standard dev

10. BIT_AND() Function

This function returns the bit-wise AND of specified rows. This returns the same data type whichever is passed through argument.

If every row has 1 in the bit position, then only it will return 1, otherwise, it returns 0.

bit-wise AND

Query:

SELECT BIT_AND( CAST(row_value VariableBIT) ) FROM student.list('0001,0111,0100,0011');

Output:

bit_and

11. BIT_OR() Function

Returns the bit-wise OR of the specified expression for each group of rows.

Query:

SELECT BIT_OR( CAST(row_value AS VariableBIT) )
FROM student.list('0001,0111,0100,0011');

The result of 0111 is determined as follows:

  • A bitwise OR is performed between row 1 (0001) and row 2 (0111), resulting in 0111.
  • A bitwise OR is performed between the result from the previous comparison (0111) and row 3 (0100), resulting in 0111.
  • A bitwise OR is performed between the result from the previous comparison (0111) and row 4 (0011), resulting in 0111.

Conclusion

In the analysis perspective as well as in the extraction process, those aggregated functions are very important. Specifically, when we give a condition in a query or while using windows function, aggregated functions play major roles. Some functions like LIMIT, RANK, etc. and GROUP BY clause always comes with aggregated functions.

Recommended Articles

This is a guide to MySQL Aggregate Function. Here we discuss the top 11 different types of MySQL aggregate function and their implementation. You may also look at the following articles to learn more –

  1. MySQL vs SQLite
  2. Insert Command in MySQL
  3. SELECT in MySQL
  4. MySQL BETWEEN
Popular Course in this category
SQL Training Program (10 Courses, 8+ Projects)
  10 Online Courses |  8 Hands-on Projects |  80+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (17 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more