[SQL] Order of Appearence in Query Results.

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Database Sql
Click For Summary

Discussion Overview

The discussion revolves around the order of fields in SQL query results, particularly focusing on the implications of using SELECT * versus explicitly listing fields. Participants explore the consistency of field order in different contexts and the best practices for writing SQL queries.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant asserts that using SELECT * preserves the order of fields as they are listed in the table or query.
  • Another participant claims that SELECT * displays fields in the order they were created in the table, not necessarily reflecting any indexing.
  • Questions arise regarding the advisability of using SELECT *, with some participants suggesting it leads to less control and efficiency in queries.
  • A participant references a concept from Codd's Relational Algebra, suggesting that column names should be treated as a set rather than an array, advocating for using column names for clarity and readability in code.

Areas of Agreement / Disagreement

Participants express differing views on the implications of using SELECT * and the preservation of field order, indicating that multiple competing perspectives remain without a consensus.

Contextual Notes

Some claims depend on interpretations of SQL standards and practices, and there may be limitations in how field order is perceived based on different database implementations.

WWGD
Science Advisor
Homework Helper
Messages
7,806
Reaction score
13,120
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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 18 ·
Replies
18
Views
4K
Replies
5
Views
7K
  • · Replies 67 ·
3
Replies
67
Views
4K