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
- 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.