Thanks for the replies guys. I think TamilSelvan's suggestions has been quite useful to improve the performance. Can we use the RANK function anyway to get the same results? Would this improve the performance?