Oracle/PLSQL: Joins

本文介绍了SQL中的JOIN操作,包括内联(INNER JOIN)和外联(OUTER JOIN)。内联返回两个表中匹配的行,而外联则返回一个表的所有行以及另一个表中匹配的行。通过具体的例子展示了如何使用这些JOIN类型,并解释了它们在实际数据查询中的应用。
摘要由CSDN通过智能技术生成

SQL: Joins

An SQL join is used to combine rows from multiple tables. An SQL join is performed whenever two or more tables is listed in the SQL FROM clause of an SQL statement.

There are different kinds of SQL joins. Let's take a look at a few examples.

SQL Inner Join (simple join)

Chances are, you've already written an SQL statement that uses an SQL inner join. It is the most common type of SQL join. SQL inner joins return all rows from multiple tables where the join condition is met.

For example,

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date

FROM suppliers, orders

WHERE suppliers.supplier_id = orders.supplier_id;

This SQL inner join example would return all rows from the suppliers and orders tables where there is a matching supplier_id value in both the suppliers and orders tables.

Let's look at some data to explain how SQL inner joins work:

We have a table called suppliers with two fields (supplier_id and supplier_ name). It contains the following data:

supplier_id

supplier_name

10000

IBM

10001

Hewlett Packard

10002

Microsoft

10003

NVIDIA

We have another table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:

order_id

supplier_id

order_date

500125

10000

2003/05/12

500126

10001

2003/05/13

If we run the SQL statement (that contains an inner join) below:

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date

FROM suppliers, orders

WHERE suppliers.supplier_id = orders.supplier_id;

Our result set would look like this:

supplier_id

name

order_date

10000

IBM

2003/05/12

10001

Hewlett Packard

2003/05/13

The rows for Microsoft and NVIDIA from the supplier table would be omitted, since the supplier_id's 10002 and 10003 do not exist in both tables.

Outer Join

Another type of join is called an SQL outer join. This type of join returns all rows from one table and only those rows from a secondary table where the joined fields are equal (join condition is met).

For example,

select suppliers.supplier_id, suppliers.supplier_name, orders.order_date

from suppliers, orders

where suppliers.supplier_id = orders.supplier_id(+);

This SQL outer join example would return all rows from the suppliers table and only those rows from the orders table where the joined fields are equal.

The (+) after the orders.supplier_id field indicates that, if a supplier_id value in the suppliers table does not exist in the orders table, all fields in the orders table will display as <null> in the result set.

The above SQL outer join statement could also be written as follows:

select suppliers.supplier_id, suppliers.supplier_name, orders.order_date

from suppliers, orders

where orders.supplier_id(+) = suppliers.supplier_id;

Let's look at some data to explain how SQL outer joins work:

We have a table called suppliers with two fields (supplier_id and name). It contains the following data:

supplier_id

supplier_name

10000

IBM

10001

Hewlett Packard

10002

Microsoft

10003

NVIDIA

We have a second table called orders with three fields (order_id, supplier_id, and order_date). It contains the following data:

order_id

supplier_id

order_date

500125

10000

2003/05/12

500126

10001

2003/05/13

If we run the SQL statement (that contains an outer join) below:

select suppliers.supplier_id, suppliers.supplier_name, orders.order_date

from suppliers, orders

where suppliers.supplier_id = orders.supplier_id(+);

Our result set would look like this:

supplier_id

supplier_name

order_date

10000

IBM

2003/05/12

10001

Hewlett Packard

2003/05/13

10002

Microsoft

<null>

10003

NVIDIA

<null>

The rows for Microsoft and NVIDIA would be included because an SQL outer join was used. However, you will notice that the order_date field for those records contains a <null> value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值