Home Database SQL Understand inner joins, left outer joins, and right outer joins of sql statements

Understand inner joins, left outer joins, and right outer joins of sql statements

Jan 15, 2021 am 10:20 AM

Understand inner joins, left outer joins, and right outer joins of sql statements

Recommended (free): SQL Tutorial

When you first learn the database, do you have some doubts about the connection query in DQL? Do you know when and what kind of connection query should be used in what scenario?
Understand inner joins, left outer joins, and right outer joins of sql statements
Don’t worry, let me introduce to you my understanding of the characteristics and application scenarios of inner joins, left outer joins, and right outer joins for your reference.

The following code demonstrations are based on the name table and country table.
name table

##5Zhao Feiyan
id name
1 西士
2 Yang Yuhuan
3 Diao Chan
4 Wang Zhaojun

country table

idcountryA_ID1Yue people during the Spring and Autumn Period12 A native of Yongle, Puzhou in the Tang Dynasty23A native of Xinzhou, Shanxi in the late Eastern Han Dynasty34People from Zigui, Nanjun in the Western Han Dynasty4##5
Warring States Period 6
Note:
1) There is a relationship between b table A_ID and a table a_id

2) The connection query uses association conditions to remove mismatches Otherwise, the data will have a Cartesian product

1) Inner join

语法:	select  要查询的字段 from 表名1 inner join 表名2 on 表1.字段 = 表2.字段;
	inner join 可简写为 逗号,
内连接特点:
	只会保留完全符合on后条件的数据
应用场景:
	如果两张表有外键关系可以使用内链接,因为通过内链接每一条只能返回单条记录
Copy after login
select * from name n inner join country c on n.id = c.N_ID;
Copy after login

id##1西士1元国人12Yang Yuhuan2People from Yongle, Puzhou, Tang Dynasty2Diao ChanWang Zhaojun2) Left outer join
name id country N_ID
##3
3 Eastern Han Dynasty A native of Xinzhou, Shanxi in the last years 3 4
4 A native of Zigui, Nanjun during the Western Han Dynasty 4
语法:	select 要查询的字段 from 表1 left outer join 表2 on 表1.字段 = 表2.字段;
	outer 可省略
左外连接特点:
	以左表为主,会保留左表中不符合on后条件的数据
应用场景:
	只有部分记录可以从表2中查到,但表1想要显示所有记录,就可以和表2通过左外连接查询。
Copy after login
rrree

id name1西士Yang Yuhuan##3Diao Chan3 People from Xinzhou, Shanxi Province in the late Eastern Han Dynasty34null
select * from name n left join country c on n.id=c.N_ID;
Copy after login
rrree
id country N_ID
1 元国人 1 2
2 People from Yongle, Puzhou, Tang Dynasty 2
##4 Wang Zhaojun
南君 Zigui people during the Western Han Dynasty 4 5 Zhao Feiyan
null null 3) Right outer join

id

nameid##1西士1元国人1People from Yongle, Puzhou, Tang Dynasty##3Diao Chan3People from Xinzhou, Shanxi in the late Eastern Han Dynasty34##nullnull5Warring States Period64) Full link
语法:	select * from 表1 right outer join 表2 on 表1.字段 = 表2.字段;
	outer 可省略
右外连接特点:
	以右表为主,会保留右表中不符合on后条件的数据
应用场景:
	和左外连接相反
	只有部分记录可以从表1中查询到,但表2想要显示所有记录, 就可以和表1通过右外连接查询。
Copy after login
select * from name n right join country c on n.id=c.N_ID;
Copy after login
idname
country N_ID
##2 Yang Yuhuan 2
2
##4 Wang Zhaojun 4 A native of Zigui, Nanjun during the Western Han Dynasty

idcountry

N_ID1##2Yang Yuhuan2People from Yongle, Puzhou, Tang Dynasty2##345null If there is anything wrong, please point it out~sql column~
##1 xishi 1 元国人
Diao Chan 3 Late Eastern Han Dynasty Shanxi Xinzhou people 3
王赵君 4 Western Han Dynasty A native of Zigui, Nanjun 4
Zhao Feiyan null null null
null 5 Warring States Period 6
Note: This syntax is not applicable in MySql Okay, I have finished the introduction. I wonder if it helps you? For more related knowledge, please pay attention to the

The above is the detailed content of Understand inner joins, left outer joins, and right outer joins of sql statements. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
SQL: The Commands, MySQL: The Engine SQL: The Commands, MySQL: The Engine Apr 15, 2025 am 12:04 AM

SQL commands are divided into five categories in MySQL: DQL, DDL, DML, DCL and TCL, and are used to define, operate and control database data. MySQL processes SQL commands through lexical analysis, syntax analysis, optimization and execution, and uses index and query optimizers to improve performance. Examples of usage include SELECT for data queries and JOIN for multi-table operations. Common errors include syntax, logic, and performance issues, and optimization strategies include using indexes, optimizing queries, and choosing the right storage engine.

SQL and MySQL: Understanding the Core Differences SQL and MySQL: Understanding the Core Differences Apr 17, 2025 am 12:03 AM

SQL is a standard language for managing relational databases, while MySQL is a specific database management system. SQL provides a unified syntax and is suitable for a variety of databases; MySQL is lightweight and open source, with stable performance but has bottlenecks in big data processing.

SQL: Making Data Management Accessible to All SQL: Making Data Management Accessible to All Apr 12, 2025 am 12:14 AM

SQLmakesdatamanagementaccessibletoallbyprovidingasimpleyetpowerfultoolsetforqueryingandmanagingdatabases.1)Itworkswithrelationaldatabases,allowinguserstospecifywhattheywanttodowiththedata.2)SQL'sstrengthliesinfiltering,sorting,andjoiningdataacrosstab

SQL for Data Analysis: Advanced Techniques for Business Intelligence SQL for Data Analysis: Advanced Techniques for Business Intelligence Apr 14, 2025 am 12:02 AM

Advanced query skills in SQL include subqueries, window functions, CTEs and complex JOINs, which can handle complex data analysis requirements. 1) Subquery is used to find the employees with the highest salary in each department. 2) Window functions and CTE are used to analyze employee salary growth trends. 3) Performance optimization strategies include index optimization, query rewriting and using partition tables.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

SQL: How to Overcome the Learning Hurdles SQL: How to Overcome the Learning Hurdles Apr 26, 2025 am 12:25 AM

To become an SQL expert, you should master the following strategies: 1. Understand the basic concepts of databases, such as tables, rows, columns, and indexes. 2. Learn the core concepts and working principles of SQL, including parsing, optimization and execution processes. 3. Proficient in basic and advanced SQL operations, such as CRUD, complex queries and window functions. 4. Master debugging skills and use the EXPLAIN command to optimize query performance. 5. Overcome learning challenges through practice, utilizing learning resources, attaching importance to performance optimization and maintaining curiosity.

SQL and MySQL: A Beginner's Guide to Data Management SQL and MySQL: A Beginner's Guide to Data Management Apr 29, 2025 am 12:50 AM

The difference between SQL and MySQL is that SQL is a language used to manage and operate relational databases, while MySQL is an open source database management system that implements these operations. 1) SQL allows users to define, operate and query data, and implement it through commands such as CREATETABLE, INSERT, SELECT, etc. 2) MySQL, as an RDBMS, supports these SQL commands and provides high performance and reliability. 3) The working principle of SQL is based on relational algebra, and MySQL optimizes performance through mechanisms such as query optimizers and indexes.

The Importance of SQL: Data Management in the Digital Age The Importance of SQL: Data Management in the Digital Age Apr 23, 2025 am 12:01 AM

SQL's role in data management is to efficiently process and analyze data through query, insert, update and delete operations. 1.SQL is a declarative language that allows users to talk to databases in a structured way. 2. Usage examples include basic SELECT queries and advanced JOIN operations. 3. Common errors such as forgetting the WHERE clause or misusing JOIN, you can debug through the EXPLAIN command. 4. Performance optimization involves the use of indexes and following best practices such as code readability and maintainability.

See all articles