A
A
alex_litv2021-08-09 12:02:12
SQL
alex_litv, 2021-08-09 12:02:12

What is the difference between the 2 queries?

Help me understand what the second request is doing.
The first, as I understand it, displays all matches of id tables a and b.
The second one I can't understand.
1. 2.SELECT * FROM a, b WHERE a.id=b.a_id;
SELECT * FROM a JOIN b ON a.id=b.a_id;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Akina, 2021-08-09
@Akina

The main difference is that the former uses the Cartesian product with selection, which has long been considered bad practice.
Otherwise , these two queries are absolutely equivalent up to the order of execution. The "comma" operation has a lower precedence than the JOIN operation, i.e. the first query does an implicit STRAIGHT_JOIN. And if the DBMS can't give a damn about this, the first query may generate a suboptimal execution plan even with up-to-date data statistics.
In more complex queries, the use of a comma, especially in combination with JOIN, can generally break the query completely, up to syntactic incorrectness. Therefore, it is strongly recommended to use CROSS JOIN instead of a comma.

S
Slava Rozhnev, 2021-08-09
@rozhnev

The result of these queries is identical, however you should use JOIN following modern SQL standards.
Related article

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question