# 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.

The answer to the exercise will be included and explained if you attend the course listed below!

 Category ==> SQL  (192 exercises) Topic ==> Looping  (3 exercises) Level ==> Harder than average Course ==> Advanced SQL

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