Aggregate Functions
Examples of Aggregate Functions
Below are several examples of SQL aggregate functions. You can use these with a GROUP BY clause to aggregate rows based on the specified GROUP BY criterion. This can be a reporting tool.
COUNT
Returns the number of rows matching the query criteria.
SELECT COUNT(*) FROM INVOICES WHERE CustomerName = 'NewTech Industries'
COUNT(DISTINCT)
Returns the number of distinct, non-null field values matching the query criteria.
SELECT COUNT(DISTINCT InvoiceId) AS DistinctValues FROM INVOICES WHERE CustomerName = 'NewTech Industries'
AVG
Returns the average of the column values.
SELECT InvoiceNumber, AVG(AnnualRevenue) FROM INVOICES WHERE CustomerName = 'NewTech Industries' GROUP BY InvoiceNumber
MIN
Returns the minimum column value.
SELECT MIN(AnnualRevenue), InvoiceNumber FROM INVOICES WHERE CustomerName = 'NewTech Industries' GROUP BY InvoiceNumber
MAX
Returns the maximum column value.
SELECT InvoiceNumber, MAX(AnnualRevenue) FROM INVOICES WHERE CustomerName = 'NewTech Industries' GROUP BY InvoiceNumber
SUM
Returns the total sum of the column values.
SELECT SUM(AnnualRevenue) FROM INVOICES WHERE CustomerName = 'NewTech Industries'
COUNT
Returns the number of rows matching the query criteria.
SELECT COUNT(*) FROM INVOICES WHERE CustomerName = 'NewTech Industries'
COUNT(DISTINCT)
Returns the number of distinct, non-null field values matching the query criteria.
SELECT COUNT(DISTINCT InvoiceId) AS DistinctValues FROM INVOICES WHERE CustomerName = 'NewTech Industries'
AVG
Returns the average of the column values.
SELECT InvoiceNumber, AVG(AnnualRevenue) FROM INVOICES WHERE CustomerName = 'NewTech Industries' GROUP BY InvoiceNumber
MIN
Returns the minimum column value.
SELECT MIN(AnnualRevenue), InvoiceNumber FROM INVOICES WHERE CustomerName = 'NewTech Industries' GROUP BY InvoiceNumber
MAX
Returns the maximum column value.
SELECT InvoiceNumber, MAX(AnnualRevenue) FROM INVOICES WHERE CustomerName = 'NewTech Industries' GROUP BY InvoiceNumber
SUM
Returns the total sum of the column values.
SELECT SUM(AnnualRevenue) FROM INVOICES WHERE CustomerName = 'NewTech Industries'