select
a.*,b.*
from
categories a,
(
select book_id,Book_name,categoryid
from books
where book_id in (select max(book_id) from books group by categoryid )
) b
where
a.id=b.categoryid
a.*,b.*
from
categories a,
(
select book_id,Book_name,categoryid
from books
where book_id in (select max(book_id) from books group by categoryid )
) b
where
a.id=b.categoryid
inner join categories on books.categoryid=categories.id
group by categoryid
order by book_id desc