SQL Show most recent record in GROUP BY?

Start with this:

select StudentId, max(DateApproved) 
from tbl
group by StudentId

Then integrate that to main query:

select * 
from tbl
where (StudentId, DateApproved) in

(
  select StudentId, max(DateApproved) 
  from tbl
  group by StudentId
)

You can also use this:

select * 
from tbl
join (select StudentId, max(DateApproved) as DateApproved 
      from tbl 
      group by StudentId)
using (StudentId, DateApproved)

But I prefer tuple testing, it’s way neater

Live test: http://www.sqlfiddle.com/#!2/771b8/5

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)