Greetings. There was such a task: to bring the first 3 users, whose rating is the highest and the position of the user in this sorted list, that is, there is a user Вася and his рейтинг = 3000 , there are also users Коля , Дима , Петя , whose ratings are respectively 10 000 , 8 200 and 6000 .
The first 3 users with high ratings can be obtained as follows:
SELECT user_id FROM users ORDER BY rating DESC LIMIT 3 It turns out like this:
1. Коля 2. Дима 3. Петя ... n. Вася The question is: is it possible to somehow display the first 3 users and the position n the user himself, with respect to their rating, using a single query?
There were solutions using a loop and a counter, but it seems to me that this is not the best option or create a separate table. What do you think?