I have two tables, username and score. Both are connected using user_id.
I want to select the top 5 usernames who have the highest score. I am trying following query but it is not working:
SELECT `user_name`
FROM `username`
WHERE `user_id` = ( SELECT `u_id`
FROM `score`
ORDER BY `high_score` DESC
LIMIT 5 )
I get this error when I run the above query: #1242 - Subquery returns more than 1 row
In your
WHEREclause your are trying to assert the congruence or equality of one value (on the left side) to a list of values (on the right side).Use the
INoperator to achieve this because it will compare the left value with any of the right values.The following is your corrected code.
As a manner of style using a join is clearer and more elegant especially for a simple query like this.