Monday, February 21, 2011

SQL Server interview question :- Select record with the second lowest value from the below customer table?

SQL Server interview question :- Select record with the second lowest value from the below customer table?


Answer:

Below is a simple customer table with 3 fields code , name and amount. Can you select the customer with second lowest amount. This is one of the favorite questions which is asked to test your SQL Server skills.



Below is the answer. It can be accomplished by using sub queries. First select the min and then get all records which are more than the min and select top 1 from the same. Below goes the query.

select top 1 * from tbl_Customer touter where touter.Amount > (SELECT
min(tinner.Amount)
 FROM [Customer].[dbo].[tbl_Customer] as
tinner)
 order by touter.Amount asc

Some time interviewers can trick you questions like select the second top. In the subquery we just need to change the same to max.

Get 200 SQL Server Interview questions and answers or .NET Interview questions and answers

No comments: