 Hello, welcome to SSUnitech. So, see this site and this is continuation of SQL Server interview questions and answers video series. So, today we have one more interview question, how we can write the query to get the alternate order by results of male and female data. So, what is the actually this question? So, move to next slide. As we can see, we have input table and it contains three columns. First is ID, then name, then gender. In our output we are also expecting all these columns. As we can see for ID 1, this is monies and gender is male. In ID 2, we have sake and gender is male as well. But in ID 6, we have swangi and female. But in our output, we want first male, then female, then again male, then female. So, we want output alternate order of male and female. So, as we can see for our first row in our output, we have male data. Then in our second row, we are having female, then again male, then female. So, we want data by alternate. So, it seems to be very easy but actually it's not. So, go to our SQL Server management studio to write the query for getting this output. So, as I am using SQL 2017, you may have other versions of SQL Server but query would be same for all the versions. As I have already written this script to create the table and insert record on this table. So, let me execute this query. As we can see, nine rows affected. So, data have been inserted successfully in this table. Let me check the data. So, we can use select a stick from our table. Now, I want to select this table. As we can see, we are having nine rows and this metadata and in our slides metadata have same. So, here how we can achieve the output. So, I want to use a row number. So, we can use a row number. Then we need to specify over clause which is syntax for row number. Then I want to do partition by with gender. So, we can write gender. Then I want to use order by clause with the gender. That is fine. Now, we can execute this query and we can see we are getting numbers 1, 2, 3, 4, then 1, 2, 3, 4, 5. Looks good. Now, we can put an alias name with rw. That is fine and we can also check. So, we are getting the data. So, I want to put this selected statement in a drive table. Then I can also use select a stick from this table. That looks good. But in our output, we don't want row number. So, we can use id then name and then gender. Now, we can execute this selected statement. So, that is fine. So, here we need to use order by. So, let me use order by with row number. That is also fine. But here, we can see we are getting the gender female first, then male, then male, then female, female, then male, male, female, male. So, this is not the correct output as we are expecting. So, how we can get the correct output? So, for that we can also use gender in order by clause and with this decreasing order. So, let me execute our selected statement. So, as we can see, we are getting the output as we were expected. First male, then female, then male, female. So, we are getting all the data with alternate genders. If you need this script, it will be available on my Facebook page. You can copy from there. 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. Thank you so much.