Wednesday, March 21, 2012

Order By

Hi,
I'm trying to understand a particular behaviour of ORDER BY.

Suppose I have 3 tables with a similar column name "samecol".

When I execute something like :

SELECT Tab1.a, Tab2.b, Tab3.c
FROM Tab1, Tab2, Tab3
ORDER BY samecol

Surprisingly, without specifying if it must order by Tab1.samecol or Tab2.samecol for instance, the query returns me a recordset.

Do you know if it chooses a table by default ?
Or it randomly order my datas ?

ThanksOriginally posted by Linirlan
Hi,
I'm trying to understand a particular behaviour of ORDER BY.

Suppose I have 3 tables with a similar column name "samecol".

When I execute something like :

SELECT Tab1.a, Tab2.b, Tab3.c
FROM Tab1, Tab2, Tab3
ORDER BY samecol

Surprisingly, without specifying if it must order by Tab1.samecol or Tab2.samecol for instance, the query returns me a recordset.

Do you know if it chooses a table by default ?
Or it randomly order my datas ?

Thanks
The answer is DBMS-specific. For example, the above is not possible in Oracle:

SQL> select emp.ename, dept.dname
2 from emp, dept
3 order by deptno;
order by deptno
*
ERROR at line 3:
ORA-00918: column ambiguously defined

(Both tables have a column called deptno.)

No comments:

Post a Comment