SQL Functions Cheat Sheet (Tree Structure)
-- Numeric Functions --
ABS(number)
Returns positive (absolute) value. Use for differences, errors.
CEIL(number) / CEILING(number)
Rounds up to nearest integer. Use for stock quantities, pages.
FLOOR(number)
Rounds down to nearest integer. Use for rounding monetary units, scores.
ROUND(number, decimals)
Rounds numeric values to a fixed number of decimal places. Use when displaying or calculating prices,
averages.
POWER(base, exponent)
Raises number to a power. Use for exponential calculations.
MOD(a, b)
Returns remainder after division. Use for alternate row flagging.
LOG(number)
Returns natural logarithm value. Use in statistical or growth models.
EXP(number)
Returns e raised to the given number. Use for growth/compound calculations.
-- String Functions --
LENGTH(string) / LEN(string)
Returns number of characters in a string. Use for data validation, text analysis.
UPPER(string)
Converts text to uppercase. Use for formatting or case-insensitive comparisons.
LOWER(string)
Converts text to lowercase. Use for standardizing text for searches.
CONCAT(str1, str2, ...)
Joins multiple strings into one. Use for combining names, IDs.
SUBSTRING(string, start, length)
Extracts a substring. Use for parsing fixed formats.
TRIM(string)
Removes spaces from both sides of text. Use for cleaning inputs.
-- Date/Time Functions --
NOW()
Returns current date and time of the system. Use in logs, timestamps.
DATEPART(part, date) / EXTRACT(part FROM date)
Gets specific part of a date. Use for reporting by period.
DATEDIFF(end_date, start_date)
Returns number of days between two dates. Use for age, durations.
-- Aggregate Functions --
SUM(column)
Adds numeric values together. Use in totals, group-wise totals.
COUNT(*), COUNT(column)
Counts number of rows (or non-NULL values). Use for record counts, group sizes.
AVG(column)
Calculates average of numeric values. Use for KPIs, scores, averages.
MAX(column)
Finds largest value. Use for highest price, latest date.
MIN(column)
Finds smallest value. Use for lowest salary, earliest date.
-- Conversion Functions --
CAST(expression AS type)
Converts a value to a different data type. Use when joining incompatible types or formatting output.
COALESCE(value1, value2, ...)
Returns first non-null value. Use for default values, null handling.
ISNULL(column, 'replacement')
Replaces nulls with a default value. Use to handle missing data.
-- Conditional Functions --
CASE WHEN condition THEN result ELSE other END
Executes conditional logic like if-else. Use for custom categorizations.
-- Window Functions --
ROW_NUMBER() OVER(ORDER BY column)
Assigns sequential numbers to rows within partitions. Use for ranking, pagination.
RANK() OVER(ORDER BY column)
Ranks rows with gaps in case of ties. Use for position rankings.
DENSE_RANK() OVER(ORDER BY column)
Ranks rows without gaps. Use when ranking but maintaining sequence.
SUM(column) OVER(PARTITION BY col)
Computes sum as window function (per partition or whole set). Use for running totals, group-wise totals.
LEAD(column) OVER(ORDER BY col)
Fetches value from next row. Use for comparisons with future records.
LAG(column) OVER(ORDER BY col)
Fetches value from previous row. Use for comparisons with past records.
DISTINCT
Removes duplicate values. Use when unique records are needed.