Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC 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
Bookmark "Order of records in a table?" Watch "Order of records in a table?" New topic
Author

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?

Regards,
Saj
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
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.


Ping & DNS - my free Android networking tools app
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Order of records in a table?
 
Similar Threads
no persistent classes found for query class
System.out.printf
making list as unique
Help with Println and Scanner
Comparing instances of two different classes - HOW?