<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>
| 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 |
| 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 |
| 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 |
| 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) |
| 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 |
| 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) |