tags:

views:

55

answers:

2

Hi guys I have two tables as such:

Categories: ID - Name - Desc

Items ID - Name - CategoryID - Desc - Price

I want a query that returns a list of categories ranked by most occurences in the items table

+6  A: 

This should do the trick:

SELECT c.ID, c.Name, count(i.ID)
FROM Categories c
LEFT JOIN Items i on (c.ID=i.CategoryID)
GROUP BY c.ID
ORDER BY count(i.ID)
Cassy
A: 
SELECT 
  CategoryID, count(*)
FROM 
  items
GROUP BY 
  CategoryID
ORDER BY 
  2 DESC

You can then join to categories to get their names.

Michal Dymel