Counting distinct items within a category on SQL

count, mysql, php, sql

Solution

The `DISTINCT` is not needed since you are using `GROUP BY category`:

SELECT category, count(*) AS counter
FROM item_descr
GROUP BY category
ORDER BY counter DESC

Problem

I need to compose a SQL statement as follows: I have a table with many items, each item having a category. In total there are 3 categories. I need to select the DISTINCT categories and then order them by number of items within each category. Would this be a good way? Or too slow? ``` SELECT DISTINCT category, count(*) AS counter FROM item_descr GROUP BY category ORDER BY counter DESC ```

Original source