Aggregate functions in SQL

I would like to explain about various aggregate functions available in sql server. SQL server has got several helpful functions that would help us in doing some operations very effectively without writing our own logics. One of those features is aggregates. Learn Aggregate functions in SQL.

About aggregate functions in SQL

• AVG: Average of the column. If you want to get the average of marks for a student then you can use this function as

select avg(marks) as average marks
from student

• COUNT: Number of records. If you want to count number students who has got >50 marks in maths

select count(*)
from student
where marks>50 and subject='maths'

• MAX: Maximum of the column. If you want to find out who has got maximun marks in any subject then

select max(marks)
from student

• MIN: Minimum of the column. This is opposite to the max function

select min(marks)
from student

• SUM: Sum of the column. To find out total marks of a student

select sum(marks) as total
from student
where studentid=1

Other than aggregate functions we can write our own user defined functions which will do desired operation; so that we need not write the same logic again and again. For example, having a function to take a comma separated value as input and retuning a table as output with splitted values in each row.

SQL functions are precompiled and cached so they are pretty fast in run time.


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: