I can do SELECT TOP (200) ... but why not BOTTOM (200)?
Well not to get into philosophy what I mean is, how can I do the equivalent of TOP (200) but in reverse (from the bottom, like you'd expect BOTTOM to do...)?
I can do SELECT TOP (200) ... but why not BOTTOM (200)?
Well not to get into philosophy what I mean is, how can I do the equivalent of TOP (200) but in reverse (from the bottom, like you'd expect BOTTOM to do...)?
Sorry, but I don't think I see any correct answers in my opinion.
The
TOP
x function shows the records in undefined order. From that definition follows that aBOTTOM
function can not be defined.Independent of any index or sort order. When you do an
ORDER BY y DESC
you get the rows with the highest y value first. If this is an autogenerated ID, it should show the records last added to the table, as suggested in the other answers. However:TOP
functionThe correct answer should be that there is not, and cannot be, an equivalent to
TOP
for getting the bottom rows."Tom H" answer above is correct and it works for me in getting Bottom 5 rows.
Thanks.
It is unnecessary. You can use an
ORDER BY
and just change the sort toDESC
to get the same effect.I've come up with a solution to this that doesn't require you to know the number of row returned.
For example, if you want to get all the locations logged in a table, except the latest 1 (or 2, or 5, or 34)
The currently accepted answer by "Justin Ethier" is not a correct answer as pointed out by "Protector one".
As far as I can see, as of now, no other answer or comment provides the equivalent of BOTTOM(x) the question author asked for.
First, let's consider a scenario where this functionality would be needed:
This returns a table of one column and five records:
As you can see: we don't have an ID column; we can't order by the returned column; and we can't select the bottom two records using standard SQL like we can do for the top two records.
Here is my attempt to provide a solution:
And here is a more complete solution:
I am by no means claiming that this is a good idea to use in all circumstances, but it provides the desired results.