case 1

explain
SELECT *
FROM table a
WHERE (SELECT COUNT(*) FROM table WHERE class_id = a.class_id and id < a.id) < 2
ORDER BY a.class_id, a.element_id DESC

case 2

explain
select a.*
FROM table a
         LEFT JOIN table b ON a.class_id = b.class_id AND a.id > b.id
group by a.id, a.element_id, a.class_id
having count(b.id) < 2