Getting started with Microsoft SQL ServerOVER ClausePIVOT / UNPIVOTDatabase SnapshotsRetrieve information about the databaseThe STUFF FunctionFOR XML PATHCursorsJoinCommon Table ExpressionsMove and copy data around tablesDatesLimit Result SetRetrieve Information about your InstanceWith Ties Option VariablesJSON in Sql ServerWindow functionsPartitioningStored ProceduresGROUP BYGenerating a range of datesCOALESCESplit String function in Sql ServerINSERT INTOCREATE VIEWString FunctionsResource GovernorORDER BYWHILE loopSystem database - TempDbMigrationPrimary KeysMERGEFull-Text IndexingFOR JSONSELECT statementDBMAILIndexQueries with JSON dataStoring JSON in SQL tablesOPENJSONRanking FunctionsTriggerConverting data typesNULLsTransaction isolation levelsAdvanced optionsIF...ELSETRY/CATCHData TypesUser Defined Table TypesTable Valued ParametersIn-Memory OLTP (Hekaton)Temporal TablesInsertSequencesSCOPE_IDENTITY()ViewsUse of TEMP TableScheduled Task or JobIsolation levels and lockingSorting/ordering rowsPrivileges or PermissionsForeign KeysSQLCMDFile Groupcross applyBasic DDL Operations in MS SQL ServerComputed ColumnsUNIONSubqueriesLast Inserted IdentityCLUSTERED COLUMNSTOREParsenameInstalling SQL Server on WindowsAggregate FunctionsQuerying results by pageSchemasBackup and Restore DatabaseTransaction handlingNatively compiled modules (Hekaton)Database permissionsSpatial DataDynamic SQLPaginationQuery HintsModify JSON textRow-level securityDynamic data maskingExport data in txt file by using SQLCMDEncryptionManaging Azure SQL DatabaseCommon Language Runtime IntegrationDelimiting special characters and reserved wordsCASE StatementDBCCBULK ImportQuery StoreService brokerAnalyzing a QueryMicrosoft SQL Server Management Studio Shortcut KeysPermissions and SecurityPHANTOM readFilestreamDrop KeywordString Aggregate functions in SQL ServerSQL Server Evolution through different versions (2000 - 2016)SQL Server Management Studio (SSMS)Logical FunctionsDynamic SQL PivotAlias Names in Sql Serverbcp (bulk copy program) Utility

Aggregate Functions

Other topics

SUM()

Returns sum of numeric values in a given column.

We have table as shown in figure that will be used to perform different aggregate functions. The table name is Marksheet.

enter image description here

Select SUM(MarksObtained) From Marksheet

The sum function doesn't consider rows with NULL value in the field used as parameter

In the above example if we have another row like this:

106    Italian    NULL

This row will not be consider in sum calculation

AVG()

Returns average of numeric values in a given column.

We have table as shown in figure that will be used to perform different aggregate functions. The table name is Marksheet.

enter image description here

Select AVG(MarksObtained) From Marksheet

The average function doesn't consider rows with NULL value in the field used as parameter

In the above example if we have another row like this:

106    Italian    NULL

This row will not be consider in average calculation

MAX()

Returns the largest value in a given column.

We have table as shown in figure that will be used to perform different aggregate functions. The table name is Marksheet.

enter image description here

Select MAX(MarksObtained) From Marksheet

MIN()

Returns the smallest value in a given column.

We have table as shown in figure that will be used to perform different aggregate functions. The table name is Marksheet.

enter image description here

Select MIN(MarksObtained) From Marksheet

COUNT()

Returns the total number of values in a given column.

We have table as shown in figure that will be used to perform different aggregate functions. The table name is Marksheet.

enter image description here

Select COUNT(MarksObtained) From Marksheet

The count function doesn't consider rows with NULL value in the field used as parameter. Usually the count parameter is * (all fields) so only if all fields of row are NULLs this row will not be considered

In the above example if we have another row like this:

106    Italian    NULL

This row will not be consider in count calculation

NOTE

The function COUNT(*) returns the number of rows in a table. This value can also be obtained by using a constant non-null expression that contains no column references, such as COUNT(1).

Example

Select COUNT(1) From Marksheet

COUNT(Column_Name) with GROUP BY Column_Name

Most of the time we like to get the total number of occurrence of a column value in a table for example:

TABLE NAME : REPORTS

ReportNameReportPrice
Test10.00 $
Test10.00 $
Test10.00 $
Test 211.00 $
Test10.00 $
Test 314.00 $
Test 314.00 $
Test 4100.00 $
SELECT  
    ReportName AS REPORT NAME, 
    COUNT(ReportName) AS COUNT 
FROM     
    REPORTS 
GROUP BY 
    ReportName 
REPORT NAMECOUNT
Test4
Test 21
Test 32
Test 41

Syntax:

  • AVG([ALL|DISTINCT]expression)
  • COUNT([ALL|DISTINCT]expression)
  • MAX([ALL|DISTINCT]expression)
  • MIN([ALL|DISTINCT]expression)
  • SUM([ALL|DISTINCT]expression)

Contributors

Topic Id: 5802

Example Ids: 20443,20444,20445,20446,20447,25679

This site is not affiliated with any of the contributors.