title: SQL Order by with group by clause
body: <p>I have one stock table which contains the foreign key of product_id, company_id, size_id and price.
  I need to fetch total companies, product name (ascending order), price and volume (for least price only)  - for every product.</p>
  
  <p>I tried,</p>
  
  <pre><code>SELECT pr.product_name, 
         COUNT(DISTINCT stock.company_id) AS total_companies, 
         sz.size_name, 
         stock.price 
  FROM stock 
  JOIN product pr ON pr.id = stock.product_id 
  JOIN size sz ON sz.id = stock.size_id 
  GROUP BY stock.product_id 
  ORDER BY pr.product_name ASC;
  </code></pre>
  
  <p>So far, here the size and price are random for every product. If I put MIN(stock.price) in select query then price is correct but then associated size is wrong. If I put condition to select only products with min price, then company count is reduced to 1.</p>
  
  <p>Any help or pointers are much appreciated.</p>