title: How to retrieve number of records from mysql database in a group?
body: <p>I have a table with multiple records in it for full name and surname.</p>
  
  <pre><code>---------------------------
  id | name       | lastname
  ---------------------------
   1 | A smith    | smith
   2 | B smith    | smith
   3 | c smith    | smith
   4 | A josh     | josh
   5 | B josh     | josh
   6 | C josh     | josh
   7 | D josh     | josh
   8 | A white    | white
   9 | D white    | white
   10| z white    | white
  </code></pre>
  
  <p>And so so....more than 100k records. Now what i want to do is to retrieve latest 7 records for each surname up to 9 surnames. I have 500 surnames but i just want latest 9 surnames..  In my application "latest" means "largest value of <code>id</code> column."</p>
  
  <p>This is the command that i tried to make but when i execute it. i am not getting any response from server. this is happening because of database size and my command is taking a lot of time. its just keep me waiting:</p>
  
  <pre><code>SELECT * FROM `queue` s WHERE ( SELECT COUNT(*) FROM `queue` f WHERE f.lastname = s.lastname AND f.id &gt;= s.id LIMIT 0 , 7) &lt;=7
  </code></pre>
  
  <p>Can someone suggest me better way of retrieving my goal.</p>