[SQL] Order of Appearence in Query Results.

In summary, when using SELECT * to retrieve fields from a table, the fields will appear in the same order as they were created in the table, not the index. It is generally recommended to avoid using SELECT * and instead specify the specific fields to retrieve, as it allows for more control and is more efficient. Additionally, thinking of column names as a set rather than an array is a good practice for code readability.
  • #1
WWGD
Science Advisor
Gold Member
7,006
10,454
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
  • #2
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).
 
  • #3
Thank; why should I try to avoid Select * ?
 
  • #4
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.
 
  • #5
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.
 

What is the order of appearance in SQL query results?

The order of appearance in SQL query results is determined by the ORDER BY clause in the query. This clause allows you to specify which column you want the results to be sorted by, and whether you want the results to be sorted in ascending or descending order.

Can I change the order of appearance in SQL query results?

Yes, you can change the order of appearance in SQL query results by using the ORDER BY clause in your query. You can specify different columns and sorting orders to change the order of the results.

What happens if I don't include an ORDER BY clause in my SQL query?

If you don't include an ORDER BY clause in your SQL query, the order of appearance in the results will be unpredictable. The database will return the results in whatever order it sees fit, which may not be the order you expect.

Can I use multiple columns in the ORDER BY clause?

Yes, you can use multiple columns in the ORDER BY clause to determine the order of appearance in SQL query results. This allows you to sort the results by multiple criteria, such as sorting by last name and then first name.

Is the order of appearance in SQL query results case sensitive?

Yes, the order of appearance in SQL query results can be case sensitive depending on the collation settings of the database. Some collations treat uppercase and lowercase letters as equal, while others treat them as separate characters. It is important to consider collation when using the ORDER BY clause in your query.

Similar threads

  • Programming and Computer Science
Replies
7
Views
421
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Set Theory, Logic, Probability, Statistics
2
Replies
67
Views
2K
  • Programming and Computer Science
Replies
18
Views
3K
  • Programming and Computer Science
Replies
1
Views
842
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
5K
Back
Top