Skip to content

Instantly share code, notes, and snippets.

@Eunoia1729
Last active December 30, 2021 09:52
Show Gist options
  • Save Eunoia1729/7a1cc45d917b663cdd7793066cd5a4d7 to your computer and use it in GitHub Desktop.
Save Eunoia1729/7a1cc45d917b663cdd7793066cd5a4d7 to your computer and use it in GitHub Desktop.
Generate prime numbers in SQL till 1000
WITH
RECURSIVE numbers(N) AS (
SELECT 2
UNION ALL
SELECT N + 1 FROM numbers
WHERE N < 1000
)
SELECT F1.N
FROM numbers F1
JOIN numbers F2 ON (F1.N % F2.N) = 0
GROUP BY F1.N
HAVING COUNT(F2.N) = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment