0

SQL Server 2005 query is as follows......

SELECT ClgId 
FROM IdMaker_DB  
WHERE Course = 'B-Tech' AND [Class] = 'Ist Year'
AND Branch = 'Computer Science and Engineering' 
ORDER BY ClgId

It works fine and selects all entries of Computer Science and Engineering Ist Year but is select only first row, how can I select nth row......

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • This may be a duplicated question: You can find an answer here: http://stackoverflow.com/questions/187998/row-offset-in-sql-server – chris Feb 02 '12 at 20:43

3 Answers3

3
select * from (
 SELECT ClgId ,row_number() over (order by ClgId) as rn  FROM IdMaker_DB 
where Course ='B-Tech' and [Class]='Ist Year'

 and Branch='Computer Science and Engineering'   ) a where rn=n   --replace the n
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0
SELECT top 1 ClgId 
FROM IdMaker_DB  
where Course ='B-Tech' 
and [Class]='Ist Year'
and Branch='Computer Science and Engineering' 
and clgid not in 
    (SELECT top N - 1 ClgId 
    FROM IdMaker_DB  
    where Course ='B-Tech' 
    and [Class]='Ist Year'
    and Branch='Computer Science and Engineering' 
    ORDER BY ClgId)
ORDER BY ClgId
John MacIntyre
  • 12,910
  • 13
  • 67
  • 106
-3
SELECT TOP 1 * 
FROM   student 
WHERE  id IN 
       ( 
        SELECT TOP n id 
        FROM student 
       ) 
ORDER BY id DESC
Leigh
  • 28,765
  • 10
  • 55
  • 103
Deepak
  • 1
  • 1
    This does not provide a correct answer to the question asked. Using `TOP` without an `ORDER BY` produces results of an [undefined order](http://technet.microsoft.com/en-us/library/ms189463.aspx). Not sure why this was posted, seeing as how two valid answers were already given ... over a year ago. – Leigh Sep 09 '13 at 00:24