Simply learn various SQL joins

Simply learn various SQL joins

The SQL JOIN clause is used to join rows from two or more tables based on the common fields between those tables.

The most common JOIN types: SQL INNER JOIN (simple JOIN), SQL LEFT JOIN, SQL RIGHT JOIN, SQL FULL JOIN, the former is an inner join, and the latter three are outer joins.

Suppose we have two tables, Table A is the table on the left and Table B is the table on the right.

id name
1 Google
2 Taobao
3 Weibo
4 Facebook
id address
1 USA
5 China
3 China
6 USA

INNER JOIN

An inner join is the most common type of join and only joins matching rows.

inner join syntax

select column_name(s)
from table 1
INNER JOIN table 2
ON
table 1.column_name = table 2.column_name

Note : INNER JOIN is the same as JOIN

The result set produced by INNER JOIN is the intersection of 1 and 2.

select * from Table A inner join Table B
on Table A.id=Table B.id

The output of executing the above SQL is as follows:

id name address
1 Google USA
3 Weibo China

LEFT JOIN

LEFT JOIN returns all rows of the left table and rows of the right table that meet the ON condition. If a row of the left table has no match in the right table, the corresponding data in the right table for this row is replaced by NULL.

LEFT JOIN Syntax

select column_name(s)
from table 1
LEFT JOIN table 2
ON table 1.column_name = table 2.column_name

Note: In some databases, LEFT JOIN is called LEFT OUTER JOIN

LEFT JOIN produces a complete set of table 1, and the matching values ​​in table 2 are replaced by null values ​​if there is no match.

select * from Table A left join Table B
on Table A.id=Table B.id

The output of executing the above SQL is as follows:

id name address
1 Google USA
2 Taobao null
3 Weibo China
4 Facebook null

RIGHT JOIN

RIGHT JOIN returns all rows of the right table and rows of the left table that meet the ON condition. If a row of the right table has no match in the left table, the corresponding data in the left table is replaced by NULL.

RIGHT JOIN Syntax

select column_name(s)
from table 1
RIGHT JOIN table 2
ON table 1.column_name = table 2.column_name

Note: In some databases, RIGHT JOIN is called RIGHT OUTER JOIN

RIGHT JOIN produces a complete set of table 2, and the matching values ​​in table 1 are replaced by null values ​​if there is no match.

select * from Table A right join Table B
on Table A.id=Table B.id

The output of executing the above SQL is as follows:

id name address
1 Google USA
5 null China
3 Weibo China
6 null USA

FULL OUTER JOIN

A FULL JOIN returns all rows from both the left and right tables. If a row in one table does not have a matching row in the other table, the opposite row is replaced with NULL.

FULL OUTER JOIN Syntax

select column_name(s)
from table 1
FULL OUTER JOIN table 2
ON table 1.column_name = table 2.column_name 

FULL OUTER JOIN produces the union of 1 and 2. However, it should be noted that for records that do not have a match, null will be used as the value.

select * from Table A full outer join Table B
on Table A.id=Table B.id

The output of executing the above SQL is as follows:

id name address
1 Google USA
2 Taobao null
3 Weibo China
4 Facebook null
5 null China
6 null USA

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Analysis of statement execution order of sql and MySQL
  • Detailed explanation of SQL statement execution order
  • SQL statement execution order graphic description
  • Execution order of SELECT statements in SQL Server
  • Complete execution sequence of SQL Select statements
  • Query process and optimization method of (JOIN/ORDER BY) statement in MySQL
  • Detailed explanation of the differences between SQL joint query inner join, outer join and cross join
  • Introduction to the difference between on and where conditions in MySQL left join operation
  • Detailed explanation of the difference between left join and on and where condition keywords in SQL
  • Analysis of MySQL multiple left join query usage

<<:  Detailed explanation of Windows time server configuration method

>>:  Example of how to implement local fuzzy search function in front-end JavaScript

Recommend

Solutions to browser interpretation differences in size and width and height in CSS

Let’s look at an example first Copy code The code ...

Browser compatibility summary of common CSS properties (recommended)

Why do we need to summarize the browser compatibi...

How to install PHP7.4 and Nginx on Centos

Prepare 1. Download the required installation pac...

Detailed explanation of Vue custom instructions

Table of contents Vue custom directive Custom dir...

MySQL 8.0.12 installation and configuration graphic tutorial

Recorded the download and installation tutorial o...

Detailed explanation of Angular data binding and its implementation

Table of contents Preface What is data binding? T...

How to implement paging query in MySQL

SQL paging query:background In the company's ...

SystemC environment configuration method under Linux system

The following is the configuration method under c...

Vue implements image drag and drop function

This article example shares the specific code of ...

HTML table markup tutorial (2): table border attributes BORDER

By default, the border of the table is 0, and we ...

Summary of commonly used CSS encapsulation methods

1. pc-reset PC style initialization /* normalize....

Detailed tutorial on installing Docker on CentOS 8

1. Previous versions yum remove docker docker-cli...

How to use cursor triggers in MySQL

cursor The set of rows returned by the select que...

Detailed explanation of webpage screenshot function in Vue

Recently, there is a requirement for uploading pi...