Skip to content

Instantly share code, notes, and snippets.

@cihangir
Created November 19, 2015 02:57
Show Gist options
  • Save cihangir/407509f0c5ef446fce42 to your computer and use it in GitHub Desktop.
Save cihangir/407509f0c5ef446fce42 to your computer and use it in GitHub Desktop.
SQL FizzBuzz
WITH RECURSIVE numbers(n) AS (
SELECT 1
UNION ALL
SELECT n + 1
FROM numbers
WHERE n < 102
) 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
to_char(n, '999')
END as result
FROM
numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment