Inner and Outer Join SQL statement

本文详细介绍了SQL中的三种连接类型:内连接(INNER JOIN)、左外连接(LEFT OUTER JOIN)和右外连接(RIGHT OUTER JOIN)。通过实例展示了如何使用这些连接来检索不同场景下的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

INNER JOIN: Retrieves customers with orders only. For example, you want to determine the amount ordered by each customer and you only want to see those who have ordered something

SELECT Customers.*, Orders.*
FROM Customers INNER JOIN Orders ON Customers.CustomerID =
Orders.CustomerID

------------------------------------------------------------------------

LEFT OUTER JOIN: Retrieves all customers with or without orders. Order data for customers without orders appears as NULL values. For example, you want to determine the amount ordered by each customer and you need to see who has not ordered anything as well. You can also see the LEFT OUTER JOIN as a mirror image of the RIGHT OUTER JOIN if you switch the side of each table.

SELECT Customers.*, Orders.*
FROM Customers LEFT OUTER JOIN Orders ON Customers.CustomerID =
Orders.CustomerID

------------------------------------------------------------------------

RIGHT OUTER JOIN: Retrieves all orders with or without matching customer records. Customer data for orders without customers appears as NULL values. For example, you want to determine if there are any orders in the data with undefined CustomerID values (say, after a conversion or something like it). You can also see the RIGHT OUTER JOIN as a mirror image of the LEFT OUTER JOIN if you switch the side of each table.

SELECT Customers.*, Orders.*
FROM Customers RIGHT OUTER JOIN Orders ON Customers.CustomerID =
Orders.CustomerID

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值