<aside> ⚠️

Syntax can vary slightly by engine (Postgres, MySQL, BigQuery, Snowflake). These are the widely supported forms; check your engine's docs for edge cases.

</aside>

Aggregate functions

Function What it does
COUNT() Counts rows (COUNT(*) counts all rows, COUNT(column) skips nulls)
SUM() Adds up numeric values
AVG() Calculates the mean
MIN() / MAX() Finds the smallest or largest value
GROUP BY Groups rows so aggregate functions apply per group, not the whole table

Window functions

Function What it does
ROW_NUMBER() Numbers each row within a partition, no ties
RANK() Ranks rows within a partition, with gaps after ties
DENSE_RANK() Ranks rows within a partition, without gaps after ties
LAG() Looks at the value from a previous row
LEAD() Looks at the value from a following row
NTILE(n) Splits rows into n roughly equal buckets
OVER (PARTITION BY ... ORDER BY ...) Defines the window a function operates across

String functions

Function What it does
CONCAT() Joins strings together
SUBSTRING() Extracts part of a string
TRIM() Removes leading and trailing whitespace
UPPER() / LOWER() Changes case
REPLACE() Swaps one substring for another
LENGTH() Returns the number of characters

Date and time functions

Function What it does
CURRENT_DATE / NOW() Returns today's date or the current timestamp
DATE_TRUNC() Rounds a date down to a unit (day, month, year)
DATEDIFF() Calculates the difference between two dates
EXTRACT() Pulls out a specific part of a date (year, month, day)

Conditional logic

Function What it does
CASE WHEN ... THEN ... ELSE ... END Inline if/else logic in a query
COALESCE() Returns the first non-null value in a list
NULLIF() Returns null if two values are equal, otherwise the first value

Query structure

Clause What it does
WITH <name> AS (...) Defines a common table expression (CTE) to reference later in the query
INNER JOIN Returns rows with a match in both tables
LEFT JOIN Returns all rows from the left table, matched where possible
UNION Combines results from two queries, removing duplicates (UNION ALL keeps them)