How many ways of getting the Last record in Sql-Server and MySql

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 669
There are several ways of getting the Last record in Sql-Server and MySql which are listed below:-

Suppose, we have an Employee_Master table that is having 50 records in it. And we want to fetch last record from Employee_Master table then we can do it by so may ways:-

1). select employee_id as last_id from employee_master where employee_id = (select max(employee_id) from employee_master);

2). select employee_id as last_id from employee_master where employee_id = all (select max(employee_id) from employee_master);

3). select max(employee_id) as last_id from employee_master;

4). select top 1 employee_id as last_id from employee_master order by employee_id desc;

5). select employee_id from employee_master where employee_id not in (select top ((select count(*) from employee_master) - 1) employee_id from employee_master);

6). select employee_id as last_id from employee_master where employee_id = ident_current('employee_master');

7). select ident_current('employee_master') as last_id;

8). select scope_identity() as last_id;

9). select @@identity as last_id;

Note:- point 8 and 9 will only give us last id after firing Insert statement
as below :-

Insert into employee_master(employee_name,employee_code)values('somename','somecode');

select scope_identity() as last_id;
select @@identity as last_id;

In Mysql:-
Select employee_id from employee_master order by employee_id desc Limit 1;

Comments or Responses

Login to post response