T-SQL by Example

Welcome to T‑SQL by Example. Each page shows a bite‑sized example with runnable code, output (when helpful), and a few notes & gotchas.

  • Start with Basics, then explore Joins, Aggregation, and more.
  • Code samples target Microsoft SQL Server (2016+ features are noted).

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.
  7. 🚧 DISTINCT & Deduping β€” Use DISTINCT and ROW_NUMBER to remove duplicates.
  8. 🚧 Subqueries β€” Scalar, correlated, and table subqueries.
  9. 🚧 EXISTS vs IN β€” Compare semi-joins with EXISTS and IN.
  10. 🚧 CTEs β€” Common Table Expressions, basic and recursive.
  11. 🚧 Set Operators β€” UNION, INTERSECT, and EXCEPT patterns.
  12. 🚧 CROSS JOIN & Self Join β€” Cartesian products and self-joins.

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.
  4. 🚧 Join Strategies β€” Hash, merge, and nested loops β€” recognition & when they appear.
  5. 🚧 Anti-join Patterns β€” NOT EXISTS vs LEFT JOIN/IS NULL.

Aggregation

  1. GROUP BY & Aggregates β€” Compute counts and sums; HAVING for post-aggregate filters.
  2. 🚧 HAVING vs WHERE β€” Pre-aggregate vs post-aggregate filtering.
  3. 🚧 Window Basics β€” ROW_NUMBER, RANK, DENSE_RANK.
  4. 🚧 Window Aggregates β€” SUM() OVER, running totals, moving averages.
  5. 🚧 Window Frames β€” ROWS vs RANGE, peers, gaps & islands.
  6. 🚧 Grouping Sets β€” ROLLUP, CUBE, and GROUPING SETS.

Data Modification

  1. 🚧 INSERT Patterns β€” Single row, multi-row, INSERT…SELECT, SELECT INTO.
  2. 🚧 UPDATE with JOIN & OUTPUT β€” Join in UPDATE; capture changes with OUTPUT.
  3. 🚧 DELETE Patterns β€” Delete with WHERE/JOIN; soft delete approaches.
  4. 🚧 MERGE (safe upserts) β€” Upserts with MERGE and caveats.
  5. 🚧 Identity & Sequences β€” IDENTITY columns and SQL Server SEQUENCE.

Programmability

  1. 🚧 Variables & Expressions β€” DECLARE, SET vs SELECT assignment, expressions.
  2. 🚧 Stored Procedures β€” Input/output params, RETURN, OUTPUT.
  3. 🚧 Functions: Scalar vs TVF β€” Scalar UDF, inline TVF, multi-statement TVF (perf notes).
  4. 🚧 TVPs (Table-Valued Parameters) β€” Table types and passing sets to procedures.
  5. 🚧 TRY…CATCH & XACT_STATE() β€” Error handling patterns for robust T-SQL.

Transactions & Concurrency

  1. 🚧 Transactions & Savepoints β€” BEGIN/COMMIT/ROLLBACK, savepoints.
  2. 🚧 Isolation Levels β€” RC, RCSI, RR, SR, SI β€” phenomena and trade-offs.
  3. 🚧 Deadlock Basics β€” Reproducing and diagnosing deadlocks.
  4. 🚧 Locking Hints β€” WITH (NOLOCK) and other hints β€” risks and uses.

Performance & Indexing

  1. 🚧 Sargability Deep Dive β€” Seek vs scan, avoid functions on indexed columns.
  2. 🚧 Index Basics β€” Clustered vs nonclustered, included columns.
  3. 🚧 Covering & Filtered Indexes β€” Cover hot queries; filter for selective subsets.
  4. 🚧 Statistics & Sniffing β€” Cardinality, auto-stats, parameter sniffing overview.
  5. 🚧 Execution Plans 101 β€” Operators, estimates vs actual, common shapes.
  6. 🚧 Temp Tables vs Table Variables β€” When to use #temp vs @table; indexing and stats.