title: MySQL - how to use index in WHERE x IN (<subquery>)
body: <p>I'm using this query to get all employees of {clients with name starting with lowercase "a"}:</p>
  
  <pre><code>SELECT * FROM employees 
    WHERE client_id IN (SELECT id FROM clients WHERE name LIKE 'a%')
  </code></pre>
  
  <p>Column <code>employees.client_id</code> is an int, with <code>INDEX client_id (index_id)</code>. The subquery should IMHO return a list of id-s, which is then used in the WHERE clause.</p>
  
  <p>When I <code>EXPLAIN</code> the query, the primary query uses no indexes (<code>type:ALL</code>). But when I <code>EXPLAIN</code>
   a list taken from the subquery (e.g. <code>SELECT ... WHERE client_id IN (121,184,501)</code>), the <code>EXPLAIN</code> switches to <code>type:range</code>, and this query gets faster by 50%.</p>
  
  <p>How can I make the query use the index for the data returned by subquery - or, is there a more efficient way of retrieving this data? (Retrieving the id-list to application server, joining it and sending a second query is even more expensive here).</p>
  
  <p>Thanks in advance.</p>