> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/the-sql-language/queries/queries-union.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 7.4. 組合查詢（UNION、INTERSECT、EXCEPT） \#

### 7.4. 組合查詢（`UNION`、`INTERSECT`、`EXCEPT`） [#](#queries-union) <a href="#queries-union" id="queries-union"></a>

兩個查詢的結果可以使用聯集、交集與差集等集合運算組合起來。語法是

```

query1 UNION [ALL] query2
query1 INTERSECT [ALL] query2
query1 EXCEPT [ALL] query2
```

其中 *`query1`* 與 *`query2`* 是可以使用到目前為止所討論之任何功能的查詢。

`UNION` 實際上是將 *`query2`* 的結果附加到 *`query1`* 的結果之後（但並不保證資料列實際上是以這個順序回傳的）。此外，除非使用 `UNION ALL`，否則它會以與 `DISTINCT` 相同的方式，從結果中排除重複的資料列。

`INTERSECT` 會回傳同時出現在 *`query1`* 結果與 *`query2`* 結果中的所有資料列。除非使用 `INTERSECT ALL`，否則會排除重複的資料列。

`EXCEPT` 會回傳出現在 *`query1`* 結果中、但不在 *`query2`* 結果中的所有資料列。（這有時稱為兩個查詢的*差集*（difference）。）同樣地，除非使用 `EXCEPT ALL`，否則會排除重複的資料列。

要計算兩個查詢的聯集、交集或差集，這兩個查詢必須是「聯集相容」（union compatible）的，也就是說，它們回傳相同數量的欄位，而且對應的欄位具有相容的資料型別，如[第 10.5 節](https://docs.postgresql.tw/19/the-sql-language/typeconv/typeconv-union-case.md)所述。

集合運算可以組合使用，例如

```

query1 UNION query2 EXCEPT query3
```

這等同於

```

(query1 UNION query2) EXCEPT query3
```

如這裡所示，你可以使用括號來控制求值的順序。沒有括號時，`UNION` 與 `EXCEPT` 是由左至右結合的，但 `INTERSECT` 的結合力比這兩個運算子強。因此

```

query1 UNION query2 INTERSECT query3
```

的意思是

```

query1 UNION (query2 INTERSECT query3)
```

你也可以用括號括住個別的 *`query`*。如果 *`query`* 需要使用後續各節所討論的任何子句（例如 `LIMIT`），這一點就很重要。沒有括號的話，你會得到語法錯誤，否則該子句會被理解為套用在集合運算的輸出上，而不是其中一個輸入上。例如，

```

SELECT a FROM b UNION SELECT x FROM y LIMIT 10
```

是可以接受的，但它的意思是

```

(SELECT a FROM b UNION SELECT x FROM y) LIMIT 10
```

而不是

```

SELECT a FROM b UNION (SELECT x FROM y LIMIT 10)
```

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/queries-union.html)（原文版本：18.6；核對日期：2026-09-11）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/the-sql-language/queries/queries-union.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
