Difference between left and right connections in the database _ MySQL

Source: Internet
Author: User
Differences between left and right connections in databases Differences between left and right connections in databases


Today, someone asked me a question: What is the difference between left and right connections in the database? If there are two tables A and B, there are three data records in Table A and four data records in Table B. through the left and right connections, what is the minimum number of data records queried? What is the maximum number?


I was asked this question, and then I asked the database developer, the results are various:

A max 12 min 0

Maximum 12 minimum unknown B

C. The maximum unknown minimum is 3.

D. maximum 12. minimum 3

E unclear


1. description

(1) left join: data can be retrieved as long as there are records in the left table,The right side has

Only records in the left table can be retrieved.

(2) Right join: the right join is to retrieve data as long as there are records in the right table.


2. Examples

  1. Create two tables: t_left_tab and t_right_tab.
  2. Use t_left_tab as the left table and t_right_tab as the right table.
Left join: SELECT * FROM t_left_tab a left join t_right_tab B ON a. 'id' = B. 'id ';

Query Results:



Right JOIN: SELECT * FROM t_right_tab a left join t_left_tab B ON a. 'id' = B. 'id ';

Query Results:


Maximum number of queries: SELECT * FROM t_left_tab a left join t_right_tab B ON 1 = 1;

Query Results:


3. Summary

The difference between left and right connections of database A: The master table is different.

B connects to the right through the left connection. the minimum number of records is 3 (the number of records is smaller), and the maximum number is 12 (3 × 4)

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.