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

WeChat applet date and time component (year, month, day, hour, and minute)

This article example shares the specific code of ...

Detailed explanation of the use of docker tag and docker push

Docker tag detailed explanation The use of the do...

CSS to achieve floating customer service effect

<div class="sideBar"> <div>...

Detailed explanation of the usage and difference between nohup and & in Linux

Example: We use the Python code loop_hello.py as ...

MySQL 5.7 generated column usage example analysis

This article uses examples to illustrate the usag...

Tutorial on installing MySQL 5.7.9 using RPM package under CentOS 7

Recorded MySQL 5.7.9 installation tutorial, share...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

How to implement a password strength detector in react

Table of contents Preface use Component Writing D...

Do designers need to learn to code?

Often, after a web design is completed, the desig...

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...

Detailed explanation of the basic use of react-navigation6.x routing library

Table of contents react-native project initializa...