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    1

(Group by the name, and then sort them by their count)

This question and answers originated from www.stackoverflow.com
Question by (6/1/2011 5:29:37 PM)

Answer

SELECT name, COUNT(*) as cnt
FROM Fruits
GROUP BY name
ORDER by cnt DESC
Answer by

Find More Answers
Related Topics  mysql  database  query
Related Questions
  • 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 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 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 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 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 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…
  • MySQL: How to optimize this simple GROUP BY+ORDER BY query?

    I have one mysql table: CREATE TABLE IF NOT EXISTS `test` ( `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, `SenderId` int(10) unsigned NOT NULL, `ReceiverId` int(10) unsigned NOT NULL, `DateSent`…
  • How would I perform this MySQL Query?

    Suppose I have a table called "approvals". mysql> desc approval; +-------------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | …
  • Which indexes do I need to add to speed up this MYSQL JOIN/GROUP BY/HAVING query?

    The following query takes about 30 seconds to execute when current_vacature_response contains 88k records, and daily_vacature_response contains 10k records. Using EXPLAIN I've concluded that no inde…