1

I have a table of data like so

---------------------
Name | Image | Points
---------------------
Bob  | a.jpg | 100
Bob  | b.jpg | 56
Sal  | c.jpg | 87
Jim  | d.jpg | 34
Bet  | e.jpg | 23
Bren | f.jpg | 5
Bren | g.jpg | 15
Test | h.jpg | 10

What i want to do is select 5 rows with the highest "Points" but dont take duplicate "Names" into account.

Just selecting the highest 5 would return


Name | Image | Points
---------------------
Bob  | a.jpg | 100
Sal  | c.jpg | 87
Bob  | b.jpg | 56
Jim  | d.jpg | 34
Bet  | e.jpg | 23

But i do not want to include duplicate rows, therefore the second Bob in the above would be removed and "Bren | g.jpg | 15" would be added instead

Any help is appreciated thank you

2 Answers2

0

You can use NOT EXISTS() :

SELECT * FROM YourTable t
WHERE NOT EXISTS(SELECT 1 FROM YourTable s
                 WHERE t.name = s.name AND s.points > t.points)
ORDER BY t.points DESC
LIMIT 5
sagi
  • 40,026
  • 6
  • 59
  • 84
0

Or I think simpler/tidier and without need for subquery is to use group by to avoid duplicates

SELECT * FROM YourTable t
  GROUP BY Name
  ORDER BY t.points DESC
LIMIT 5
Dave P
  • 178
  • 2
  • 9