FizzBuzz in SQL--no loops

MS SQL Server, no loops. ;)

WITH Numbers AS (
SELECT 1 as Number
UNION ALL
SELECT Number+1 FROM Numbers WHERE Number < 100
)
SELECT CASE
WHEN Number % 15 = 0 THEN 'FizzBuzz'
WHEN Number % 5 = 0 THEN 'Buzz'
WHEN Number % 3 = 0 THEN 'Fizz'
ELSE CAST(Number as varchar)
END AS FizzBuzz
FROM Numbers;


With performance being the most important concern, could this be made better?

-
A note about compatibility: this also works as written in SQLite 3. For PostgreSQL compatibility, you need WITH RECURSIVE (I believe this is standard), but SQL Server doesn't seem to like the RECURSIVE keyword. SQLite 3 doesn't care either way. Oracle has its own weird syntax involving START and CONNECT keywords. MySQL (still) doesn't support CTEs. –  Dagg Jul 16 '14 at 3:05

Taking the suggestion by @usr to avoid recursive CTEs, I came up with the following formulation:

WITH R4 AS (
SELECT col FROM (VALUES (1), (2), (3), (4)) x(col)
), R16 AS (
SELECT a.col
FROM R4 AS a CROSS JOIN R4 AS b
), R100 AS (
SELECT TOP 100 ROW_NUMBER() OVER (ORDER BY a.col) AS n
FROM R16 AS a CROSS JOIN R16 AS b
)
SELECT CASE
WHEN n % 15 = 0 THEN 'FizzBuzz'
WHEN n %  3 = 0 THEN 'Fizz'
WHEN n %  5 = 0 THEN 'Buzz'
ELSE CAST(n AS NVARCHAR(8))
END AS FizzBuzz
FROM R100
ORDER BY n;


I've found that SQL Fiddle is a poor benchmarking platform, as execution times there are completely erratic. Since you want to run this on MS SQL Server, though, we can use Stack Exchange Data Explorer. ☺

Here's your original query and the my non-recursive formulation. Both take 3 ms on Stack Exchange Data Explorer when the results are not cached.

-
Use ORDER BY (SELECT NULL) to loose a sort operator from the plan. Even faster. Your case statement mixes Unicode and non-Unicode. Make the string literals N''. –  usr Jul 13 '14 at 10:52

Recursive CTEs in SQL Server execute involving a temp table. This is probably why this tiny amount of work even takes a measurable amount of time. This should take <= 1ms.

Use a numbers table. Or, use one of the many tricks to materialize a sequence of numbers without table access such as:

SELECT Num
FROM (VALUES (1), (2), ...) x(Num)


Using Number % 15 = 0 instead of Number % 3 = 0 AND Number % 5 = 0 is less clear. It requires mathematical insight to convince anyone that this is even correct. Code should be obviously correct.

The default length of varchar is unclear. (Can you tell from memory?!) Better use nvarchar(400). By default, use Unicode characters in order to just never have certain bugs and problems.

These issues aside this query is simple. Anyone can very quickly understand it and conclude that it is correct. That's good code.

-
nvarchar(3)? Since in this case the longest possible one would be '100'? –  nhgrif Jul 12 '14 at 21:31
Why complicate unnecessarily? It's just brittle and redundant. There is no value in choosing a shorter length. A varchar(N) does not take N bytes of storage. Neither on disk nor in memory. It only consumes used space. –  usr Jul 12 '14 at 21:32