Is there a way to order data in reverse of the order it appears in the database?
The answer is No! The answer is also Yes.
Since there is no inherent sequence of rows in a database table, there can be no order, and thus there can be no "reverse" order.
This is not to say, however, that you won't notice some order in the rows returned by a simple query like --
select something from yourTable
Even more maddening is that you may see the same order being returned consistently, and that it appears to be FIFO (first in , first out).
Do not make the assumption, however, that this order is reliable. Relational databases have no "first" record, or "last" record, the way a physical file does. The only way to get reliable ordering is to specify a column by which the rows can be sorted. Then, of course, you can specify the "reverse" order using the DESCENDING keyword --
select something from yourTable order by sortfield desc
One of the easiest ways to preserve FIFO sequence is by making sure that the table has an autonumber or sequence or identity column, which is automatically incremented by the database as rows are inserted into the table. You would then use this column as the sort field in the ORDER BY clause with the DESC keyword.
For More Information
- What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.