[SQL] Order of Appearence in Query Results.

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Database Sql
AI Thread Summary
Using SELECT * in SQL retrieves fields in the order they were created in the table, not based on any index. However, it's advisable to avoid using SELECT * due to several reasons. It offers less control over the data retrieved, can lead to inefficiencies by fetching unnecessary fields, and makes code harder to read and maintain. Instead, specifying only the required fields enhances clarity and aligns with best practices in coding. Additionally, database APIs typically allow access to columns by name rather than by position, reinforcing the importance of explicitly naming fields for better readability and understanding.
WWGD
Science Advisor
Homework Helper
Messages
7,678
Reaction score
12,354
Hi All, I know that if we use Select * , then the fields in the table will appear in the same order as listed. Same if we list all the fields manually, as in Select field1, field2,.., fieldn . Is it true that the order of the fields is preserved always (I can't think of other cases at this point)?
Thanks.
 
Technology news on Phys.org
Using SELECT * (which you should try to avoid) will display the fields in the order in which they were created in the table (not the index, if there is one).
 
Thank; why should I try to avoid Select * ?
 
WWGD said:
Thank; why should I try to avoid Select * ?
Less control and usually much less efficient. Grab only what you will use and name it. When looking through old code it's also easier to understand what you are trying to do.
 
I can't find a link right now, but remember reading somewhere - I think it was about the Codd Relational Algebra behind relational db's - that you are to think of the column names as a set not an array. In terms of writing code, the database API will almost always give you a way to get content from the db by column name not number, and its generally good form (for readability) to do so.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top