 Hello friends, welcome to SSUnited. My name is Susheel Singh and this is continuation of SQL Server interview questions and answers. So today I gonna discuss one more question. How we can switch any column values from bottom to top by using SQL Server joins? So what it means let's move to another slide where we will discuss about the question. This is our slide as you can see we have an input table and it contains two columns. First is ID and second column is name. In ID column we have 1, 2, 3, 4, 5 data in a sequence and incremented by 1. So here we can see in our output we have ID and we have the reverse name. Like if ID is 1 then go to the bottom in this table and we can see the name this is F. So it would come first then our second then the second last value is E. So we want to get the values in name from bottom to top. So how we can achieve this biogic SQL Server query? So let's move to SQL Server Management Studio where we will discuss how we can get the output. So here we can see I have already typed the query so it would save time. So I wanna create the tables. I am going to execute this create statement. The table has been created successfully. Now I wanna insert some recall on it. So here 6 was affected and now we can check by using select start from table. So here we can see we have the same metadata as I have displayed in our slide. So first of all we need to copy this output and go to Excel to understand the logic how we can apply logic and get the output. I wanna apply self join so I have to paste two times. So here we can see if we have one more additional column over here which is reverse ID and this ID is started from the bottom. It's 6 so it's 6 and then 5 then 4 3 2 and 1. Let's assume this is our A table and this is our B table. So when we apply inner join between these two tables on the behalf of ID and reverse ID and when the ID and reverse ID value will be matched then we want to get the name from B table. So our first is ID 1. When ID value will be matched then we want to get name from B table which is F. So it will written F. For next time we can see ID is 2 and when we are going to match with our ID then we can see it's going to written E. So it's going to written E then D then C then B and then A. And we can see in our slide here we want FED CBA and here we can get the output by using this method. So let's move to SQL Server Management Studio where we will write the query. So first of all we need to create a reverse ID. So we have to use row number function. So this is our row number and we have to specify over clause and then order by this ID and we are going to get from bottom. So we have to use decreasing and I am going to provide an alias name which are ID. So reverse ID and now I want to execute and here we can see this is our ID and this is our reverse ID and I am going to put order by 1 on the behalf of ID. Now I want to execute and here we can see ID and we are getting reverse ID is 6. It's going to start from bottom 654321. So now our B table has been created successfully. So now put this B table in a derived table and provide an alias name is B. And then we want to apply join from this over which table column values listed here and provide an alias name is A. Then specify inner join and then on the behalf of ID from table 1 and reverse ID from table B. So it's our reverse ID. When these values will be matched then we want to get ID from table 1 and name from table B. So this is our table B and now I want to execute this query. So here we can see we are getting same output as I have displayed in our slide. So this output and this output is same. So I hope you have understand how we can get this output by using SQL Server query. If you like this video please subscribe our channel for many more videos and press bell icon to get all the notifications for new videos. If you need this script it will be available on my Facebook page you can compile from there. Thank you so much for watching.