An SQL aggregate function calculates on a set of values and returns a single value Because an aggregate function operates on a set of values, it is often used with the GROUP BY clause of the SELECT statement. The GROUP BY clause divides the result set into groups of values and the aggregate function returns a single value for each group. SQL Tutorial
The following are the commonly used SQL aggregate functions:
AVG() – returns the average of a set
COUNT() – returns the number of items in a set
COUNT(*) – returns the number of items in a set (including NULL values)
MAX() – returns the maximum value in a set
MIN() – returns the minimum value in a set
SUM() – returns the sum of all or distinct values in a set
Except for the COUNT() function, SQL aggregate functions ignore null.
You can use aggregate functions as expressions only in the following:
The select list of a SELECT statement, either a subquery or an outer query.
A HAVING clause
StringFunction
Functions that allow you to manipulate string data more effectively.
CONCAT(s1,s2,...,sn) – return the result of concatenation two strings together.
LOWER(str),UPPER(str) – converts a string to lower case or upper case.
LPAD(str,n,padstr),RPAD(str,n,padstr) – left/right pads a string with another string to a certain length.
TRIM(str),LTRIM(str),RTRIM(str) – removes all spaces from a string or from the left or right side of a string.
REPLACE(str,source,replace) – replaces all occurrences of a substring within a string with another substring.
SUBSTRING(str,pos,len) – returns a substring from a string starting at a specified position with a specified length. Reference:
Math Functions
SQL has many mathematical functions that allow you to perform business and engineering calculations.
ABS(x) – returns the absolute value of x.
CEIL(x) – returns the smallest integer that is greater than or equal to x.
FLOOR(x) – returns the largest integer that is less than or equal to x.
RAND() – returns a random floating-point value.
MOD(x,y) – returns the remainder(modulo) of x divided by y, MOD(10,7) returns 3. e.g.
ROUND(x,d) – returns a number rounded to d decimal places. ROUND(2.6666,3) returns 2.667 e.g.