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  
 45600  

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.

Comments

Popular Posts from this Blog

N-th Tribonacci Number Solution - The Coding Shala

Find Second Smallest Element in the Array - The Coding Shala

New Year Chaos Solution - The Coding Shala

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

Shell Script to Create a Simple Calculator - The Coding Shala