Order of join during SQL query

Source: Internet
Author: User

In our work, we often need to write query statements. Left join and right join are common associations in query statements. But when we use them, have we ever thought about how to associate them and how to select their order?

For example, if Table A is associated with table B, table A must also be associated with table C. If table A is associated with table B, A and table B may have a null value or null value, A and C cannot have null values, so let's first think about how to deal with them?

You may say this is not easy, as shown below:

Implementation 1:

Select * from C inner join

On C. ID = A. ID

Left join B

On a. Code = B. Code

Implementation 2:

Select * from a left join B

On a. Code = B. Code

Inner join C

On a. ID = C. ID

Implementation 3:

Select * from B right join

On a. Code = B. Code

Inner join C

On a. ID = C. ID

Which of the three methods is listed at will, is it always better?

According to the current situation, if the data volume is the same as that at the time, first join inner and then

Left join and right join provide query efficiency.

However, it depends on the actual situation. Generally, we will give priority to implementation 1.

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.