File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Order of records in a table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Order of records in a table?" Watch "Order of records in a table?" New topic

Order of records in a table?

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hello all,

Suppose i have a table "Employee".
I insert Employee1 into Employee table.
Then I insert Employee2 into Employee table.
Now if i do "Select * from Employee", will i always get Employee1 followed by Employee2. In other words will the order be always Employee1 and then Employee2. Is there any change that i may get Employee2 and then Employee1?

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
The order in which you get results from a SQL database is not defined - it can change from request to request. If you want the same order every time you'll have to explicitly sort the results.
Bryan Scarbrough
Ranch Hand

Joined: Aug 08, 2005
Posts: 49
You can specify: ORDER BY Employee Desc

This will take the results and display them based upon the Employee column in descending order, basically showing it Employee2, Employee1. You can also play with the GROUP BY function to group results together, such as all employees in a particular work center. You can even use the ORDER BY with the GROUP BY to make the results return exactly like you want them to.

Bryan Scarbrough<br /> <br />Consistency is the last resort of the unimaginative!
I agree. Here's the link:
subject: Order of records in a table?
It's not a secret anymore!