This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Question:
Write a SQL query to find first 100 Prime numbers in SQL Server.
Require Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
PrimeNumbers ------------ 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |
Solution:
1 2 3 4 5 6 7 8 9 10 |
SELECT DISTINCT Number AS PrimeNumbers FROM MASTER..SPT_VALUES AS a WHERE Number >= 2 AND Number <= 100 AND NOT EXISTS ( SELECT 1 FROM MASTER..SPT_VALUES AS b WHERE b.Number > 1 AND b.Number < a.Number AND a.Number % b.Number = 0 ) |