title: mysql - group by to get oldest and newest dates into one row
body: <p>I have tbl_events</p>
  
  <pre><code>key  userID    date
  
  1     1         1.1.2000
  2     1         1.1.2017
  3     2         2.2.1990
  </code></pre>
  
  <p>I'm trying to get this:</p>
  
  <pre><code>select distinct userID, date [that is earliest eg row 1] as earliest_date, 
  date [that is latest eg row 2] as latest_date
  from tbl_events
  </code></pre>
  
  <p>if a userid has only one row in tbl_events then tbl_events.date would serve as both the earliest and latest_date</p>
  
  <p>ie my result would be:</p>
  
  <pre><code>userID    earliest_date    latest_date
  
  1         1.1.2000         1.1.2017
  2         2.2.1990         2.2.1990
  </code></pre>
  
  <p>I can do this rather inefficiently with lots of looping but I'm wondering if there is a way to do this with "group by" or i have seen queries that seem to contain additional select statements in brackets</p>
  
  <p>If anyone can please point me in the right direction I'd be very grateful.</p>
  
  <p>Thank you.</p>
  
  <p>Nick</p>