[prog] newbie SQL query assistance needed

Chris Wilson chris+linuxchix at aptivate.org
Tue Apr 27 20:17:58 UTC 2010


On Tue, 27 Apr 2010, Chris Wilson wrote:
> On Tue, 27 Apr 2010, Tina wrote:
> 
> > I have an orders table. In it are all the orders (orderid) with each item
> > ordered (itemid) and quantity (quantity).
> > 
> > I need to figure out which item was ordered the most times. I know it
> > involves a multi-row query, but I just can't seem to figure it out.
> 
> select itemid, count(1) from orders group by itemid;

Actually, sorry, I just realised that you only want the first one. To do 
that, use:

  select itemid from orders group by itemid order by count(1) desc
  limit 1;

Cheers, Chris.
-- 
Aptivate | http://www.aptivate.org | Phone: +44 1223 760887
The Humanitarian Centre, Fenner's, Gresham Road, Cambridge CB1 2ES

Aptivate is a not-for-profit company registered in England and Wales
with company number 04980791.


More information about the Programming mailing list