Skip to content

Commit 3753fca

Browse files
Update Advanced SQL Puzzles Solutions.sql
1 parent e9f99d3 commit 3753fca

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Advanced SQL Puzzles/Advanced SQL Puzzles Solutions.sql

+15-4
Original file line numberDiff line numberDiff line change
@@ -1799,20 +1799,31 @@ Answer to Puzzle #39
17991799
Prime Numbers
18001800
*/----------------------------------------------------
18011801

1802-
DROP TABLE IF EXISTS #SampleData;
1802+
DROP TABLE IF EXISTS #PrimeNumbers;
18031803
GO
18041804

1805-
CREATE TABLE #SampleData
1805+
CREATE TABLE #PrimeNumbers
18061806
(
18071807
IntegerValue INTEGER PRIMARY KEY
18081808
);
18091809
GO
18101810

1811-
INSERT INTO #SampleData VALUES
1811+
INSERT INTO #PrimeNumbers VALUES
18121812
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
18131813
GO
18141814

1815-
--Answer coming soon!
1815+
WITH cte_Mod AS
1816+
(
1817+
SELECT a.IntegerValue, a.IntegerValue % b.IntegerValue AS Modulus
1818+
FROM #PrimeNumbers a INNER JOIN
1819+
#PrimeNumbers b ON a.IntegerValue >= b.IntegerValue
1820+
)
1821+
SELECT IntegerValue AS PrimeNumber
1822+
FROM cte_Mod
1823+
WHERE Modulus = 0
1824+
GROUP BY IntegerValue
1825+
HAVING COUNT(*) = 2;
1826+
GO
18161827

18171828
/*----------------------------------------------------
18181829
Answer to Puzzle #40

0 commit comments

Comments
 (0)