T-SQL by Example

Browse all examples below.

Basics

  1. Hello, SELECT — Selecting constants and a first query.
  2. Columns & Aliases — Selecting specific columns, aliasing, schema-qualified names.
  3. WHERE Filters — Common predicates, BETWEEN/IN/LIKE/IS NULL and sargability notes.
  4. ORDER BY & TOP — Sorting, TOP, WITH TIES, NULLS LAST workaround.
  5. CASE Expressions — Simple vs searched CASE, default branch, common patterns.
  6. NULLs & 3-Valued Logic — NULL behavior, COALESCE/ISNULL, comparisons and pitfalls.

Joins & APPLY

  1. INNER JOIN — Join two tables with matching keys; basic patterns.
  2. OUTER JOINs — LEFT/RIGHT/FULL joins, handling unmatched rows.
  3. CROSS/OUTER APPLY — Per-row subqueries/TVFs; choose CROSS vs OUTER APPLY.

Aggregation

  1. GROUP BY & Aggregates — Compute counts and sums; HAVING for post-aggregate filters.