EXERCISE TOPIC▼
- Access exercises (91)
- C# exercises (79)
- Excel exercises (278)
- Power Apps exercises (13)
- Power Automate exercises (18)
- Power BI exercises (139)
- Power Platform exercises (157)
- Python exercises (28)
- Report Builder exercises (141)
- SQL exercises (198)
- SSAS exercises (51)
- SSIS exercises (46)
- SSRS exercises (99)
- VBA exercises (85)
- Visual Basic exercises (46)
SQL EXERCISES▼
SQL EXERCISES▼
- Simple Queries (4)
- Setting criteria using WHERE (5)
- Calculations (7)
- Calculations using dates (4)
- Basic joins (8)
- More exotic joins (2)
- Aggregation and grouping (8)
- Views (5)
- Subqueries (5)
- Stored procedures (5)
- Variables (8)
- Parameters and return values (11)
- Testing conditions (1)
- Looping (3)
- Scalar functions (6)
- Transactions (5)
- Creating tables (5)
- Temporary tables and table variables (9)
- Table-valued functions (6)
- Derived tables and CTEs (13)
- Dynamic SQL (4)
- Pivots (2)
- Triggers (2)
- Archived (70)
SQL | Looping exercise | Find first n primes using looping
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
Software ==> | SQL (198 exercises) |
Version ==> | Any version of SQL Server |
Topic ==> | Looping (3 exercises) |
Level ==> | Harder than average |
Subject ==> | SQL training |
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Your task is to write a query to show the first (say) 1000 primes. Here's a suggested algorithm:
- Create an integer variable to hold the value whose primeness you're testing
- Use WHILE to loop until this integer is 1000, incrementing its value by 1 each time round the loop (let's call this number P)
Within this outer loop:
- Find out the square root of the number you're testing (use SQRT)
- Find out what the highest integer is just below this (use FLOOR) - let's call this X
- Try dividing each number from 1 up to and including X into P
- If we ever find a number such that P % X = 0 (ie that P leaves a remainder of 0 when divided by X) we have a non-prime, and we should flag this accordingly and break out of the loop
If it helps, here's what you should see to start with:

The first few primes!
If you get this working, try incorporating a timestamp to see how long it took to run:

The start of the time test - at the end, set @EndTime and use DateDiff(ms, @StartTime, @EndTime) to work out the difference in milliseconds
Save your hard work as Printing primes.sql, then close it down!
Holy ****!
That was awesome!
It took me waaaaay more time than I care to admit, but I got it.
I had to exclude 2 and 3 because of the use of FLOOR(SQRT(@P)) though.
There it is:
-------------------------------------------------
-- A little script to see how long it takes:
DECLARE @StartTime DATETIME
DECLARE @EndTime DATETIME
-- This is the start point. At the end we'll set the end point and calculate it
SET @StartTime = CURRENT_TIMESTAMP
-------------------------------------------------
/* Printing all prime numbers from 1 to 1,000 */
DECLARE @P INT --A variable to hold the number in check
DECLARE @counter INT -- A variable to hold the running numbers from 1 to @SQP
DECLARE @SQP INT -- the square root of @P (without the leftover)
SET @P = 1
SET @counter = 1
WHILE @P <= 1000 --As long as @P <= 1000, do the following:
BEGIN
/* 1 */ SET @SQP = FLOOR(SQRT(@P))
/* 2 */ IF @P = 1
BEGIN
PRINT '1 is a special number. it''s neither Prime nor Composite Number'
END
/* 3 */ IF @P IN (2,3)
BEGIN
PRINT @P
END
ELSE -- Now we're REALLY starting to check the number in @P
BEGIN
WHILE @counter <= @SQP
BEGIN
/* 1 */ IF @P % @counter = 0
BEGIN
/* PRINT 'Not Prime' */
BREAK -- We can stop checking
END
ELSE
BEGIN
IF @counter = @SQP --That means it's a Prime
BEGIN
PRINT @P
BREAK -- We can stop checking
END
END
/* 2 */ IF @counter = @SQP
BEGIN
PRINT @P
BREAK -- We can stop checking
END
ELSE
BEGIN
SET @counter = @counter+1
END
END
END
/* 4 */BEGIN
SET @P = @P+1
SET @counter = 2
END
END
----------------------------------------
-- Calculating how long it took:
SET @EndTime = CURRENT_TIMESTAMP
PRINT ''
PRINT 'This process took '+ CAST(DateDiff(ms, @StartTime, @EndTime) AS VARCHAR) + ' milliseconds'
----------------------------------------