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 Average
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 Average

By Roja MetlaRoja Metla

MySQL Average

Introduction to MySQL Average

To find the average of a field in various records, we use MySQL Average. We can find the average of the records of a column by using the “Group by” clause.MySQL AVG() function is an aggregate function that allows you to calculate the average value of column values. To calculate the average of the distinct values from a column we can use the “DISTINCT” operator. AVG function will ignore “NULL” values.

AVG function can be used in the subqueries. It can be used along with the control flow functions as well like IF, IFNULL, NULLIF, and CASE.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax:

SELECT AVG(EXPRESSION) FROM <TABLE_NAME>;

Here above is the syntax of the Average function. The average function returns the data of INT datatype.

How Does MySQL Average Work?

Now let us create a table and perform the average function on the column and retrieve the data.

Query:

create table Freelancer_data
(
Freelancer_id INT,
Freelancer_Name VARCHAR(20),
Type_of_work VARCHAR(30),
No_of_submission INT,
No_of_pages_submitted INT,
EMAIL varchar(30)
);

1. Insert data Into the Table

Query:

insert into freelancer_data values (1278,'Jack','Typist', 2, 300,'[email protected]');
insert into freelancer_data values (2278,'Will','Artical Writer', 3, 450,'[email protected]');
insert into freelancer_data values (3278,'Rose','Artical Writer', 3, 540,'[email protected]');
insert into freelancer_data values (4278,'Ben','Typist', 3, 860,'[email protected]');
insert into freelancer_data values (5278,'Stuart','Typist', 5, 600,'[email protected]');
insert into freelancer_data values (6278,'Rample','Artical Writer', 6, 900,'[email protected]');
insert into freelancer_data values (7278,'Jackern','Typist', 3, 700,'[email protected]');
insert into freelancer_data values (8278,'fred','Artical Writer', 2, 300,'[email protected]');
insert into freelancer_data values (9278,'Gram','Artical Writer', 9, 400,'[email protected]');

2. Select the Data from the Table

Query:

select * from freelancer_data;

Output:

MySQL Average Example 1

3. Now let us find the average Pages Submitted by the Freelancer

Query:

select AVG(No_of_pages_submitted) as "Average papers submitted" from freelancer_data;

Output:

MySQL Average Example 2

Now let us find the average pages submitted by the freelancer based on the Type_of_work using “GROUP BY” clause:

Query:

select AVG(No_of_pages_submitted) as "Average papers submitted", Type_of_work from freelancer_data group by 2;

Output:

MySQL Average Example 3

or

Query:

select AVG(No_of_pages_submitted) as "Average papers submitted", Type_of_work from freelancer_data group by Type_of_work;

Output:

MySQL Average Example 4

4. Using AVG() With “HAVING” Clause

To set conditions to the output of the average values, we use the “HAVING” clause.

Query:

select AVG(No_of_pages_submitted) as "Average papers submitted", Type_of_work
from freelancer_data
group by 2
having AVG(No_of_pages_submitted) > 600;

or

Query:

select AVG(No_of_pages_submitted) as "Average papers submitted", Type_of_work
from freelancer_data
group by Type_of_work
having AVG(No_of_pages_submitted) > 600;

Output:

MySQL Average Example 5

5. Using AVG() with sub-query

Here in the sub-query, we are finding the average based on “type_of_work”. The outer query gets the average for the output of the inner query.

Query:

SELECT
AVG(AVG_PAGES) as "average pages"/* Outer query*/
FROM
(select
AVG(NO_OF_PAGES_SUBMITTED) AS "AVG_PAGES" /* -- inner query --*/
from
freelancer_data
GROUP BY TYPE_OF_WORK) AVG;

Output:

SubQuery Example 6

6. Using AVG() with Control functions

Here let us find the average of the pages submitted if the “no_of_submission” is 3 else consider as “null”. As, AVG ignores the NULL values the below output is average for only the count of submission is =3.

Query:

SELECT
AVG(IF(No_of_submission= 3,
No_of_pages_submitted,
NULL))/No_of_submission 'Avg pages'
FROM
freelancer_data;

Output:

Control Functions Example 6

Example to Implement MySQL Average

Now let us consider other simple examples below:

Query:

create table sample_AVG
(
ID INT,
NAME VARCHAR(30),
DEPT_NO INT,
SALARY FLOAT(10,2)
);

1. Insert data Into the Table

Query:

insert into SAMPLE_AVG values (1278,'Jack', 2, 90000);
insert into SAMPLE_AVG values (2278,'Will', 2, 80000);
insert into SAMPLE_AVG values (3278,'Rose', 3, 78000);
insert into SAMPLE_AVG values (4278,'Ben', 3, 45000);
insert into SAMPLE_AVG values (5278,'Stuart', 3, 67000);
insert into SAMPLE_AVG values (6278,'Rample', 4, 57000);
insert into SAMPLE_AVG values (7278,'Jackern', 4, 47000);
insert into SAMPLE_AVG values (8278,'fred', 4, 68000);
insert into SAMPLE_AVG values (9278,'Gram', 4,86000);

Query:

select * from SAMPLE_AVG;

Output:

MySQL Average Example 8

2. Now let us find the average of the salary from the Table

Query:

select AVG(salary) as "Average salary" from sample_avg;

Output:

MySQL Average Example 9

3. Now let us find the average SALARY based on the DEPT_NO using the “GROUP BY” Clause

Query:

select AVG(salary) as "Average salary", Dept_no from sample_avg group by 2; /* - - Position of the column - -*/

Output:

MySQL Average Example 10

or

Query:

select AVG(salary) as "Average salary", Dept_no from sample_avg group by dept_no;

Output:

MySQL Average Example 11

4. Using AVG() With “HAVING” Clause

To set conditions to the output of the average values, we use the “HAVING” clause.

Query:

select
AVG(salary) as "Average salary",
Dept_no from sample_avg
group by 2
having AVG(salary) > '65000'

or

Output:

AVG() With “HAVING” Clause Example 12

Query:

select
AVG(salary) as "Average salary",
Dept_no from sample_avg
group by dept_no
having AVG(salary) > '65000'

Output:

AVG() With “HAVING” Clause Example 13

5. Using AVG() with sub-query

Here in the sub-query we are finding the average based on “dept_no”. The outer query gets the average for the output of the inner query.

Query:

SELECT
AVG(AVG_SAL) as "average salary"
FROM
(select
AVG(salary) as "Avg_sal",
Dept_no from sample_avg
group by 2
)AVG;

Output:

SubQuery Example 5

6. Using AVG() with Control Functions

Here let us find the average of the SALARY if the “DEPT_NO” is in 3, 4 else consider as “null”. As, AVG ignores the NULL values the below output is average for only the count of submission is 3 and 4.

Query:

SELECT
AVG(IF(DEPT_NO IN (3,4),
SALARY,
NULL))/COUNT(DEPT_NO) 'AVGSALARY'
FROM
SAMPLE_AVG;

Output: 

Control Functions Example 6

Conclusion

  • To find the average of a field in various records, we use MySQL Average. We can find the average of the records of a column by using the “Group by” clause.
  • MySQL AVG() function is an aggregate function that allows you to calculate the average value of column values.
  • To calculate the average of the distinct values from a column we can use the “DISTINCT” operator. AVG function will ignore “NULL” values.
  • AVG function can be used in the subqueries. It can be used along with the control flow functions as well like IF, IFNULL, NULLIF, and CASE.

Recommended Articles

This is a guide to MySQL Average. Here we discuss the Introduction of MySQL Average and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –

  1. Introduction to MySQL Operators
  2. Top 23 MySQL String functions
  3. MySQL vs SQLite | Top 14 Comparisons
  4. Guide to MySQL Timestamp
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ 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