I want to limit the size of records inside a group, and here is my trial, how to do it right?
mysql> select * from accounts limit 5 group by type;
Try placing the LIMIT clause after the GROUP BY clause.
EDIT: Try this:
SELECT * FROM accounts a1 WHERE 5 > ( SELECT COUNT(*) FROM accounts a2 WHERE a2.type = a1.type AND a2.balance > a1.balance )
This returns at most 5 accounts of each type with the biggest balances.