> 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.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 第 7 章 查詢

**目錄**

* [7.1. 概觀](https://docs.postgresql.tw/19/the-sql-language/queries/queries-overview.md)
* [7.2. 資料表運算式](https://docs.postgresql.tw/19/the-sql-language/queries/queries-table-expressions.md)
  * [7.2.1. `FROM` 子句](https://docs.postgresql.tw/19/the-sql-language/queries/queries-table-expressions.md#queries-from)
  * [7.2.2. `WHERE` 子句](https://docs.postgresql.tw/19/the-sql-language/queries/queries-table-expressions.md#queries-where)
  * [7.2.3. `GROUP BY` 與 `HAVING` 子句](https://docs.postgresql.tw/19/the-sql-language/queries/queries-table-expressions.md#queries-group)
  * [7.2.4. `GROUPING SETS`、`CUBE` 與 `ROLLUP`](https://docs.postgresql.tw/19/the-sql-language/queries/queries-table-expressions.md#queries-grouping-sets)
  * [7.2.5. Window 函式的處理](https://docs.postgresql.tw/19/the-sql-language/queries/queries-table-expressions.md#queries-window)
* [7.3. 選取清單](https://docs.postgresql.tw/19/the-sql-language/queries/queries-select-lists.md)
  * [7.3.1. 選取清單項目](https://docs.postgresql.tw/19/the-sql-language/queries/queries-select-lists.md#queries-select-list-items)
  * [7.3.2. 欄位標籤](https://docs.postgresql.tw/19/the-sql-language/queries/queries-select-lists.md#queries-column-labels)
  * [7.3.3. `DISTINCT`](https://docs.postgresql.tw/19/the-sql-language/queries/queries-select-lists.md#queries-distinct)
* [7.4. 組合查詢（`UNION`、`INTERSECT`、`EXCEPT`）](https://docs.postgresql.tw/19/the-sql-language/queries/queries-union.md)
* [7.5. 排序資料列（`ORDER BY`）](https://docs.postgresql.tw/19/the-sql-language/queries/queries-order.md)
* [7.6. `LIMIT` 與 `OFFSET`](https://docs.postgresql.tw/19/the-sql-language/queries/queries-limit.md)
* [7.7. `VALUES` 清單](https://docs.postgresql.tw/19/the-sql-language/queries/queries-values.md)
* [7.8. `WITH` 查詢（通用資料表運算式）](https://docs.postgresql.tw/19/the-sql-language/queries/queries-with.md)
  * [7.8.1. `WITH` 中的 `SELECT`](https://docs.postgresql.tw/19/the-sql-language/queries/queries-with.md#queries-with-select)
  * [7.8.2. 遞迴查詢](https://docs.postgresql.tw/19/the-sql-language/queries/queries-with.md#queries-with-recursive)
  * [7.8.3. 通用資料表運算式的具體化](https://docs.postgresql.tw/19/the-sql-language/queries/queries-with.md#queries-with-cte-materialization)
  * [7.8.4. `WITH` 中的資料修改陳述式](https://docs.postgresql.tw/19/the-sql-language/queries/queries-with.md#queries-with-modifying)

前面幾章說明了如何建立資料表、如何填入資料，以及如何操作這些資料。現在我們終於要討論如何從資料庫中取出資料。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/queries.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.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.
