What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 5293 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > SQL Server Interview Questions > How to get the nth row value of a table. ...

How to get the nth row value of a table.

Interview question and answer by: Dhiren.Kaunar@Gmail.Com | Posted on: 4/20/2012 | Category: SQL Server Interview questions | Views: 821 | | Points: 40


Answer:

Problem : I have a employee table then how to find out nth employee detail with out using the actual id.

For the above question I have found the below solution.

The below solution I have wrote the query for , getting the record of third employee.

CREATE TABLE [dbo].[Employee](
[Id] [int] NOT NULL PRIMARY KEY,
[Name] [varchar](50) NULL,
[Age] [int] NOT NULL,
[Photo] [image] NULL,
[Salary] [numeric](10, 2) NULL,
)

INSERT INTO Employee values (101,'James Clerk',29,NULL,1000.00);
INSERT INTO Employee values (102,'Steve Proell',40,NULL,60000.00);
INSERT INTO Employee values (103,'Matt Mcnair',35,NULL,5000.00);
INSERT INTO Employee values (104,'Amit Kr',29,NULL,1000.00);
INSERT INTO Employee values (105,'Jeff Yeary',32,NULL,1000.00);

# Solution 1
select  * from employee where id in (

select MAX(Id) from Employee
where Id in (select top(3) ID from Employee ))


# Solution 2

select top 1 *

from employee
where Id in (select top 3 Id from employee order by Id asc)
order by Id desc


# Solution 3

SELECT * FROM

(SELECT ROW_NUMBER() OVER (ORDER BY ID) AS RowNum, * FROM Employee) sub
WHERE RowNum = 3


Please suggest if any other solution are there for the above problem

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


 Responses

Posted by: Pervej | Posted on: 30 Apr 2012 06:51:46 AM | Points: 10 | Alert Moderator 
>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Dhiren.Kaunar@Gmail.Com

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 4:07:26 PM