CTEs can …
aid readability of a query be used as an alternative to derived tables (aka nested subqueries) be referenced multiple times be used for recursive queries Here are some use cases.
Filtering or Grouping on Windowing Function Here are the top two cities by population for each state in the table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 DECLARE @CityList TABLE ( City VARCHAR(30) NOT NULL, StateCode CHAR(2) NOT NULL, [Population] INT NOT NULL ); INSERT INTO @CityList (City, StateCode, [Population]) VALUES ('Portland', 'ME', 66363), ('Lewiston', 'ME', 36491), ('Bangor', 'ME', 33011), ('South Portland', 'ME', 25068), ('Manchester', 'NH', 109830), ('Nashua', 'NH', 86704), ('Concord', 'NH', 42733), ('Derry', 'NH', 33109), ('Dover', 'NH', 30168), ('Rochester', 'NH', 29935), ('Merrimack', 'NH', 25494), ('Boston', 'MA', 625087), ('Worcester', 'MA', 181631), ('Springfield', 'MA', 153155), ('Lowell', 'MA', 107584), ('Cambridge', 'MA', 106038), ('New Bedford', 'MA', 95183), ('Brockton', 'MA', 94316), ('Quincy', 'MA', 92909); WITH CityRank AS ( SELECT ROW_NUMBER() OVER(PARTITION BY StateCode ORDER BY [Population] DESC) AS PopulationOrder, City, StateCode, [Population] FROM @CityList ) SELECT City, StateCode, [Population] FROM CityRank WHERE PopulationOrder <= 2 ORDER BY StateCode, PopulationOrder; Nested Aggregate Functions Here it’s using the same numbers as Population above but let’s call it TotalSales for this example, and we will calculate the average sales for a state.