You have a Studets table as follows. The student table doesn't contains SlNo. You need to create a reoprt with SlNo. Write a query that add SlNo with the result set.
The student Master table is :

StuID SName Marks
S001 Naga 98
S102 Sundar 92
S203 Ganesh 95
S158 John 85
S211 Kumar 90

 Posted by Nagasundar_Tn on 12/3/2012 | Category: Sql Server Interview questions | Views: 2807 | Points: 40
Answer:

We need to use Row_Number() function to obtain the result. Row_Number() gives the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition. The query is

  SELECT ROW_NUMBER() OVER (ORDER BY StuID ASC) AS 'SlNo' ,* from StudentMaster


I used order by with StudentID. So student id gets ordered in ascending at first and then RowNumber() function will be implemented on the result set.

The output will be

SlNo  StuID   SName   Marks

1 S001 Naga 98
2 S102 Sundar 92
3 S158 John 85
4 S203 Ganesh 95
5 S211 Kumar 90


Asked In: GAVS Technology | Alert Moderator 

Comments or Responses

Login to post response