• Thank You very much @SScrazy for providing the solution. i am very new to the sql server programming and have started learning recently. could you please help me understand the logic of script that you provided and how it works?

    Actually this question was asked to me in an interview