Detailed explanation of various join summaries of SQL

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natural Join Summary of Various Joins

In SQL, there are various Joins, including Left Join, Right Join, Inner Join, and Natural Join. Beginners must be confused about what they are. What are the differences and connections between them? Let's use a picture to illustrate:

The above picture illustrates the difference between Left Join, Right Join, Inner Join, and Full Outer Join. Let's use a simple example to help us understand and distinguish them. Now there are two tables Person and Address:

-- Table Person
+ -- --------+-----------+----------+
| PersonId | FirstName | LastName |
+ -- --------+-----------+----------+
| 1 | Zhang | San |
| 2 | Li | Si |
| 3 | Wang | Wu |
| 4 | Yang | Liu |
+ -- --------+-----------+----------+


--Table Address
+ ---------- +----------+---------------+-------+
| AddressId | PersonId | City | State |
+ ---------- +----------+---------------+-------+
| 1 | 2 | San Francisco | CA |
| 2 | 3 | Los Angeles | CA |
| 3 | 1 | San Diego | CA |
+ ---------- +----------+---------------+-------+

Let’s look at each one of them below:

Left Join: returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

Left intersection: Returns all rows of the left table and the matching rows of the right table. If there is no match, use NULL.

SELECT * FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId;
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City | State |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+
| 2 | Li | Si | 1 | 2 | San Francisco | CA |
| 3 | Wang | Wu | 2 | 3 | Los Angeles | CA |
| 1 | Zhang | San | 3 | 1 | San Diego | CA |
| 4 | Yang | Liu | NULL | NULL | NULL | NULL |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+

Right Join: returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

Right intersection: Returns all rows of the right table and the matching rows of the left table. If there is no match, use NULL.

SELECT * FROM Person RIGHT JOIN Address ON Person.PersonId = Address.PersonId;
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City | State |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+
| 1 | Zhang | San | 3 | 1 | San Diego | CA |
| 2 | Li | Si | 1 | 2 | San Francisco | CA |
| 3 | Wang | Wu | 2 | 3 | Los Angeles | CA |
| NULL | NULL | NULL | 4 | 5 | Memphis | TN |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+

Inner Join: selects all rows from both tables as long as there is a match between the columns in both tables.

Internal intersection: Select the rows where the keywords match in the left and right tables.

SELECT * FROM Person INNER JOIN Address ON Person.PersonId = Address.PersonId; + -- --------+-----------+----------+-----------+----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City | State |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+
| 1 | Zhang | San | 3 | 1 | San Diego | CA |
| 2 | Li | Si | 1 | 2 | San Francisco | CA |
| 3 | Wang | Wu | 2 | 3 | Los Angeles | CA |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+

Full Join: returns all rows from the left table (table1) and from the right table (table2), and it combines the result of both LEFT and RIGHT joins.

Full intersection: Returns all rows of the left table and all rows of the right table, which is the union of the left intersection and the right intersection.

Note that since there is no Full Join command in MySQL, we can also UNION the results of Left Join and Right Join:

SELECT * FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId UNION
SELECT * FROM Person RIGHT JOIN Address ON Person.PersonId = Address.PersonId; + -- --------+-----------+----------+-----------+----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City | State |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+
| 2 | Li | Si | 1 | 2 | San Francisco | CA |
| 3 | Wang | Wu | 2 | 3 | Los Angeles | CA |
| 1 | Zhang | San | 3 | 1 | San Diego | CA |
| 4 | Yang | Liu | NULL | NULL | NULL | NULL |
| NULL | NULL | NULL | 4 | 5 | Memphis | TN |
+ -- --------+-----------+----------+-----------+-----------+---------------+-------+

Natural Join: creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. A NATURAL JOIN can be an INNER join , a LEFT OUTER join , or a RIGHT OUTER join . The default is INNER join .

Natural intersection: Create an implicit join operation based on the common columns of the left and right tables. The common columns are the two columns with the same column names in the two tables. Natural intersection can be inward intersection, left intersection or right intersection. The default is internal.

SELECT * FROM Person NATURAL JOIN Address;

+ -- --------+-----------+----------+-----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | City | State |
+ -- --------+-----------+----------+-----------+---------------+-------+
| 1 | Zhang | San | 3 | San Diego | CA |
| 2 | Li | Si | 1 | San Francisco | CA |
| 3 | Wang | Wu | 2 | Los Angeles | CA |
+ -- --------+-----------+----------+-----------+---------------+-------+

Finally, note that the keywords on both sides of the equal sign below are equivalent:

A LEFT JOIN B = A LEFT OUTER JOIN B
A RIGHT JOIN B = A RIGHT OUTER JOIN B
A FULL JOIN B = A FULL OUTER JOIN B
A INNER JOIN B = A JOIN B

This is the end of this article on the detailed explanation of various join summaries of SQL. For more relevant content on various join summaries of SQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on the underlying principle of mysql join
  • Analysis of usage scenarios of JOIN in SQL statements
  • MYSQL database basics - Join operation principle
  • How to solve the problem of invalid left join in MySQL and the precautions for its use
  • The process of quickly converting mysql left join to inner join
  • Why do code standards require SQL statements not to have too many joins?
  • MySQL efficient query left join and group by (plus index)
  • MySQL join buffer principle

<<:  Solve the problem of running jupyter notebook on the server

>>:  The tdwidth setting of the table in multiple divs is the same and cannot be aligned

Recommend

Summary of Linux vi command knowledge points and usage

Detailed explanation of Linux vi command The vi e...

Docker nginx example method to deploy multiple projects

Prerequisites 1. Docker has been installed on the...

Solution to Docker disk space cleaning

Some time ago, I encountered the problem that the...

Detailed explanation of the problem of configuring servlet url-pattern in tomcat

When configuring web.xml for tomcat, servlet is a...

A brief analysis of CSS3 using text-overflow to solve text layout problems

Basic syntax The use of text-overflow requires th...

How to gracefully and safely shut down the MySQL process

Preface This article analyzes the process of shut...

Linux system prohibits remote login command of root account

ps: Here is how to disable remote login of root a...

JavaScript canvas to achieve scratch lottery example

This article shares the specific code of JavaScri...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

Detailed use cases of MySql escape

MySQL escape Escape means the original semantics ...

Introduction to the use of html base tag target=_parent

The <base> tag specifies the default address...

React handwriting tab switching problem

Parent File import React, { useState } from '...

Sample code for implementing horizontal infinite scrolling with pure CSS3

The examples in this article are all written in s...