PostgreSQL Functions

Built-in PostgreSQL functions

String Functions

CONCAT(first_name, ' ', last_name) # concatenate
LENGTH(name) # string length
UPPER(name) # uppercase
LOWER(email) # lowercase
SUBSTRING(name, 1, 3) # extract substring
TRIM(name) # remove whitespace
REPLACE(text, 'old', 'new') # replace text

Numeric Functions

ABS(-10) # absolute value (10)
CEIL(4.3) # round up (5)
FLOOR(4.7) # round down (4)
ROUND(4.567, 2) # round to 2 decimals (4.57)
POWER(2, 3) # 2^3 = 8
SQRT(16) # square root (4)

Date Functions

NOW() # current timestamp
CURRENT_DATE # current date
CURRENT_TIME # current time
AGE(timestamp) # interval from timestamp to now
EXTRACT(YEAR FROM date) # extract year
DATE_TRUNC('month', timestamp) # truncate to month

Aggregate Functions

COUNT(*) # count rows
SUM(amount) # sum values
AVG(price) # average
MIN(age) # minimum
MAX(age) # maximum
STRING_AGG(name, ',') # concatenate strings

Array Functions

array_length(arr, 1) # array length
array_append(arr, val) # append value
unnest(arr) # expand array to rows