T-SQL by Example
Browse all examples below.
Basics
- Hello, SELECT — Selecting constants and a first query.
- Columns & Aliases — Selecting specific columns, aliasing, schema-qualified names.
- WHERE Filters — Common predicates, BETWEEN/IN/LIKE/IS NULL and sargability notes.
- ORDER BY & TOP — Sorting, TOP, WITH TIES, NULLS LAST workaround.
- CASE Expressions — Simple vs searched CASE, default branch, common patterns.
- NULLs & 3-Valued Logic — NULL behavior, COALESCE/ISNULL, comparisons and pitfalls.
Joins & APPLY
- INNER JOIN — Join two tables with matching keys; basic patterns.
- OUTER JOINs — LEFT/RIGHT/FULL joins, handling unmatched rows.
- CROSS/OUTER APPLY — Per-row subqueries/TVFs; choose CROSS vs OUTER APPLY.
Aggregation
- GROUP BY & Aggregates — Compute counts and sums; HAVING for post-aggregate filters.