SQL SERVER 2008 – How to select the TOP N rows from a table

SELECT * FROM   (SELECT * FROM my_table ORDER BY col_name_1 DESC)

WHERE  ROWNUM < 10;

Use this workaround with prior releases:

SELECT *

FROM my_table a

WHERE 10 >= (SELECT COUNT(DISTINCT maxcol)

FROM my_table b

WHERE b.maxcol >= a.maxcol)

ORDER BY maxcol DESC;

Reference : Dilip Kumar Jena ( https://sqlexplore.wordpress.com )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s