Difference between left join and right join in SQL

Source: Internet
Author: User

In MYSQL, you can use the internal and external key links to merge data in related tables for conditional filtering:

First, create two new tables. The data is as follows:

Student table data:
Score table data:

We can see that the score table corresponding to the record with stu_id 16048008 in the students table has no data;

1. When the internal connection is performed, the system automatically ignores the data that cannot be matched in the two tables:
-- Display all data connected in:
SELECT * FROM students st inner join score SC ON st. sid = SC. stu_id;
 
There is too much data, only the at the end of the screenshot:


As you can see, the data is only displayed as 16048007,16048008 and not as shown, so the inner connection only displays all associated data.

2. The left link shows all the data in the left table of the keyword left. The NULL value is missing for the data in the right table, and the data is not displayed when the data is too much;
-- Left outer link left
SELECT * FROM students st left join score SC ON st. sid = SC. stu_id;

 

As you can see, 16048008 does not have data in the score table on the right. The system uses NUll to complete the data,
SELECT * FROM score SC left join students st ON st. sid = SC. stu_id;


 

The figure above shows the running result of changing the position of the students table and score table. We can see that the 16048008 records in the current right table were deleted because there was no data correspondence in the left table clock, we can see that the left link is based on the data in the left Table. All data in the left table is displayed. The right table only displays the data corresponding to the left Table. If NULl is missing, the left table is deleted;

Similarly, the right link serves as the reference on the right. If NULL is missing on the left and deletion is added, we will not talk about it here;

We can conclude that:
1. inner join: displays all data associated with the two tables;
2. The left link, with the left table as the reference, displays all data;
3. Right link: displays data in the right table as a reference;

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.