kks
kks

Reputation: 342

How to get Student Details SQL Query

Upvotes: 3

Views: 4378

Answers (1)

Brian DeMilia
Brian DeMilia

Reputation: 13248

The fastest approach is probably to use an analytic function:

select *
  from (select s.*,
               row_number() over(partition by sid order by date desc) as rn
          from student s)
 where rn = 1

Another way is a correlated subquery:

select *
  from student s
 where date = (select max(x.date) from student x where x.sid = s.sid)

Another way is to join into an inline view:

select s.*
  from student s
  join (select sid, max(date) as sid_last_date from student group by sid) v
    on s.sid = v.sid
   and s.date = v.sid_last_date

Upvotes: 4

Related Questions