Reputation: 6054
So the Table structure is this
CD id | Artist id
I want to return the artist who has the most CDs, in this case the artist column value that repeats most.
I'm using Mysql so what is the best SQL query to do this?
Upvotes: 0
Views: 138
Reputation: 30993
I'dont use MySQL I translate a similar TSQL and it becomes like this:
SELECT ArtistId, count(CDId) AS CDCount FROM Table
GROUP BY ArtistId ORDER BY CdCount DESC LIMIT 1
I hope can help you. Btw: I remove column spaces in my example ok?
Upvotes: 1
Reputation: 165065
SELECT `Artist id`, COUNT(`CD id`) AS `cd_count`
FROM `Table`
GROUP BY `Artist id`
ORDER BY `cd_count` DESC
LIMIT 1
Upvotes: 6