SQL Functions
SQL has many built-in functions for performing calculations on data.
Sql has two type functions
- Aggregate Functions
- Scalar Functions
Aggregate Functions
SQL aggregate functions return a single value, calculated from values in a column.
list of aggregate functions:
AVG(expression)Computes the average value of a column by the expressionSyntax
SELECT AVG(column_name) FROM table_name
COUNT(expression)
Counts the rows defined by the expressionSyntax
SELECT COUNT(column_name) FROM table_name
COUNT(*)
Counts all rows in the specified table or viewSyntax
SELECT count(*) FROM table_name
MIN(expression)
Finds the minimum value in a column by the expressionSyntax
SELECT MIN(column_name) FROM table_name
MAX(expression)
Finds the maximum value in a column by the expressionSyntax
SELECT MAX(column_name) FROM table_name
SUM(expression)
Computes the sum of column values by the expressionSyntax
SELECT SUM(column_name) FROM table_name
Scalar Functions
SQL scalar functions return a single value, based on the input value.
UCASE()
Converts a field to upper case
SELECT UCASE(column_name) FROM table_name
LCASE()
Converts a field to lower case
SELECT LCASE(column_name) FROM table_name
MID()
Extract characters from a text field
SELECT MID(column_name,start[,length]) FROM table_name
LEN()
Returns the length of a text field
SELECT LEN(column_name) FROM table_name
ROUND()
Rounds a numeric field to the number of decimals specified
SELECT ROUND(column_name,decimals) FROM table_name
NOW()
Returns the current system date and time
SELECT NOW() FROM table_name
FORMAT()
Formats how a field is to be displayed
SELECT FORMAT(column_name,format) FROM table_name
