0

I'm working on a database interface and need the answer of the following question:

Will SELECT * FROM some_table always return the results in the same order in MySQL?

Vingtoft
  • 13,368
  • 23
  • 86
  • 135
  • 1
    `SELECT *` is antipattern. In long run more problems than gains. What if in the future someone adds new column? – Lukasz Szozda Apr 15 '16 at 09:36
  • same column order, or row order? – Alnitak Apr 15 '16 at 09:36
  • 2
    If you mean the order of columns: yes. If you mean the order of rows: no. Except you add some `order by` clause, then the order of rows is always the same, too. – PerlDuck Apr 15 '16 at 09:37
  • As long as you dont use order by clause – Akshay Apr 15 '16 at 09:39
  • 3
    Relying on a specific column order opens your code to an obvious potential bug: if you alter the table you need to ensure that new columns are inserted in the expected location. DBAs will normally avoid that at any cost on very large data sets. – Álvaro González Apr 15 '16 at 09:44

1 Answers1

-1

By default it returns the order by primary_key(if you do not provide any order by clause)

Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14
  • 1
    That would be news to me? – Pekka Apr 15 '16 at 09:40
  • 3
    Ah, it seems to be *sometimes* the case, but can't be relied upon. See http://stackoverflow.com/questions/8746519/sql-what-is-the-default-order-by-of-queries – Pekka Apr 15 '16 at 09:41
  • I guess it can happen if query optimiser decides to fetch rows directly through an index. But SQL thumb rule is: avoid unnecessary expensive operations (such as sorting). – Álvaro González Apr 15 '16 at 09:52