-2

I have a table1 which has column "Id". When I write query "select * from table1" it is giving me correct result like below

Id
4
5
2
3
1

But I want the result like -

Id
1
2
3
4
5

I know I can achieve this by using "order by Id" but I want this result even if I use "select * from table1".

Himanshu Jansari
  • 30,115
  • 28
  • 106
  • 129
  • 1
    Add an `ORDER BY` to your `SELECT` statement... Without an `ORDER BY` your data can be returned in *any* arbitrary order. There is **no** way to guarantee the order of the data without an `ORDER BY`. – Larnu May 24 '22 at 08:51
  • Data won't be returned in _any_ arbitrary order. It will be returned in the order of the index used to retrieve the data. If your table has a primary key, and the query doesn't go parallel, numbers should be returned in numerical order. `CREATE TABLE table1 (Id int primary key); INSERT INTO table1 (Id) VALUES (4),(5),(2),(3),(1); SELECT Id FROM table1;` and data will be returned in order, because the Primary Key is enforced using an index. But if the query goes parallel, all bets are off and you should use `ORDER BY` – mikkel May 24 '22 at 12:50

0 Answers0