Reference

SQL Syntax Types

Use this page as a quick syntax map.
It is designed for lookup and review, not as a step-by-step tutorial.

Query Structure Clauses #

SyntaxMeaning
SELECTchoose columns to return
FROMdefine source table(s)
WHEREfilter rows before grouping
GROUP BYaggregate by key(s)
HAVINGfilter grouped/aggregated results
ORDER BYsort output
LIMITrestrict number of output rows
UNION / UNION ALLstack result sets (distinct vs keep duplicates)
INTERSECT / EXCEPTset intersection and difference

Comparison Operators #

SyntaxMeaning
=equal to
<> or !=not equal to
>, <greater than / less than
>=, <=greater/less with equality

Logical and Filtering Syntax #

SyntaxMeaning
ANDall conditions must be true
ORat least one condition is true
NOTinvert a condition
IN (...)value is in a list
BETWEEN a AND bvalue is within a range
LIKEpattern matching with wildcards
IS NULL / IS NOT NULLcheck missing values

Join Syntax #

SyntaxMeaning
INNER JOINonly matching rows
LEFT JOINall left rows + matches from right
RIGHT JOINall right rows + matches from left
FULL OUTER JOINall rows from both sides
CROSS JOINevery left row paired with every right row
ONjoin condition

Aggregation Syntax #

SyntaxMeaning
COUNT()count rows/non-null values
SUM()total numeric values
AVG()average numeric value
MIN(), MAX()smallest/largest value

Window Syntax #

SyntaxMeaning
OVER (...)define window context for row-wise analytics
PARTITION BYreset window calculation per group
ROW_NUMBER()unique sequence per partition
RANK()ranking with gaps on ties
DENSE_RANK()ranking without gaps on ties
LAG(), LEAD()look at previous/next row values
NTILE(n)bucket rows into n tiles (quartiles, deciles)
FIRST_VALUE(), LAST_VALUE()value from the first/last row in the window
QUALIFYfilter on window-function result (DuckDB / Snowflake / BigQuery)

Control and Composition Syntax #

SyntaxMeaning
CASE WHEN ... THEN ... ELSE ... ENDconditional logic
WITH ... AS (...)define CTE(s) for readable query steps
WITH RECURSIVE ... AS (...)self-referencing CTE for hierarchies / sequences
EXISTS (SELECT 1 FROM ...)membership check, NULL-safe alternative to IN
CAST(x AS type) / x::typeconvert value to another type

Reshaping Syntax #

SyntaxMeaning
PIVOT ... ON ... USING ...rotate row values into columns (DuckDB / Snowflake / BigQuery / SQL Server)
UNPIVOT ... ON ... INTO NAME ... VALUE ...rotate columns into rows