I have the following MYSQL query:
SELECT *,
(
SELECT SUM(points)
FROM point
WHERE employeeId=employee.id
) as totalPoints
FROM employee
ORDER BY totalPoints DESC
As you can see is a simple list by the number of points of each worker, the goal is to form a simple ranking by score, the problem is that there are many workers with the same score (example 10) then when making the query the position starts to vary. For example, I get this list:
nombre | apellido | puntaje
--------------------------------
Juan | Perez | 120
Pepe | Suarez | 100
Ricardo | Cruzado | 80
Carlos | Medina | 80
Jesus | Segura | 80
Alberto | Veocinco | 50
Guillermo | Cabrejos | 20
Antonio | Pereyda | 10
Now focus on the 3 workers who have score 80, every time I call the query Mr. Ricardo crossed for example that is before Mr. Carlos Medina can appear below it or at the end and the positions of the same vary every time I call the consultation, for example I get the following:
nombre | apellido | puntaje
--------------------------------
Juan | Perez | 120
Pepe | Suarez | 100
Jesus | Segura | 80
Carlos | Medina | 80
Ricardo | Cruzado | 80
Alberto | Veocinco | 50
Guillermo | Cabrejos | 20
Antonio | Pereyda | 10
and if I call again I get:
nombre | apellido | puntaje
--------------------------------
Juan | Perez | 120
Pepe | Suarez | 100
Jesus | Segura | 80
Ricardo | Cruzado | 80
Carlos | Medina | 80
Alberto | Veocinco | 50
Guillermo | Cabrejos | 20
Antonio | Pereyda | 10
And so with all workers who have the same score.
I would like to know if there is a better way to apply the query. Thanks in advance!