Technical Article

To fetch the immediate previous and next row

,

Consider an employee table containing name and age of the employees. Write a script to retrieve the row which contain the details of the employee whose age is 50 and also retrieve the immediate next and previous rows

create table employee(name varchar(50), age int)

insert into employee values('ADAMS',30)
insert into employee values('ALEX',35)
insert into employee values('BILL',40)
insert into employee values('BRIAN',50)
insert into employee values('DAMIEN',60)



--SCRIPT
select top 3 * from employee where age <= (select min(age) from employee where age > 40) order by age desc

Rate

1 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

1 (2)

You rated this post out of 5. Change rating