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?

This question and answers originated from www.stackoverflow.com
Question by (3/15/2010 7:28:53 AM)

Answer

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.

Answer by

Find More Answers
Related Topics  mysql  database  query
Related Questions
  • How do I execute this GROUP BY mysql query?

    Suppose I have a table called "Fruits" with a column called "name". name -------- apple orange orange orange apple grape How can I execute a query to produce this: orange 3 apple 2 grape…
  • How do I do this query in MySQL? (datetime)

    Suppose I have a datetime column in MySQL. How do I select all that have a datetime within 2500 seconds of the current datetime? SELECT ALL where current_datetime - that_datetime < 2500 ...
  • How do I write this GROUP BY query in MYSQL?

    Suppose I have a column called "fruits" I want to select all of the top fruits, ranked by fruits (and group by + count). Fruits: orange orange apple banana apple apple In this case, the selec…
  • How do I modify this query in MYSQL?

    SELECT title, title REGEXP 'apple' as is_fruit FROM mytable; TO: SELECT title, title REGEXP 'apple' or orange...or grapes...as is_fruit... Basically, how do I do an "OR" for REGEXP?
  • How do I run this MySQL JOIN query?

    Let's say I have 2 tables. The first table is a list of personas. A user can have many personas. mysql> select id, user_id, name from personas_personas; +----+---------+--------------+ | id…
  • How can I optimize this confusingly slow query in MySQL?

    I have a table of blog posts, each with a foreign key back to it's author. There are < 15,000 entries in this table. This query scans over 19,000 rows (per EXPLAIN ), requires a filesort (that mi…
  • How do I get PHP variables from this MySQL query?

    I am working on an Asset Database problem using PHP / MySQL. In this script I would like to search my assets by an asset id and have it return all related fields. First I query the database as…
  • How would I perform this MySQL Query?

    Suppose I have a table called "approvals". mysql> desc approval; +-------------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | …
  • How do I difference time in MySQL query?

    There's a datetime field "time" and it's in Y-m-d H:i:s format. All the values are past values. I need to query that field diffed from current time resulting in seconds. SELECT time FROM table
  • How do I query "begin with" in MySQL?

    SELECT stuff REGEXP 'itunes' as is_itunes; In this MySQL query, if "stuff" has the word "itunes" in it, it will mark it as itunes. However, I want to say "begin with". How can I check for "be…