← All cheatsheets
Data Analyst · #033 · August 25, 2026 · 2 min read

Which SQL functions do you actually use?

Aggregates, strings, dates, NULLs, window functions and conditionals: the 25 SQL functions that answer 80% of real queries, each with a five-word gloss, on one printable notebook page.

Get the free PDF

One page, print-ready, free to share. No signup needed.

Download the PDF

Nobody memorizes SQL. The people who look fast simply stop re-googling the same 25 functions, because those 25 answer 80% of real queries. Here they are, one notebook page, with a five-word gloss each. The print-ready A4 PDF is at the bottom.

Aggregates

  • COUNT(*): rows, nulls included.
  • COUNT(DISTINCT x): uniques.
  • SUM(x) / AVG(x): both skip nulls silently.
  • MIN(x) / MAX(x): work on dates too.

Strings

  • LOWER(x) / TRIM(x): clean before comparing, always.
  • CONCAT(a,b) or a || b: glue.
  • SUBSTRING(x,1,3): slice.
  • REPLACE(x,'a','b'): swap.
  • SPLIT_PART(x,',',1): everything before the first comma.

Dates

  • DATE_TRUNC('month', d): first day of the month, the backbone of every monthly report.
  • EXTRACT(year FROM d): pull one part out.
  • d + INTERVAL '7 day': date math.
  • CURRENT_DATE: today.

NULLs

  • COALESCE(x, 0): first non-null value.
  • NULLIF(x, 0): divide-by-zero armor.
  • x IS NULL: never = NULL. Never.

Window functions

  • ROW_NUMBER() OVER (…): the dedupe trick.
  • RANK() / DENSE_RANK(): differ on ties.
  • LAG(x) / LEAD(x): previous / next row.
  • SUM(x) OVER (ORDER BY d): running total.

Conditionals

  • CASE WHEN … THEN … END: if / else.
  • COUNT(*) FILTER (WHERE …): conditional count.
  • GREATEST(a,b) / LEAST(a,b): row-wise max and min.

The pattern worth stealing: dedupe

Strings and window functions together clean most real tables:

SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER (
    PARTITION BY LOWER(TRIM(email))
    ORDER BY created_at DESC) AS rn
  FROM customers
) t WHERE rn = 1;

Normalize inside the PARTITION BY, keep the newest row per person, drop the rest.

The trap: NULL breaks your math

You wroteWhat happensWrite instead
WHERE x = NULLalways emptyWHERE x IS NULL
AVG(score)ignores nulls silentlyAVG(COALESCE(score,0)) if 0 is real
a / berrors when b = 0a / NULLIF(b,0)

NULL is not a value, it is "unknown". Every comparison with it returns unknown, not false, and every aggregate quietly skips it.

Frequently asked questions

Which SQL functions should a beginner learn first?
The aggregates: COUNT(*), COUNT(DISTINCT x), SUM, AVG, MIN and MAX. They answer the first question every stakeholder asks (how many, how much) and they appear in virtually every production query. Learn GROUP BY with them and you can already produce most basic reports.
What is the difference between COALESCE and NULLIF?
COALESCE(x, 0) returns the first non-null argument, so it fills holes: a missing value becomes 0. NULLIF(x, 0) does the opposite: it turns a value INTO null when it equals the second argument, which is mostly used as divide-by-zero armor, as in a / NULLIF(b, 0).
Why does WHERE x = NULL return no rows?
Because NULL is not a value, it is unknown. Any comparison with NULL returns unknown, not true or false, so the row never passes the filter. Always write x IS NULL or x IS NOT NULL.
What is the most useful window function?
ROW_NUMBER() OVER (PARTITION BY key ORDER BY created_at DESC). Filter on the result being 1 and you keep exactly one row per key: this is the standard dedupe pattern, and it also answers latest-record-per-customer questions without a self-join.

Get the free PDF

One page, print-ready, free to share. No signup needed.

Download the PDF

More cheatsheets