SQL Quick Reference
The 34 most useful clauses and functions for everyday analysis, from SELECT and GROUP BY through joins and window functions.
Download quick reference ↓Learning resource
A searchable reference for core SQL, PostgreSQL, analysis functions, joins, window functions, and more. SQL differs across database systems, so every entry names its dialect and whether it works in this site’s browser playground.
Downloadable resources
Use the concise cheat sheet for quick lookups or download the complete reference for studying and offline use.
The 34 most useful clauses and functions for everyday analysis, from SELECT and GROUP BY through joins and window functions.
Download quick reference ↓All 70 entries with plain-language definitions, examples, dialect notes, difficulty, and playground compatibility.
Download complete guide ↓Open the full reference in Excel or Google Sheets, then sort and filter it by category, level, dialect, or playground support.
Download CSV ↓SELECTChooses the columns or expressions returned by a query.
SELECT name, district FROM tributes;DISTINCTRemoves duplicate result rows.
SELECT DISTINCT district FROM tributes;WHEREKeeps only rows that meet a condition.
SELECT * FROM tributes WHERE score >= 8;AND / OR / NOTCombines or reverses filtering conditions.
WHERE district = 12 AND score >= 8INMatches any value in a list.
WHERE district IN (4, 7, 12)BETWEENTests whether a value falls inside an inclusive range.
WHERE score BETWEEN 7 AND 10LIKEMatches a text pattern using % and _ wildcards.
WHERE name LIKE 'K%';IS NULLFinds missing values; use IS NOT NULL for present values.
WHERE mentor IS NULLORDER BYSorts results ascending or descending.
ORDER BY score DESC, name ASCLIMITReturns only a specified number of rows.
SELECT * FROM tributes LIMIT 5;FETCH FIRSTStandard-SQL alternative for limiting rows.
FETCH FIRST 5 ROWS ONLYASGives a column or table a temporary alias.
SELECT score AS training_score FROM tributes;COUNT()Counts rows or non-null values.
SELECT COUNT(*) AS tributes FROM tributes;SUM()Adds numeric values.
SELECT SUM(points) FROM arena_events;AVG()Calculates the arithmetic mean.
SELECT AVG(score) FROM tributes;MIN() / MAX()Returns the smallest or largest value.
SELECT MIN(score), MAX(score) FROM tributes;GROUP BYCombines rows into groups for aggregate calculations.
SELECT district, AVG(score) FROM tributes GROUP BY district;HAVINGFilters groups after aggregation.
GROUP BY district HAVING AVG(score) >= 8CASECreates conditional values inside a query.
CASE WHEN score >= 8 THEN 'High' ELSE 'Other' ENDCOALESCE()Returns the first non-null expression.
COALESCE(mentor, 'Unknown')NULLIF()Returns NULL when two expressions are equal.
points / NULLIF(events, 0)CAST()Converts a value to another data type.
CAST(score AS DECIMAL(5,2))ROUND()Rounds a number to a chosen precision.
ROUND(AVG(score), 2)ABS()Returns a number's absolute value.
ABS(final_score - initial_score)CEILING() / CEIL()Rounds a number upward to an integer.
CEILING(7.2)FLOOR()Rounds a number downward to an integer.
FLOOR(7.9)POWER()Raises a number to a power.
POWER(score, 2)SQRT()Returns a square root.
SQRT(points)MOD()Returns the remainder after division.
MOD(district, 2)LOWER() / UPPER()Changes text to lower- or uppercase.
UPPER(name)TRIM()Removes leading and trailing spaces.
TRIM(name)LTRIM() / RTRIM()Removes spaces from one side of text.
LTRIM(name)LENGTH() / LEN()Counts characters; the name varies by dialect.
LENGTH(name)SUBSTRING()Extracts part of a text value.
SUBSTRING(name FROM 1 FOR 3)REPLACE()Replaces matching text.
REPLACE(name, ' ', '_')CONCAT()Combines text values.
CONCAT(name, ' — District ', district)STRING_AGG()Combines values from multiple rows into one string.
STRING_AGG(name, ', ' ORDER BY name)REGEXP_REPLACE()Replaces text that matches a regular expression.
REGEXP_REPLACE(name, '[^A-Za-z]', '', 'g')CURRENT_DATEReturns today's date.
SELECT CURRENT_DATE;CURRENT_TIMESTAMPReturns the current date and time.
SELECT CURRENT_TIMESTAMP;EXTRACT()Pulls a component such as year or month from a date.
EXTRACT(YEAR FROM event_date)DATE_TRUNC()Rounds a timestamp down to a time unit.
DATE_TRUNC('month', event_date)AGE()Calculates an interval between dates.
AGE(CURRENT_DATE, birth_date)INTERVALRepresents a duration used in date arithmetic.
event_date + INTERVAL '30 days'INNER JOINReturns rows that match in both tables.
FROM tributes t JOIN arena_events e ON t.id = e.tribute_idLEFT JOINKeeps every left-table row and matching right rows.
FROM tributes t LEFT JOIN arena_events e ON t.id = e.tribute_idRIGHT JOINKeeps every right-table row and matching left rows.
FROM tributes t RIGHT JOIN arena_events e ON t.id = e.tribute_idFULL OUTER JOINKeeps matched and unmatched rows from both tables.
FROM tributes t FULL OUTER JOIN arena_events e ON t.id = e.tribute_idCROSS JOINReturns every possible pairing of two tables.
FROM tributes CROSS JOIN arenasUNIONStacks query results and removes duplicates.
SELECT name FROM tributes UNION SELECT name FROM mentors;UNION ALLStacks query results and keeps duplicates.
SELECT name FROM tributes UNION ALL SELECT name FROM mentors;INTERSECTReturns rows present in both query results.
query_one INTERSECT query_twoEXCEPTReturns first-query rows absent from the second.
query_one EXCEPT query_twoWITH (CTE)Names a temporary result used by the main query.
WITH ranked AS (SELECT * FROM tributes) SELECT * FROM ranked;EXISTSTests whether a subquery returns at least one row.
WHERE EXISTS (SELECT 1 FROM arena_events e WHERE e.tribute_id = t.id)ROW_NUMBER()Assigns a unique sequence number within a result.
ROW_NUMBER() OVER (ORDER BY score DESC)RANK()Ranks rows with gaps after ties.
RANK() OVER (ORDER BY score DESC)DENSE_RANK()Ranks rows without gaps after ties.
DENSE_RANK() OVER (ORDER BY score DESC)LAG() / LEAD()Reads a value from a previous or following row.
LAG(score) OVER (ORDER BY event_date)FIRST_VALUE() / LAST_VALUE()Returns the first or last value in a window frame.
FIRST_VALUE(score) OVER (PARTITION BY district ORDER BY event_date)NTILE()Splits ordered rows into a chosen number of groups.
NTILE(4) OVER (ORDER BY score DESC)PERCENTILE_CONT()Calculates an interpolated percentile, including the median.
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY score)STDDEV_SAMP()Estimates the sample standard deviation.
STDDEV_SAMP(score)VAR_SAMP()Estimates the sample variance.
VAR_SAMP(score)FILTERApplies a condition to one aggregate calculation.
COUNT(*) FILTER (WHERE score >= 8)GENERATE_SERIES()Generates a sequence of numbers or dates.
SELECT * FROM GENERATE_SERIES(1, 12);TO_CHAR()Formats dates or numbers as text.
TO_CHAR(event_date, 'Mon YYYY')TO_DATE()Parses text into a date using a format.
TO_DATE('08/17/2026', 'MM/DD/YYYY')JSONB_EXTRACT_PATH_TEXT()Reads text from a JSONB value at a path.
JSONB_EXTRACT_PATH_TEXT(details, 'weapon')ARRAY_AGG()Collects grouped values into an array.
ARRAY_AGG(name ORDER BY name)