 Hello, welcome to SSUnited Social Decide. And this is continuation of SQL Server interview questions and answers. So today we gonna discuss one more interview question that is print only prime numbers. So what is prime numbers? So those numbers which is going to divide by one or itself. So those numbers are prime numbers. So here as we can see we have 2, 3, 5, 7, 11. So these are the numbers which will be divided by one or itself. So we need to write a query to print the prime numbers between 1 to 15. So how we can write the query for printing the prime numbers between 1 to 15. So let's move to SQL Server Management Studio where we can write the query. So here I am using SQL 2017. You may have another version of SQL Server. So here, so first of all I need to declare a variable. So declare at the rate I and this should be integer type and this is 2. So initial value would be 2. Then we need to declare one more variable that would be checking for prime. So we can say this is prime and integer initial value should be 0. Next we need to declare our output table. So declare at the rate output and this is table type. And it would have only one column that would be number and it will be going to hold only prime numbers. Then our logic will be started. So here first of all we need to use a loop. So we can set up while loop if this at the rate I value is less than or equals to 50 because we want to print prime numbers between 1 to 15. If we want to print the prime numbers between 1 to 100 then we can replace this 50 by 100. Then begin and so between this begin and we need to declare one more variable to check about the prime numbers and we can call this as j and this would be integer type as well. And here we need to initialize the value of this at the rate I. So at the rate I minus 1 because it will be going to check 1 by 1. If we have 5 then initial value for this at the rate J would be 4. Then we need to check up for 4, 3, 2, 1 if that value will be divided between these numbers then that is not prime number. So next we can set our flag that is prime equals to 1. Now we need to use one more loop. So here we can set up for while at the rate J value is greater than 1 then begin then and and between this begin and we can write the logic. So logic would be very simple. Here we can check if at the rate I value modulus with the at the rate J value equals to 0. Then we can say that number is not a prime number. Then we can set the value for this at the rate prime variable would be 0. After this if we need to increase the value of this at the rate J variable will be at the rate J plus 1. After this if condition we need to decrease the value of this at the rate J variable would be minus 1. So at the rate J minus 1. After this inner while loop we need to check if at the rate prime equals to 1. It means that number is prime number. So we can use the begin then and and here we need to insert that value into the output table. So this is the output table and we can call this as values and here we can say at the rate I now looks good after this if we just need to increase the value of at the rate I variable so at the rate I plus 1 now looks good now we can select and we can check so select as it from our output now we can execute. So here we are getting the output like 2, 3, 5, 7, 11, 13, 17, 19, 23 so these are the values which we are expecting if we want to print prime numbers between 1 to 100 then 15 will be replaced by 100. Now here we can check we are getting the prime numbers between 1 to 100. So here this is very simple we have initialized a variable like at the rate I with the value of 2 then we have set up a flag for prime initial value is 0 then we have declared a output table then we have started a loop because here we have value with 2 and we want to print the prime numbers between 1 to 100 so that's why we have used this while loop after that here we have set up a at the rate J variable and initial value of this at the rate J variable will be at the rate I minus 1 because if we will have at the rate I value will be 2 then this at the rate J will be 1 and we can set flag that is at the rate prime will be 1 and here we have loop so first at the rate J will be greater than 1 so our condition will not be satisfied so it will be directly moved to if condition and here we can see it's a prime number so we can insert that value. Next time once this at the rate I value will be increased by 3 so it would be so 3 is less than 100 then go inside this and here we can check at the rate J value that would be 2 so we can set at the rate prime flag will be 1 then here we can see we have a while loop and this while loop will be going to satisfy the condition because at the rate J value is 2 then go inside at the rate I that is 3 so here 3 mod 2 that will not be going to 0 this if condition will be skipped and at the rate J value will be decreased by 1 next time that would be 1 so this while loop will not be executed because condition will not be satisfied and this at the rate I value will be increased so this at the rate I value will be increased by 4 so here this loop will be satisfied the condition so here at the rate J value will be 3 we can set flag prime for 1 then this loop will be executed because 4 is greater than 1 and here it will be going to check if mod of 4 with 3 so this will not be satisfied so at the rate J value will be decreased by 1 so next time that value will be 2 so here while condition it will be going to check the condition that is satisfied then at that I value will be 4 and at the rate J value will be 2 so this condition will be satisfied this time then at the rate prime value will be set to 0 so here just skip this loop and here we can see at the rate prime value that is not is equals to 1 so that value will not be inserted in the output and this loop will be executed until our condition will be satisfied so I hope you have understand how we can write the query to print the prime numbers so thank you so much for watching this video if you like this video please subscribe our channel to get many more videos don't forget to press the bell icon to get the notification of our newly uploaded videos if you need this script it will be available on the description of this video so you can copy from there thank you so much for watching