Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;

Execution time ~5ms.

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

share|improve this question
1  
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 at 3:05

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
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 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.

share|improve this answer
    
nvarchar(3)? Since in this case the longest possible one would be '100'? –  nhgrif Jul 12 at 21:31
2  
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 at 21:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.