I am not able to understand how Cartesian product works. Consider the simple schema:
mysql> select * from account;
+----------------+-------------+---------+
| account_number | branch_name | balance |
+----------------+-------------+---------+
| A101 | Downtown | 500 |
| A102 | Perryridge | 400 |
| A201 | Brighton | 900 |
| A215 | Mianus | 700 |
| A217 | Brighton | 750 |
| A222 | Redwood | 700 |
| A305 | Round Hill | 350 |
+----------------+-------------+---------+
7 rows in set (0.00 sec)
Now, when I pose the query
select a.balance from account a, account b where a.balance<b.balance;
I get a series of values except the maximum value 900. Then using the not in
operator I determine the maximum value. Before that in the above query, when the join takes place based on the condition a.balance<b.balance
, the first tuple in the relation must be 500
. Theoretically, the first 5 values must be:
500
500
500
500
400
But I get :
+---------+
| balance |
+---------+
| 400 |
| 350 |
| 350 |
| 500 |
| 400 |
How is it working? I am using MySQL database.
A Cartesian join joins every record in the first table with every record in the second table, so since your table has 7 rows and it's joined with itself, it should return 49 records had you not had a
where
clause. Your where clause only allows records wherea
's balance is smaller thanb
's balance. Since900
is, as you said, the maximal balance in the table, it will never be smaller than any other balance, and therefore it will never be returned.With regard to the first five rows, the normal rules of SQL apply to joins too. Since SQL tables have no intrinsic order, it's completely up to the database to decide how to return them, unless you explicitly state an order in the
order by
clause. The values you listed are perfectly valid values you'd expect the query to return.