title: MySQL MAX and MIN on Varchar
body: <p>I have a table that contains 3 columns; <code>day_id</code>, <code>start_date</code>, <code>end_date</code>. <code>start_date</code> and <code>end_date</code> are varchar(8) in a format like this <code>HH:II:SS</code>. Sometimes dates can go over 24h in order to represent that something happened day after, for example: 25:20:01 is 01:20:01 but in a new day. <strong><code>day_id</code> is not unique, it repeats.</strong> I need to get first and last event of a day, and this is my code:</p>
  
  <pre><code>SELECT day_id, 
  MIN(start_date) as start_time, 
  MAX(end_date) as end_date 
  FROM events WHERE day_id IN ('day_1', 'day_2', 'day_3') 
  GROUP BY day_id ORDER BY start_time ASC
  </code></pre>
  
  <p>It works as intended but I can't figure out why, how does MySQL know that <code>25:01:45</code> is larger than <code>20:21:09</code> since they are both varchars? The whole table is in <code>utf8mb4_0900_ai_ci</code> collation, running on MySQL server version 8.</p>