SQL AVG() Function - The Coding Shala

Home >> Learn SQL >> SQL AVG() function

SQL AVG() Function

The SQL AVG() Function returns the average value of a numeric column.
SQL AVG() Function - The Coding Shala

SQL AVG() Syntax

 The basic syntax of SQL AVG() is as follows - 

SELECT AVG(column_name)  
 FROM table_name  
 WHERE condition; 

The following 'emp' table is used for the examples - 

emp_id     emp_name     city     dept_no     salary  
 1          Akshay        Pune      101       50000  
 3          Nikhil        Pune      101       51000  
 5          Mohit         Delhi     103       40000  
 6          Shubham       Surat     105       42000  
 7          Akash         Mumbai    106       45000  

SQL AVG() Examples 

The following SQL query will return the average salary of all the employees using SQL AVG() Function - 

 SQL >> select avg(salary) as 'Average Salary'   
        from emp;  
Output >>   
 Average Salary  

The following SQL query will return the average salary of employees who are from the same city - 

 SQL >>select city, avg(salary) as 'Average Salary'   
        from emp  
        group by city;  
Output >>   
 city     Average Salary  
 Delhi      40000  
 Mumbai     45000  
 Pune      50500  
 Surat     42000

Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.


Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Display the digits which are at odd positions in a given 5-digit number - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

Add two numbers in Scala - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala