How to select bottom most rows?
SELECT
columns
FROM
(
SELECT TOP 200
columns
FROM
My_Table
ORDER BY
a_column DESC
) SQ
ORDER BY
a_column ASC
Logically,
BOTTOM (x) is all the records except TOP (n - x), where n is the count; x <= n
E.g. Select Bottom 1000 from Employee:
In T-SQL,
DECLARE
@bottom int,
@count int
SET @bottom = 1000
SET @count = (select COUNT(*) from Employee)
select * from Employee emp where emp.EmployeeID not in
(
SELECT TOP (@count-@bottom) Employee.EmployeeID FROM Employee
)
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 a BOTTOM
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:
- This only works if there is an autogenerated id column
- It has a significant performance impact if you compare that with the
TOP
function
The correct answer should be that there is not, and cannot be, an equivalent to TOP
for getting the bottom rows.
It is unnecessary. You can use an ORDER BY
and just change the sort to DESC
to get the same effect.