0

I have a simple table:

Cartesian
+--------+
| letter |
+--------+
| A      |
| B      |
| C      |
+--------+

Self Join query 1:

SELECT t1.letter, t2.letter
FROM cartesian t1, cartesian t2;

Result:

+--------+--------+
| letter | letter |
+--------+--------+
| A      | A      |
| B      | A      |
| C      | A      |
| A      | B      |
| B      | B      |
| C      | B      |
| A      | C      |
| B      | C      |
| C      | C      |
+--------+--------+

Self Join query 2:

SELECT t1.letter, t2.letter
FROM cartesian t2, cartesian t1;

Result:

+--------+--------+
| letter | letter |
+--------+--------+
| A      | A      |
| A      | B      |
| A      | C      |
| B      | A      |
| B      | B      |
| B      | C      |
| C      | A      |
| C      | B      |
| C      | C      |
+--------+--------+

Why are the results different? I am selecting the same columns and from the same tables. Only thing different is the ordering of tables in the FROM clause.

4 Answers 4

1

But the two results are not different at all if you disregard the ordering - the two sets contain the exact same pairs of tuples and therefore are exactly the same.

Any result that is returned and not explicitly ordered using an order by clause will not be guaranteed to be ordered in any way. The fact that the order does not appear to be random is due to the internal workings of the query processing component of the database and not something that can or should be relied on.

If you want order you have to state it.

0

The results aren't different. The same data exists in both results.

Without an order by clause its doing it itself, and for what ever reason its decided that t2 will be ordered alphabetically where t1 wont in both cases.

0

Same results, different order.

Actually the simple thing is as long as there is not order by the output order can differ each time, even when using group by to be sure you are getting the order you want you need to give the order-by. This is a vendor specific implementation and has nothing to do with sql language. (Even though most db implementations give the order by embedded in the group by.)

0

Ordering is not guaranteed unless you explicitly use order by

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.