Problems Linking the max value of an attribute in a table to its primary Key I need to extract the order_id with the max number of items, I've figured out how to extract the max value but  I'm having problems linking it to the particular order_id. 

PS: I'm a uni student so please go easy on me.

select MAX(f), x.order_id 
from ( select count( distinct z.item_id) f, z.order_id as g 
       from  Order_item z, Item i, Ordert o
       where o.order_id=z.order_id 
       group by z.order_id
      ) AS T, Ordert x
having x.order_id=g;
Maybe you need simple

select count( distinct z.item_id) f, z.order_id as g 
from  Order_item z, /* Item i, */ Ordert o
where o.order_id=z.order_id 
group by z.order_id
order by f desc limit 1


?