'Get top 10 records from db and record from current log in user

I have table which consist of some data, just I want to show the top 10 ranks from that table and at the last column I want show the rank of the user who login to application.

Example

rank  | name |school

 1        aaa     JUU
 2        bbb      AL
 3        ccc      TN
 .       .....    ......

 8       xxx     KAR
 9       yyy     PUN
 10      zzz     KOL
 31      NNN      DEL 

At the last row I want to show the rank for user who logs into the application, e.g. here student "NNN" has logedin in application and his rank is 31.



Solution 1:[1]

You can use:

  SELECT * -- or whatever is your selection
    FROM mytable
   WHERE rank <= 10
   LIMIT 10
UNION
  SELECT *
    FROM mytable
   WHERE name = 'lll' -- Provided name is unique
ORDER BY rank;

Fiddle Demo

Solution 2:[2]

  1. select the top 10 on the rank
  2. union with the current user
  3. make use the current user is not already on the top 10...

select rank, name, school from ranks order by rank asc limit 10
union 
select rank, name, school from ranks where name = ?? 
     and name not in (select  name from ranks order by rank asc limit 10)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rachcha
Solution 2 SQL.injection