title: Which is best way of getting min and max date while comparing keys of 2 table
body: <p>I am learning mysql, I have 2 tables, I have to compare table1 primary keys with table2 primary key, on successful match I need to get date min and max from table2 for this I have tried these commands, though I got result but it takes long time please let me know if there any good way to handle this case.</p>
  
  <p><strong>date format is like this</strong></p>
  
  <pre><code>   mysql&gt; select sdate from table2
     | 27-Apr-2000 11:50:00 AM |
     | 27-Apr-2000 10:20:00 AM |
     | 27-Apr-2000 08:30:00 AM |
     | 20-Jan-1999 12:00:00 PM |
  </code></pre>
  
  <p><strong>Commands I tried</strong> </p>
  
  <pre><code>   mysql&gt; select min(str_to_date(m.sdate,'%d-%M-%Y')) as date_min, max(str_to_date(m.sdate,'%d-%M-%Y')) as date_max from ( select distinct p.key1 as key1, p.key2 as key2 from table1 as p ) as T inner join table2 as m on T.key1 = m.key1 and T.key2 = m.key2 where m.sdate !='';
     +------------+------------+
     | date_min   | date_max   |
     +------------+------------+
     | 1989-02-24 | 2011-12-30 |
     +------------+------------+
     1 row in set, 11396 warnings (18.95 sec)
  
  
     mysql&gt; select min(str_to_date(m.sdate,'%d-%M-%Y')) as date_min, max(str_to_date(m.sdate,'%d-%M-%Y')) as date_max from ( select p.key1 as key1, p.key2 as key2 from table1 as p ) as T inner join table2 as m on T.key1 = m.key1 and T.key2 = m.key2 where m.sdate !='';
     +------------+------------+
     | date_min   | date_max   |
     +------------+------------+
     | 1989-02-24 | 2011-12-30 |
     +------------+------------+
     1 row in set, 11442 warnings (18.78 sec)
  
  
     mysql&gt; select min(str_to_date(m.sdate,'%d-%M-%Y')) as date_min, max(str_to_date(m.sdate,'%d-%M-%Y')) as date_max from table2 as m, table1 as p where p.key1 = m.key1 and p.key2 = m.key2 and m.sdate !='';
     +------------+------------+
     | date_min   | date_max   |
     +------------+------------+
     | 1989-02-24 | 2011-12-30 |
     +------------+------------+
     1 row in set, 11442 warnings (18.86 sec)
  
     mysql&gt; 
  </code></pre>