Tutorials

SQL Aggregate Function


SQL AVG


In sql, AVG() function returns the average value of the columns having numeric value.

Syntax for AVG()

SELECT AVG(column_name)
FROM table_name
WHERE condition;

Example on AVG()

We will use 'tih_Courses' table for demonstration purpose

tih_Courses
Course_ID Course_Name Professor Mode_of_Training Course_Fee
001 .Net Mukesh Tiwari Online and Offline 550
002 Java Rajneesh Shukla Online and Offline 550
003 ANSI SQL Rajneesh Shukla Online 500
004 Basics of Computer Shweta  Online 350
005 Blogging Tutorial Puja  Online 250
006 JSP Alok Pandey Online and Offline 350
007 Servlet Mukesh Tiwari Online and Offline 350
Query-1: Write a sql query to calculate average course fee of all the courses
SELECT AVG(Course_Fee)
FROM tih_Courses

After above sql query gets executed, we will get following result:

AVG(Course_Fee)
414.285714285

Query-2: Write a sql query to calculate the average course fee of Professor Mukesh Tiwari

SELECT AVG(Course_Fee)
FROM tih_Courses
WHERE Professor = 'Mukesh Tiwari';

After above sql query gets executed, we will get following result:

AVG(Course_Fee)
450

Similarly we can use AVG() on different column having numeric value applying different condition to get the average result.



Please Share this page
Views : 26