The difference between on and where in left_join and inner_join in SQL statements

Source: Internet
Author: User
The following is a detailed analysis of the differences between on and where in left_join and inner_join in SQL statements. For more information, see tablea (id, type ): idtype ---------------------------------- 112132 tableb (id, class): idclass ---------------------------------

The following is a detailed analysis of the differences between on and where in left_join and inner_join in SQL statements. For more information, see table a (id, type ): id type ---------------------------------- 1 1 2 1 3 2 table B (id, class): id class ---------------------------------

The following is a detailed analysis of the differences between on and where in left_join and inner_join in SQL statements. For more information, see

Table a (id, type ):
Id type
----------------------------------
1 1
2 1
3 2
Table B (id, class ):
Id class
---------------------------------
1 1
2 2
SQL statement 1: select a. *, B. * from a left join B on a. id = B. id and a. type = 1;
SQL statement 2: select a. *, B. * from a left join B on a. id = B. id where a. type = 1;
SQL statement 3: select a. *, B. * from a left join B on a. id = B. id and B. class = 1;

The execution result of SQL statement 1 is:
A. id a. type B. id B. class
----------------------------------------
1 1 1 1
2 1 2 2
3 2

The execution result of SQL statement 2 is:
A. id a. type B. id B. class
----------------------------------------
1 1 1 1
2 1 2 2

The execution result of SQL statement 3 is:
A. id a. type B. id B. class
----------------------------------------
1 1 1 1
2 1
3 2
As can be seen from SQL statement 1, all records in the left table in left join will be queried and displayed. The condition after on does not work for it unless it is followed by where for filtering, this is SQL statement 2. It can be seen from SQL statement 3. In the condition following on, the restriction condition of the right table will take effect.
**************************************** **********************************
SQL statement 4: select a. *, B. * from a inner join B on a. id = B. id and a. type = 1;
SQL statement 5: select a. *, B. * from a inner join B on a. id = B. id where a. type = 1;
SQL statement 6: select a. *, B. * from a, B where a. id = B. id and a. type = 1;
SQL statement 7: select a. *, B. * from a, B where a. type = 1 and a. id = B. id;

The execution results of these four statements are the same, as follows:
A. id a. type B. id B. class
----------------------------------------
1 1 1 1
2 1 2 2
It can be seen that all the constraints after on in inner join will take effect, which is the same as the execution result of where. In addition, the where statement and inner join can indeed get the same result, but the efficiency is different (I have not tested this, but I believe this conclusion ).
But is SQL statement 6 less efficient than SQL statement 7? I don't have enough data to test it, but I believe so.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.