Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does an index provide order by itself?

Tags:

sql

indexing

lets say I have

create table mytable(
 a VARCHAR(200)
 b VARCHAR(200)
 c VARCHAR(200)
)

create index on mytable (b)

if I select

select a, b, c from mytable;

would it be sorted by b?

like image 996
dnuske Avatar asked Dec 04 '22 08:12

dnuske


2 Answers

Perhaps (more likely in the case of clustered indexes I would imagine), but you cannot rely on this or expect it. Unless you have an order by, assume it will not be ordered.

like image 121
Kieren Johnstone Avatar answered Jan 01 '23 21:01

Kieren Johnstone


You should never assume that data returned by a query to a RDBMS will be in any particular order. The only way to be certain that the data is ordered is to explicitly request (generally with the ORDER BY clause) for the database engine to sort and order the data returned by the query.

like image 35
Philip Kelley Avatar answered Jan 01 '23 21:01

Philip Kelley