title: Select one record from query response based on condition | function postgres
body: <p>I am new to sql/postgres and trying to write a postgresql query which will return one record from a DB table</p>
  
  <p><strong>My current query:</strong></p>
  
  <pre><code>select * from table 
  where key = 'WITS' or substring(key, 1,1) = substring('WITS', 1, 1);
  </code></pre>
  
  <p><strong>Response:</strong></p>
  
  <ul>
  <li>WHITE</li>
  <li>WITS</li>
  </ul>
  
  <p><strong>Problem:</strong>
  I want to update this query in a way that it returns only <strong>WITS</strong> in response. 
  <em>i.e. if key is matched, return the record else match the first character</em></p>
  
  <p>I think postgres function may be of some help in this regard and i can add this if/else condition in that.</p>
  
  <p>How to do it?</p>
  
  <p><strong>Sample Data:</strong>
  keys = WITS, WHITE, Abhis, Amanda, Blue, Delta</p>
  
  <p>Table attched in screenshot</p>
  
  <p><a href="https://i.stack.imgur.com/lF93n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lF93n.png" alt="enter image description here"></a></p>