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.
Code |
Name |
Amount |
1001 |
pqr |
200.00 |
1002 |
raju |
100.00 |
1004 |
shiv |
300.00 |
1007 |
shaam |
400.00 |
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.
Asked In: Many Interviews |
Alert Moderator