skip to Main Content

I need to display SQL query results by having specified columns appear first (on the leftmost side of the table).

The currrent code I have is:

Select * from table
order by column 4, column 3, column 1

Result:

column 1, column 2, column 3, column 4 - however it orders by the results in column 4 first.

The result I am looking for when using the same input is:

column 4, column 3, column 1, column 2

I do not care in which order the records are shown, I only care that column 4 appears first.

2

Answers


  1. Specify the columns explicitly instead of using select *. That always returns the columns in the order they appear in the CREATE TABLE statement.

    Select column4, column3, column1, column2
    from table
    order by column4, column3, column1
    
    Login or Signup to reply.
  2. The ORDER BY clause is not referring to the order in which each column appears. Rather, it refers to the order of the values in the column itself. e.g. if you had an ORDER BY height, weight clause, then height would be ordered in ascending order, followed by weight. However, this would not affect which column would appear in the table first.

    If you wanted to ensure that column 4 was first in the table, then it is simply a matter of designating it as such in the query, i.e:

    select column_4, column_3, column_2, column_1 from table;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search