Para tu posicion "SELECT count(*) as miranking FROM table WHERE (nivel > minivel) OR (nivel = minivel AND id <= mi_id)"