Discussion:
opposite of SELECT TOP(n) *
(too old to reply)
Jim in Arizona
2008-06-05 17:28:09 UTC
Permalink
SELECT TOP(n) returns the top n records. How can I return the bottom n
records? BOL on SELECT didn't seem to have an answer to this (that I could
easily see anyway).

Thanks,
Jim
Raymond D'Anjou
2008-06-05 17:35:10 UTC
Permalink
Just 'ORDER DESC'
Post by Jim in Arizona
SELECT TOP(n) returns the top n records. How can I return the bottom n
records? BOL on SELECT didn't seem to have an answer to this (that I could
easily see anyway).
Thanks,
Jim
Tom Moreau
2008-06-05 17:35:20 UTC
Permalink
Just use ASC or DESC in your ORDER BY:

This gives you the worst students:

select top (10)
*
from
Students
order by
GPA asc

This gives you the best students:

select top (10)
*
from
Students
order by
GPA desc
--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau


"Jim in Arizona" <***@hotmail.com> wrote in message news:***@TK2MSFTNGP02.phx.gbl...
SELECT TOP(n) returns the top n records. How can I return the bottom n
records? BOL on SELECT didn't seem to have an answer to this (that I could
easily see anyway).

Thanks,
Jim
Eric Isaacs
2008-06-05 17:42:43 UTC
Permalink
If you order by the clustered index in DESC order, you'll get the
bottom(N) records in descending order.

SELECT TOP(#) *
FROM table
ORDER BY [clustered-index-field][, fields...] DESC
Madhivanan
2008-06-05 18:09:05 UTC
Permalink
Post by Jim in Arizona
SELECT TOP(n) returns the top n records. How can I return the bottom n
records? BOL on SELECT didn't seem to have an answer to this (that I could
easily see anyway).
Thanks,
Jim
Note that if you dont use order by clause the result may be random
Jim in Arizona
2008-06-05 19:06:59 UTC
Permalink
Post by Jim in Arizona
SELECT TOP(n) returns the top n records. How can I return the bottom n
records? BOL on SELECT didn't seem to have an answer to this (that I could
easily see anyway).
Thanks,
Jim
Thanks Raymond, Dr. Moreau, Eric and Madhivanan. I knew it was something
really simple!
We learn (at least try to) something new every day! :)

Continue reading on narkive:
Loading...