T-SQL clause selection: WHERE vs GROUP BY vs ORDER BY vs window function

Verdict: WHERE restricts which rows return. GROUP BY with an aggregate collapses detail rows into group totals. ORDER BY only sorts. A window function with PARTITION BY and ORDER BY computes running totals without collapsing the detail grain.

CriterionWHEREGROUP BYORDER BYWindow function
PurposeRestrict which rows returnAggregate rows into summary groupsSort the returned rowsCompute per-row values across a partition
Row grainPreserves rows meeting the conditionCollapses to one row per groupPreserves rows, reordersPreserves detail grain
ExampleWHERE OrderDate in current fiscal yearGROUP BY Category, Month with SUMORDER BY OrderDateSUM(...) OVER (PARTITION BY CustomerId ORDER BY OrderDate)
Choose whenCurrent fiscal year onlyTotal sales by category and monthPresent rows in date orderRunning year-to-date total per customer

Rules

Traps