TIMEX
TIMEX

Reputation: 271824

How do I execute this query in MYSQL?

Suppose I have a column with words:

orange
grape
orange
orange
apple
orange
grape
banana

How do I execute a query to get the top 10 words, as well as their count?

Upvotes: 3

Views: 163

Answers (2)

Alnitak
Alnitak

Reputation: 339816

SELECT   word, COUNT(*) AS n
FROM     `table`
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10

Upvotes: 3

Peter Lang
Peter Lang

Reputation: 55524

SELECT word, COUNT(*) word_cnt
FROM your_table
GROUP BY word
ORDER BY COUNT(*) DESC
LIMIT 10

The GROUP BY groups by values of word, the ORDER BY COUNT(*) DESC gets you the rows with highest count first, and the LIMIT 10 returns the first 10 rows only.

Upvotes: 3

Related Questions