SELECT *
FROM favorites
WHERE favoriteId IN (
SELECT MAX(f2.favoriteId)
FROM favorites AS f1,
     favorites AS f2
WHERE f1.statusId = f2.statusId AND
      f1.statusId IN /*statusIds*/(0, 1, 2) AND
      f1.favoriteId >= f2.favoriteId
GROUP BY f1.favoriteId
HAVING COUNT(f1.favoriteId) <= /*limit*/0
)