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
- Create two tables: t_left_tab and t_right_tab.
- 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)