SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

SQL AVG Command


AVG sql command is used to find out average value of a numeric field in a table.  Now let us apply this AVG command to this table and find out the average  mark obtain by all the students. The AVG command will calculate the average value  of all the marks

id name class mark
1 John Deo Four 75
2 Max Ruin Three 85
3 Arnold Three 55
4 Krish Star Four 60
5 John Mike Four 60
6 Alex John Four 55
  Same way we can get the minimum value of a range of records by using SQL MIN command. Also check up SQL MAX command to get highest value of data
We will apply the AVG command here like this to the field  mark

SELECT avg( mark ) FROM `student`
avg(mark)
65.0000
The command will calculate average value of marks considering all the marks of the table. We can define some header  like this also.
SELECT AVG(mark) as avg_mark FROM `student` 
avg_mark
65.0000

Here we can use the Group By command to find out the average  mark obtained by each classes. 
Related Tutorial
SQL Group by
MySQL Max
MySQL Min
MySQL Sum
Copy data to new table
SQL Left Join



SELECT class, avg( mark ) as avg_mark FROM `student` GROUP BY class

class avg_mark
Four 62.5000
Three 70.0000

Please see the SQL SUM command to know the details on uses of GROUP BY command and the precautions. 


We can add condition to the sql command to get our desired result. We can add one Where clause to the query to consider records for which mark is more than some value ( say 55 )


SELECT avg( mark ) as avg_mark, class FROM student where mark > 55 GROUP BY class

avg_mark class
65.0000 Four
85.0000 Three

Other sql commands like between can be used along with this avg command to find out required results.











lotr04-04-2009
Note that on some mysql servers the autorounding is bad handled ...
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked
Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
SQL Tutorial List
MySQL Math
SQL Commands
SQL Sections
Date & Time
Join Table
String
Math
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.