 Hello, welcome to SSUnitech. So, see this side and this is continuation of SQL Server interview questions and answers. So, today we are going to discuss few more interview questions. So, here how we can write the query to print the triangle pattern. So, what is this question? Let's move to next slide to understand about the question. So, here as we can see we have four different images and these images are going to print their stars in triangles. So, in the first image we can see this is going to print triangle like this, then second, then third and then fourth. So, how we can write the query to print these images like as it is. So, let's move to SQL Server management studio where we can write the query and we can get the output. So, here I am using SQL 2017. You may have another version of SQL Server. So, first of all I need to declare a variable and that would have to reslide the value with 1. Then we need to use while loop. So, while if this address i value is less or equals to 5, this loop will be executed. Now, we can use the begin and end and between this we need to write the query. So, this is very simple that we can write print and here we can use a replicate function. So, replicate and what we want to replicate, we just want to replicate this star value and how many times we want to replicate, we want to replicate address i. Then we need to increase this address i value. So, address i is equals to address i plus 1. Now, we can execute and we can check the output. So, here we are getting the output as we are expecting. So, here first, second, third we are getting values like this but as we can see we are getting two spaces. So, how we can remove these two spaces? For that we can directly use our dream function. So, space will be removed. Now we can execute and now we are getting the output without any space. Now it looks good and we are getting the output as we are expecting. Move to next image. In the next image we just want to print this value. So, now we have tunnel like this 5, 4, 3, 2 and 1. So, now we can write the query and we can get the output. So, here let me comment this and again we need to declare a variable. So, declare address i and initialize the value with 5. In the while loop we can use this address i if this address i value greater or equals to 1 then this while loop will be executed and we can use begin and as here what we want to do? We just want to print and here we can use the replicate function. What we want to replicate? We want to replicate this star with two blank spaces comma how many times we just want to replicate with i times. Now we can set the value of this at the rate i variable will at the rate i minus 1. Now we can execute and we are getting the output as we are expecting but again we can see we are getting two blank spaces at the right side. So, what we need to do? We can use the R trim function to remove this blank spaces. So, here now we can execute and we are getting the output without any blank spaces. So, this is for second. Now move to third image. So, in the third image we need to print the values but here as we can see it will be going to start from left side to right side. So, how we can write the query to print this triangle. So, move to Sikosovo and here I need to declare two variables this time. So, at the rate i and initialize the value with 5 and in the second variable that is a and set the value is 5 and here we can use the while loop while this at the rate i value is greater than 0 then this while loop will be executed and here begin then end and here we need to print. So, what we need to print? First of all we need to use replicate function. So, in replicate function we can see we are getting blank spaces. So, we just need to put two blank spaces comma how many times we want at the rate j minus at the rate i. Now again we need to use the replicate. So, replicate and this time we just want to replicate will star comma how many times we want to replicate we want at the rate i times. Now we can set at the rate i value will be at the rate i minus 1. Now we can execute and we can check the output. So, we are getting the output as we are expecting. So, this is image 3 but here again we can see we are having a blank space. So, how we can remove this blank space? So, this is very simple we just want to get the values from right side. So, we can use right and let me copy this whole code and put comma and again we can use the length of this minus 1. Now execute and now we are getting the output without any space. Now looks good and this is image 3 move to last image. So, this is the last image. So, how we can write the query and we can print this image. So, this is again very simple move to Sikoso and here then again we need to declare two variables. Let me call the first variable with at the rate i and set the value as 1 and in the second variable that is at the rate j and that would be 5. Then we need to use the while loop. So, while if this at the rate i value is less than or equals to 5 then our loop will be executed. So, we can use begin then and and inside this begin and we need to print the values. So, we can use the print and then we can use replicate and what we want to replicate? First of all we just want to replicate the blank space. So, we have two blank spaces. Now how many times we want? So, we just want this j times. Now we can use plus again we need to use a replicate function. So, replicate and here we just want to replicate our space with star and how many times we want? We just want at the rate i times. Now it looks good and here we can set the value of this i that will be i plus 1 and again we need to set the value for at the rate j and this at the rate j value should be at the rate j minus 1. Now it is good and we can check the output. So, here we are getting the output but as we can see we are getting 1, 2 blank spaces and 3 blank spaces. So, how we can remove these 3 blank spaces? So, for that again we can get the values from right side and let me copy this whole code and put comma. We can use the length function and this is the length and now minus 3. Now we can execute. Now we are getting the output without any blank space. If we can check we do not have any blank space right now. 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.