title: PostgreSQL sort by oldest of two dates?
body: <p>I need to sort a postgres table by the "more recent of column A, fallback to column B"</p>
  
  <p>If my table looks like this: <code>id</code>, <code>reminder_at</code>, <code>updated_at</code></p>
  
  <pre><code>1, 01-11-2019, 12-01-2018
  2, null,       01-04-2019
  3, null,       01-02-2019
  4, 01-01-2019, 01-04-2019
  </code></pre>
  
  <p>expected sorting output would be</p>
  
  <pre><code>4, 01-01-2019, 01-04-2019 # 01-01-2019 is soonest 
  3, null,       01-02-2019 # then 01-02-2019
  2, null,       01-04-2019 # then 01-04-2019
  1, 01-11-2019, 12-01-2018 # then 01-11-2019
  </code></pre>
  
  <p>I'm currently doing this with application code, and i'd prefer to do in SQL</p>
  
  <p>For example if the reminder_at went to NULL for record 1, then it would immediately go to the top because the <code>updated_at</code> date is the oldest</p>
  
  <p>Currently:</p>
  
  <pre><code>SELECT * 
  FROM "tasks" 
  WHERE completed_at IS NULL
  ORDER by reminder_at, updated_at
  </code></pre>
  
  <p><strong>EDIT with Correct Answer:</strong></p>
  
  <pre><code>SELECT * 
  FROM "tasks" 
  WHERE completed_at IS NULL
  ORDER by COALESCE(reminder_at, updated_at)
  </code></pre>