Usage difference between On and Where in SQL statement Left join

Source: Internet
Author: User

The SQL statement is as follows:

SELECT *
FROM table 1
Left join table 2 ON table 1.id = TABLE 2.id AND table 2. Name! = 'Ff'
WHERE table 1. NAME! = 'A'

Step 1: Return Cartesian Product (SELECT * FROM table 1 cross join table 2)

Step 2: Apply the ON filter (the current condition is table 1.id = TABLE 2.id AND table 2. Name! = 'Ff ')

Step 3: Add external rows

This step only applies to outer join. If left join is performed, the table on the LEFT is reserved. If right join is performed, the table on the RIGHT is reserved. The external row refers to retaining the rows in the table. Even if some rows are filtered out in step 2, rows filtered out in step 2 are added based ON the reserved table. This is not the case in the current example.

Step 4. Apply the WHERE filter (currently Name! = 'A') filter the data of the virtual table generated in the first three steps.

Conclusion: If the SQL statement uses Left Join, the conditions after On have no effect On the Left table and only filter the Right table. The Where statement can filter the Left table.

If the SQL statement uses Right Join, the conditions after On have no effect On the Right table and only filter the Left table. The Where statement can filter the Right table.

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.