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 #
| Syntax | Meaning |
|---|---|
SELECT | choose columns to return |
FROM | define source table(s) |
WHERE | filter rows before grouping |
GROUP BY | aggregate by key(s) |
HAVING | filter grouped/aggregated results |
ORDER BY | sort output |
LIMIT | restrict number of output rows |
UNION / UNION ALL | stack result sets (distinct vs keep duplicates) |
INTERSECT / EXCEPT | set intersection and difference |
Comparison Operators #
| Syntax | Meaning |
|---|---|
= | equal to |
<> or != | not equal to |
>, < | greater than / less than |
>=, <= | greater/less with equality |
Logical and Filtering Syntax #
| Syntax | Meaning |
|---|---|
AND | all conditions must be true |
OR | at least one condition is true |
NOT | invert a condition |
IN (...) | value is in a list |
BETWEEN a AND b | value is within a range |
LIKE | pattern matching with wildcards |
IS NULL / IS NOT NULL | check missing values |
Join Syntax #
| Syntax | Meaning |
|---|---|
INNER JOIN | only matching rows |
LEFT JOIN | all left rows + matches from right |
RIGHT JOIN | all right rows + matches from left |
FULL OUTER JOIN | all rows from both sides |
CROSS JOIN | every left row paired with every right row |
ON | join condition |
Aggregation Syntax #
| Syntax | Meaning |
|---|---|
COUNT() | count rows/non-null values |
SUM() | total numeric values |
AVG() | average numeric value |
MIN(), MAX() | smallest/largest value |
Window Syntax #
| Syntax | Meaning |
|---|---|
OVER (...) | define window context for row-wise analytics |
PARTITION BY | reset 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 |
QUALIFY | filter on window-function result (DuckDB / Snowflake / BigQuery) |
Control and Composition Syntax #
| Syntax | Meaning |
|---|---|
CASE WHEN ... THEN ... ELSE ... END | conditional 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::type | convert value to another type |
Reshaping Syntax #
| Syntax | Meaning |
|---|---|
PIVOT ... ON ... USING ... | rotate row values into columns (DuckDB / Snowflake / BigQuery / SQL Server) |
UNPIVOT ... ON ... INTO NAME ... VALUE ... | rotate columns into rows |