Skip to content

Instantly share code, notes, and snippets.

@Animesh-Ghosh
Created December 25, 2024 18:47
Show Gist options
  • Select an option

  • Save Animesh-Ghosh/eab3d46ebadacc871f1a5674b58c9f6d to your computer and use it in GitHub Desktop.

Select an option

Save Animesh-Ghosh/eab3d46ebadacc871f1a5674b58c9f6d to your computer and use it in GitHub Desktop.
Christmas Tree in SQL
--- original version found on @iavins Tweet: https://x.com/iavins/status/1871914543590289672
WITH RECURSIVE numbers(n) AS (
SELECT 1
UNION ALL
SELECT n + 1 FROM numbers WHERE n < 9
), tree_parts(level, stars, spaces) AS (
SELECT
n,
REPEAT('*', n * 2 - 1),
REPEAT(' ', 20 - n)
FROM numbers
), trunk(level, stars, spaces) AS (
SELECT 10, '|||', REPEAT(' ', 18)
UNION ALL
SELECT 11, '|||', REPEAT(' ', 18)
)
SELECT level, spaces || stars AS tree FROM tree_parts
UNION ALL
SELECT level, spaces || stars FROM trunk;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment