views:

43

answers:

6

how to select top 3rd record from a column in sql

A: 
  with tempTable(column1)
 as (
SELECT top(3) column1
FROM table1
ORDER BY column1DESC
) select top(1)column1 from tempTable

or

need to take first 3 records use

SELECT top(3) column1
    FROM table1
    ORDER BY column1DESC
anishmarokey
+1  A: 

Make use of Top

select  top 3 columnname form table 

More about SQL TOP Clause

Pranay Rana
I've never seem the form clause before! Is that new for SQL Server 2005? :-)
codingguy3000
Let he who has never tried to use the form clause cast the first negative comment!
codingguy3000
it will not return 3rd record , it will return first 3 rows.
Avadhesh
A: 

For top 3 records it's simple:

SELECT TOP 3 <column-name> FROM <table>

For the third record in a result set, try this page How do I get the nth row in a SQL Server table?

Zabba
A: 

To get the third row from the top.

;with cte as
(
select *, 
       row_number() over (order by number) as rn
from master.dbo.spt_values

)
select * 
from cte
where rn=3

To get the top third

select top (100.0/3) percent * 
from master.dbo.spt_values
order by number
Martin Smith
A: 

You can use below query

SELECT *
 FROM(
     SELECT ROW_NUMBER() OVER (ORDER BY [OrderBYColumn]) AS SrNo, 
             <Column1,Column2,Column3 ....> 
     FROM 
         TableName
     )    AS TableAlias
 WHERE 
     SrNo=3

Also for from and to range you can use

SELECT *
 FROM(
     SELECT ROW_NUMBER() OVER (ORDER BY [OrderBYColumn]) AS SrNo, 
             <Column1,Column2,Column3 ....> 
     FROM 
         TableName
     )    AS TableAlias
 WHERE 
SrNo between 3 and 5
Aamod Thakur
A: 
Select Top 1 col_name 
From (Select Top 3 col_name from tablename order by col_name desc)
Avadhesh