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 select statement would return:

apple, 3
orange, 2
banana, 1
This question and answers originated from www.stackoverflow.com
Question by (4/13/2010 10:32:49 PM)

Answer

select fruits, count(fruits)
from table
group by fruits
order by count(fruits) desc
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 MySQL query?

    I am working on an Asset DB using a lamp stack. In this example consider the following 5 tables: asset, server, laptop, desktop, software All tables have a primary key of id, which is a uni…
  • 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?
  • 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 write this MySQL Query? (datetime)

    Suppose I have a DateTime field called "time_before" I want to insert a datetime that is 1 hour before the now() time. INSERT INTO mytable(time_before) VALUES(now()-3600 seconds)...something l…
  • how i group do this mysql query

    i want to make charts system and i think it must be like that 1 jan 2009 = 10 post 2 jan 2009 = 2 post 4 jan 2009 = 10 post 6 jan 2009 = 60 post and i have posts table that has id,user_id,dat…
  • 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 do I use MySQL Group By in this instance?

    I have tables containing the following fields among others in a application for a transportation company... jempe_users +------------------------------+ | Field | +-----------------…
  • 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…