FizzBuzz in SQLite 2 This query performs a FizzBuzz in SQLite 2.8.17 without creating any tables or sprocs.

First attempt:

select case when fb &lt;&gt; '' then fb else n end from (
    select g + f * 2 + e * 4 + d * 8 
        + c * 16 + b * 32 + a * 64 as n,
    coalesce(fizz, '') || coalesce(buzz, '') as fb 
    from
    (select 0 union select 1 as a),
    (select 0 union select 1 as b),
    (select 0 union select 1 as c),
    (select 0 union select 1 as d),
    (select 0 union select 1 as e),
    (select 0 union select 1 as f),
    (select 0 union select 1 as g)
    left join (select 'Fizz' as fizz, 3 as fizzstep)
        on n % fizzstep = 0 
    left join (select 'Buzz' as buzz, 5 as buzzstep)
        on n % buzzstep = 0
    where n between 1 and 100
);


Second attempt:

select case
    when n % 3 = 0 and n % 5 = 0 then 'FizzBuzz'
    when n % 3 = 0 then 'Fizz'
    when n % 5 = 0 then 'Buzz'
    else n
end from (
    select g + f * 2 + e * 4 + d * 8 + 
        c * 16 + b * 32 + a * 64 as n from
    (select 0 union select 1 as a),
    (select 0 union select 1 as b),
    (select 0 union select 1 as c),
    (select 0 union select 1 as d),
    (select 0 union select 1 as e),
    (select 0 union select 1 as f),
    (select 0 union select 1 as g)
    where n between 1 and 100
);


Can it be improved, either by:


making it shorter / less redundant, or 
making it significantly faster, or
making it more portable so it also works in another RDBMS (including SQLite 3)?


Note that CTEs are not available until later versions of SQLite. Also note that this won't work as written on sqlfiddle.com; they're using SQLite 3.
I will have to admit I personally prefer the First attempt, but both seem like they would run fast and deliver as expected. 

The only piece I don't like so much, which is found in both attempts, is this:


    select g + f * 2 + e * 4 + d * 8 
    + c * 16 + b * 32 + a * 64 as n



I think this could really use some parentheses for clarity, if nothing else. 

    select (
    g + (f * 2) + (e * 4) + (d * 8) 
    + (c * 16) + (b * 32) + (a * 64) 
    ) as n