If it's just a straight join between two tables, then the database gets the rows they physically exist on disk.
It's rare for rows to move. So if you inserted rows in the order you want them in your queries, you "got lucky".
If someone reorgs the table for some reason, you'll likely find the sort is now wrong.
In the example below:
- Insert rows in t2 in a random order
- The uses attribute clustering to force the database to store the rows in a specific way (after the move)
create table t1 (
c1 int
);
create table t2 (
c1 int, c2 int
);
insert into t1
with rws as (
select level x from dual
connect by level <= 10
)
select * from rws;
insert into t2
with rws as (
select level x from dual
connect by level <= 10
)
select t1.c1, x
from t1 cross join rws
order by dbms_random.value;
commit;
select t1.c1, t2.c2
from t1
join t2
on t1.c1 = t2.c1
order by t1.c1
fetch first 10 rows only;
C1 C2
---------- ----------
1 10
1 7
1 2
1 8
1 3
1 9
1 4
1 6
1 1
1 5
alter table t2
add clustering
by linear order ( c1, c2 );
alter table t2 move;
select t1.c1, t2.c2
from t1
join t2
on t1.c1 = t2.c1
order by t1.c1
fetch first 10 rows only;
C1 C2
---------- ----------
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
alter table t2
drop clustering ;
alter table t2
add clustering by
interleaved order ( c1, c2 );
alter table t2 move;
select t1.c1, t2.c2
from t1
join t2
on t1.c1 = t2.c1
order by t1.c1
fetch first 10 rows only;
C1 C2
---------- ----------
1 1
1 10
1 9
1 8
1 7
1 6
1 5
1 4
1 3
1 2
Note how the order of the values for C2 changes every time!