Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of records in a table?

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic