SQL Functions
A reference for the SQL functions you use every day, with interactive examples.
Numeric
ABS()
NumericReturns the absolute (non-negative) value of a number.
CEIL()
NumericRounds a number up to the nearest integer (ceiling).
FLOOR()
NumericRounds a number down to the nearest integer (floor).
GREATEST()
NumericReturns the largest value from a list of expressions.
LEAST()
NumericReturns the smallest value from a list of expressions.
MOD()
NumericReturns the remainder of a division (modulo).
POWER()
NumericRaises a base to a specified exponent.
ROUND()
NumericRounds a numeric value to a specified number of decimal places.
SIGN()
NumericReturns -1, 0, or 1 indicating the sign of a number.
SQRT()
NumericReturns the square root of a non-negative number.
TRUNC()
NumericTruncates a number toward zero, removing digits past a given precision.
Aggregate
ARRAY_AGG()
AggregateCollects values from a group into a single array (list).
AVG()
AggregateCalculates the average (mean) of the values in a numeric column across a group of rows.
BOOL_AND()
AggregateReturns TRUE if every value in the group is TRUE (logical AND across rows).
BOOL_OR()
AggregateReturns TRUE if at least one value in the group is TRUE (logical OR across rows).
COUNT()
AggregateReturns the number of rows that match a query, optionally counting only non-null values in a specific column.
MAX()
AggregateReturns the largest value in a column across a group of rows.
MIN()
AggregateReturns the smallest value in a column across a group of rows.
STDDEV()
AggregateReturns the standard deviation of a set of values, measuring spread around the mean.
STRING_AGG()
AggregateConcatenates values from a group into a single string, separated by a delimiter.
SUM()
AggregateAdds up all the values in a numeric column across a group of rows.
VARIANCE()
AggregateReturns the variance (squared spread) of a set of numeric values.
Conversion
CAST()
ConversionConverts a value from one data type to another, such as a number to text or text to a number.
COALESCE()
ConversionReturns the first non-null value from a list of expressions.
NULLIF()
ConversionReturns NULL if two expressions are equal; otherwise returns the first.
TO_CHAR()
ConversionFormats a date, number, or timestamp as a string using a format pattern.
TO_DATE()
ConversionParses a string into a DATE using a format pattern.
String
CONCAT()
StringJoins two or more strings together into a single string.
LEFT()
StringReturns the first N characters from the left of a string.
LENGTH()
StringReturns the number of characters in a string.
LOWER()
StringConverts all characters in a string to lowercase.
LTRIM()
StringRemoves leading (left-side) characters, usually spaces, from a string.
POSITION()
StringReturns the 1-based location of a substring within a string.
REPEAT()
StringConcatenates a string to itself a given number of times.
REPLACE()
StringReplaces all occurrences of a substring within a string.
REVERSE()
StringReturns the characters of a string in reverse order.
RIGHT()
StringReturns the last N characters from the right of a string.
RTRIM()
StringRemoves trailing (right-side) characters, usually spaces, from a string.
SPLIT_PART()
StringSplits a string on a delimiter and returns the Nth piece.
SUBSTRING()
StringExtracts a portion of a string, starting at a given position and running for a given length.
TRIM()
StringRemoves leading and/or trailing characters (usually spaces) from a string.
UPPER()
StringConverts all characters in a string to uppercase.
Date & Time
CURRENT_DATE()
Date and TimeReturns today's date (no time component).
CURRENT_TIMESTAMP()
Date and TimeReturns the current date and time (with time zone in some engines).
DATE_ADD()
Date and TimeAdds an interval to a date or timestamp.
DATE_SUB()
Date and TimeSubtracts an interval from a date or timestamp.
DATE_TRUNC()
Date and TimeTruncates a date/timestamp down to the start of a given unit.
DATEDIFF()
Date and TimeReturns the difference between two dates in a specified unit.
EXTRACT()
Date and TimePulls a single date part (year, month, day, ...) out of a date or timestamp.
NOW()
Date and TimeReturns the current date and time.