SQL Quick Reference
PostgreSQL 15 · SELECT-only · 13 categories · learnwithsql.com
For learning SQL at learnwithsql.com
SELECT col1, col2Choose columns to return
SELECT *Return all columns
FROM table_nameSpecify the source table
WHERE conditionFilter rows before grouping
ORDER BY col ASC|DESCSort the result set
LIMIT n / OFFSET nReturn at most n rows / skip first n
DISTINCTRemove duplicate rows
col AS aliasRename a column or expression
AND / OR / NOTCombine conditions
IN (val1, val2, …)Match any value in a list
NOT IN (…)Exclude values in a list
BETWEEN a AND bRange check, inclusive
LIKE 'J%'% = any chars, _ = one char
ILIKE 'j%'Case-insensitive LIKE (Postgres)
IS NULL / IS NOT NULLCheck for missing value
EXISTS (subquery)True if subquery returns any row
COUNT(*) / COUNT(col)Count rows / non-null values
COUNT(DISTINCT col)Count unique non-null values
SUM(col) / AVG(col)Total / average (ignores NULLs)
MIN(col) / MAX(col)Smallest / largest value
GROUP BY colCollapse rows sharing a value
HAVING conditionFilter on aggregate result
FILTER (WHERE cond)Conditional aggregate
INNER JOIN t ON a.id = b.idRows matching in both tables
LEFT JOIN t ON …All left rows + matching right (NULL if none)
RIGHT JOIN t ON …All right rows + matching left
FULL OUTER JOIN t ON …All rows from both tables
CROSS JOIN tEvery combination — cartesian product
USING (col)Join on same-named column
WHERE col IN (SELECT …)Filter using subquery list
WHERE col = (SELECT …)Scalar subquery — one value
WHERE EXISTS (SELECT 1 …)True if subquery has any result
FROM (SELECT …) AS tInline view / derived table
Correlated subqueryReferences outer query column
WITH cte AS (SELECT …)Named temporary result set
WITH a AS (…), b AS (…)Chain multiple CTEs
WITH RECURSIVE cte AS (…)Self-referencing CTE for hierarchies
func() OVER ()Apply across all rows
OVER (PARTITION BY col)Reset window per group
OVER (ORDER BY col)Ordered window — running totals
ROW_NUMBER()Unique position per row
RANK() / DENSE_RANK()Rank with / without gaps on ties
NTILE(n)Divide rows into n equal buckets
LAG(col,n) / LEAD(col,n)Value n rows before / after
FIRST_VALUE() / LAST_VALUE()First / last in the window frame
SUM(col) OVER (…)Running total
ROWS BETWEEN … AND …Define window frame explicitly
UPPER(col) / LOWER(col)Change case
LENGTH(col)Number of characters
TRIM(col)Remove leading/trailing spaces
SUBSTRING(col, start, len)Extract part of a string
LEFT(col,n) / RIGHT(col,n)First / last n characters
CONCAT(a,b) or a || bJoin two strings
REPLACE(col, old, new)Swap all occurrences
SPLIT_PART(col, delim, n)Return nth part after splitting
INITCAP(col)Capitalise first letter of each word
LPAD(col,n,fill) / RPAD(…)Pad string to width n
NOW()Current timestamp with time zone
CURRENT_DATEToday's date (no time)
DATE_TRUNC('month', col)Round timestamp down to period
EXTRACT(part FROM col)Get numeric date part (year, month…)
AGE(end, start)Interval between two dates
col + INTERVAL '3 days'Add an interval to a date
TO_CHAR(col, 'YYYY-MM')Format date as string
col::dateCast timestamp to date
CAST(col AS integer)Standard SQL cast
col::integer / ::textPostgres shorthand cast
col::numeric(10,2)Decimal with precision
col::booleanConvert to true / false
col::date / ::timestampConvert to date or timestamp
CASE WHEN … THEN … ENDConditional expression
COALESCE(a, b, c)Return first non-null value
NULLIF(a, b)Returns NULL if a = b, else a
+ - * / %Basic arithmetic; % is modulo
ROUND(col, n)Round to n decimal places
FLOOR(col) / CEIL(col)Round down / up to integer
ABS(col)Absolute value
POWER(base, exp) / SQRT(col)Exponentiation / square root
UNIONCombine results, remove duplicates
UNION ALLCombine results, keep duplicates (faster)
INTERSECTRows that appear in both queries
EXCEPTRows in first query but not second