SQL SUM() Function

SUM() FUNCTION:- The SQL SUM() function is an aggregate function that calculates the sum of all or Distinct value in an expression.

Syntax:- 
  • This query will  SUM all the records including duplicates.

         SELECT SUM(column_name)
         FROM table_name ;

  • Showing only unique values DISTINCT KEYWORD is used as follows:-
          SELECT SUM(DISTINCT column_name)
          FROM table_name ;

EXAMPLE:- To understand SUM function, consider a Persons table, which is having the following records:-

  • This example will SUM the column name Salary including duplicates values.
       SELECT  SUM(Salary) As TotalSalary 
       FROM Persons ;

OUTPUT:-

  • This example will SUM the column name Salary values With Distinct values.
         SELECT SUM(DISTINCT Salary) As TotalSalary 
         FROM Persons ;

OUTPUT:-



Comments

Popular posts from this blog

ASP.NET Overview

SQL AND ,OR, NOT Operators

SQL Joins