Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opposite of "top" in sql server, without using order by, there are no keys/indices

Tags:

sql

sql-server

I want to retrieve the bottom 10 results from a sql server table. I want them to be the last 10 records that were inserted, how can I do this ?

I want to write select bottom 10 * from mytable however this does not exist (afaik).

I want the last 10 rows inserted. There is no timestamp.

like image 423
NimChimpsky Avatar asked Jan 03 '12 12:01

NimChimpsky


People also ask

What is the opposite of top in SQL query?

You can reverse the ordering by using DESC instead of ASC at the end of your query.

What is an alternative for top clause in SQL?

SQL SELECT TOP statement vs ROW_NUMBER function ROW_NUMBER function helps to give temporary numbers to the result set of the query and it can be also used instead of the TOP clauses.

What we can use instead of order by in SQL?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

How does SQL sort without order by?

In SQL Server if you don't specify an order then you may get either an index scan or an allocation ordered scan for example. Plus also you might encounter the "advanced scanning" / merry-go-round scanning feature.


1 Answers

You can't.

There is no guarantee at all that the last 10 records returned by select * from mytable will be the last 10 inserted. There is no default ordering that is used.

You need an ORDER BY on an appropriate column reflecting insert order.

like image 91
Martin Smith Avatar answered Nov 04 '22 07:11

Martin Smith