title: SQL Group By and min (MySQL)
body: <p>I have the following SQL:</p>
  
  <pre><code>select code, distance from places;    
  </code></pre>
  
  <p>The output is below:</p>
  
  <pre><code>CODE    DISTANCE            LOCATION
  106     386.895834130068    New York, NY
  80      2116.6747774121     Washington, DC
  80      2117.61925131453    Alexandria, VA
  106     2563.46708627407    Charlotte, NC
  </code></pre>
  
  <p>I want to be able to just get a single code and the closest distance. So I want it to return this:</p>
  
  <pre><code>CODE    DISTANCE            LOCATION
  106     386.895834130068    New York, NY
  80      2116.6747774121     Washington, DC
  </code></pre>
  
  <p>I originally had something like this:</p>
  
  <pre><code>SELECT code, min(distance), location
  GROUP BY code
  HAVING distance &gt; 0 
  ORDER BY distance ASC
  </code></pre>
  
  <p>The min worked fine if I didn't want to get the correct location that was associated with the least distance. How do I get the min(distance) and the correct location (depending on the ordering on the inserts in the table, sometimes you could end up with the New York distance but the Charlotte in Location).</p>