Writing a CTE name two times can mean doing the work two times. Two small demos show when a temp table helps and when it only adds cost.
Triangular Joins: The Hidden Row Explosion in Self-Joins
A small grouped result can hide a huge pile of row pairs. Here is how to count them and what to do instead.
Aggregate Before the Join: Shrinking Rows Early With GROUP BY
Aggregate before the join only when the grouping preserves the required customer result. This private lab checks NULLs, missing matches, duplicate dimensions, complete outputs, actual plans, and reads.
ISO Week Numbers vs DATEPART(week): Grouping Sales by Week
Group sales by ISO week and ISO year, compare DATEPART week behavior, and avoid New Year errors caused by session settings.
Finding Duplicates Before Adding a Unique Constraint
A unique constraint fails on the first duplicate it meets and tells you about only one. Here is how to list them all and clean up safely.
Value Histograms: Counting Rows per Range Bucket in T-SQL
Turn a column of amounts into a clear shape. Three small traps decide whether your bucket counts are true: empty buckets, uneven ranges and BETWEEN.
PIVOT and UNPIVOT: Aggregation, NULLs and Cross-Tab Queries
PIVOT and UNPIVOT reshape SQL Server data. See a tested example, disappearing NULLs, aggregation and fixed cross-tab columns.







