Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
檢索過程或從資料庫檢索資料的命令稱之為查詢。在 SQL 中,SELECT 命令用於進行條件查詢。 SELECT 指令的一般語法是:
以下各節介紹了資料列表(select list),資料表和排序規則的詳細資訊。由於 WITH 查詢是高級功能,因此最後再介紹。
一種簡單的查詢形式如下:
假設有一個名稱為 table1 的資料表,該指令會將取出 table1 中的所有資料表和所有用戶定義的欄位。(檢索的方法取決於用戶端的應用程序,例如,psql 程序將在屏幕上顯示一個 ASCII-art 表格,而用戶端的程式函式庫將提供從查詢結果中提取單一值的功能。選擇資料列表定義「*」表示由資料表表示式所產生的所有欄位。篩選列表可以是可用欄位的子集或使用欄位進行計算。例如,如果 table1 具有名稱為 a,b 和 c(也許是其他)的欄位,則可以進行以下查詢:
(假設 b 和 c 是數字型別)。更多細節詳見 7.3 節。
FROM table1是一種簡單的資料表表示式:它只讀取一個資料表。一般來說,資料表表示式可以是一般的資料表,交叉查詢和子查詢的複雜結構。但是,你也可以完全省略資料表表示式,並使用 SELECT 指令作為計算機:
使用資料列表中的表達式產生變動的結果,是更為常用的方式。例如,你可以這樣呼叫一個函數:
前面的章節解釋了如何建立資料表,如何填入資料以及如何操作這些資料。現在我們是時候討論如何從資料庫中檢索資料了。
兩個查詢的結果可以使用集合操作聯、交集和差集來組合。其語法為:
query1 和 query2 是到目前為止討論過的任何查詢功能。集合操作也可以巢狀也可以連接,例如:
會如下方式執行:
UNION 將 query2 的結果有效率地附加到 query1 的結果中(但不能保證這是實際回傳資料列的次序)。此外,除非使用了UNION ALL,否則它將以與 DISTINCT相同的方式從結果中消除重複的資料列。
INTERSECT 返回 query1 的結果和 query2 的結果中所有共同的資料列。除非使用 INTERSECT ALL,否則會刪除重複的資料列。
EXCEPT 回傳 query1 的結果中但不包含在 query2 的結果中的所有資料列。(這有時被稱為兩個查詢之間的差集。)同樣地,除非使用 EXCEPT ALL,否則重複資料列將被刪除。
為了計算兩個查詢的聯集、交集或差集,兩個查詢必須是「union compatible」,這意味著它們回傳相同數量的欄位,相應的欄位具有相容的資料型別,如 10.5 節所述。
如前一節所述,SELECT 指令中的資料示表表示式透過各種可能地組合資料表、view、消除資料列、分組等來建構中介的虛擬資料表。這個資料表最終會被傳遞給資料列表的處理。資料列表確認中介資料表的哪些欄位是實際上要輸出的。
最簡單的選擇列表是*,它表示資料表表示式產生的所有欄位。否則,資料列表是逗號分隔的參數表示式列表(如第 4.2 節中所定義的)。例如,它可能是欄位名稱的列表:
欄位名稱 a、b 和 c 是 FROM 子句中資料表的欄位的實際名稱,或者是由第 7.2.1.2 節中所賦予它們的別名。資料列表中可用的命名空間與 WHERE 子句中的命名空間相同,除非是使用分組查詢,在這種情況下,它與 HAVING 子句中的相同。
如果多個資料表具有相同名稱的欄位,則還必須加上資料表的名稱,如下所示:
處理多個資料表時,查詢特定資料表的所有欄位也是可以的:
有關 table_name.* 表示法的更多信息,請參閱第 8.16.5 節。
如果在資料列表中使用任意值表示式,則概念上是它將新的虛擬欄位加到回傳的資料表中。參數表示式對每個結果資料列計算一次,將該資料列的值替換為任何欄位引用。但是資料列表中的表示式不必引用 FROM 子句的資料表表示式中的任何欄位;例如,它們可以是常數算術表示式。
資料列表中的項目可以被分配用於後續處理的名稱,例如在 ORDER BY 子句中使用或由用戶端應用程序顯示。 例如:
如果沒有使用 AS 指定輸出欄位的名稱,系統將分配一個預設的欄位名稱。對於簡單欄位的引用,就是引用欄位的名稱。對於函數呼叫,就是函數的名稱。對於複雜的表示式,系統將會產成一個通用的名稱。
AS 關鍵字是選用的,但前提是新的欄位名稱不為任何PostgreSQL 關鍵字(請參閱附錄C)。為避免與關鍵字意外撞名,你可以對欄位名稱使用雙引號。例如,VALUE 是一個關鍵字,所以就不能這樣使用:
但這樣就可以了:
為了防止未來可能增加的關鍵字,建議你習慣使用 AS 或總是在欄位名稱使用雙引號。
注意這裡輸出欄位的命名與 FROM 子句中的命名不同(參閱第 7.2.1.2 節)。可以重新命名相同的欄位兩次,但在資料列表中分配的名稱是將要回傳的名稱。
DISTINCT
在處理了資料列表之後,結果資料表可以選擇性地消除重複的資料列。 DISTINCT 關鍵字在 SELECT 之後直接寫入以指定這個動作:
(如果不是 DISTINCT,而是關鍵字 ALL,可用於指定保留所有資料列的預設行為。)
顯然,如果至少有一個欄位值不同,則兩個資料列就會被認為是不同的。 在這個比較中,空值(null)被認為是相等的。
或者,使用表示式可以指定資料列如何被認為是不同的:
這裡表示式是一個任意的運算表示式,對所有資料列進行求值運算。所有表示式相等的一組資料列被認為是重複的,並且只有該組的第一個資料列會被保留在輸出中。請注意,集合中的「第一行」是不可預知的,除非查詢按足夠的欄位進行排序,以保證進到 DISTINCT 過濾器的資料列是唯一排序。(在 ORDER BY 排序後才進行 DISTINCT ON 處理。)
DISTINCT ON 子句不是SQL標準的一部分,有時被認為是不好的樣式,因為其結果有潛在的不確定性。透過在 FROM 中智慧地使用 GROUP BY 和子查詢,可以避免這種結構,但這卻往往是最方便的選擇。
LIMIT 和 OFFSET 允許你只回傳由查詢生成的一部分資料列:
如果給了一個限制的數量,那麼只有那個數目的資料列會回傳(如果查詢本身產生較少的資料列,則可能會少一些)。LIMIT ALL 與省略 LIMIT 子句相同,也如同 LIMIT 的參數為 NULL。
OFFSET 指的是在開始回傳資料列之前跳過那麼多少資料列。OFFSET 0 與忽略 OFFSET 子句相同,就像使用 NULL 參數的 OFFSET 一樣。
如果同時出現 OFFSET 和 LIMIT,則在開始計算回傳的LIMIT 資料列之前,先跳過 OFFSET 數量的資料列。
使用 LIMIT 時,運用 ORDER BY 子句將結果資料列限制為唯一順序非常重要。否則,你會得到一個不可預知的查詢資料列的子集。你可能會查詢第十到第二十個資料列,但是第十到第二十個資料列是按什麼順序排列的?次序是未知的,除非你指定 ORDER BY。
查詢最佳化在產生查詢計劃時會將 LIMIT 考慮在內,所以根據你給的 LIMIT 和 OFFSET,你很可能會得到不同的計劃(產生不同的資料列順序)。因此,使用不同的 LIMIT / OFFSET 值來選擇查詢結果的不同子集將導致不一致的結果,除非使用 ORDER BY 強制執行可預測的結果排序。這不是一個錯誤;這是一種事實上的結果,即 SQL 不保證以任何特定順序傳遞查詢的結果,除非使用 ORDER BY 來約束順序。
由 OFFSET 子句跳過的資料列仍然需要在伺服器內計算。因此一個大的 OFFSET 可能是低效率的。
在查詢產生了一個輸出資料表(處理了資料列表之後)之後,可以對其資料列進行排序。如果未選擇排序,則資料列將以未指定的順序回傳。在這種情況下的實際順序將取決於資料掃描和交叉查詢類型以及磁碟上的順序,但不能依賴它。只有明確選擇了排序方式,才能保證特定的輸出排序。
以 ORDER BY 子句指定排序順序:
排序表示式可以在查詢的資料列表中有效的任何表示式。 一個例子是:
當指定多個表示式時,後面的表示式用於前面表示式都相同的資料進行排序。每個表示式可以跟隨一個選擇性的 ASC 或 DESC 關鍵字來設定排序方向為升冪或降冪。 ASC 排序是預設的選項。升冪首先放置較小的值,其中「較小」是根據「<」運算元定義的。 同樣,降冪也是由「>」運算元決定的。
NULLS FIRST 和 NULLS LAST 選項可用於確定在排序順序中是否出現空值出現在非空值之前或之後。預設情況下,空值排序大於任何非空值;也就是 NULLS FIRST 是 DESC 選項的預設值,否則就是 NULLS LAST。
請注意,排序選項是針對每個排序欄位獨立考慮的。例如 ORDER BY x, y DESC 是指 ORDER BY x ASC, y DESC,它與 ORDER BY x DESC, y DESC 不同。
排序表示式也可以是輸出欄位的欄位標籤或編號,如下所示:
兩者都按第一個輸出欄位排序。請注意,輸出欄位名稱必須獨立,也就是說,不能在表示式中使用 - 例如,這樣是不正確的:
這種限制是為了減少歧義。 即使 ORDER BY 項目是一個簡單的名字,可以匹配輸出欄位名稱或者資料表表示式中的一項,這仍然是會混淆的。在這種情況下請使用輸出欄位。如果您使用 AS 來重新命名輸出欄位以匹配其他資料表欄位的名稱,只會導致混淆。
可以將 ORDER BY 應用於 UNION、INTERSECT 或 EXCEPT 組合的結果,但在這種情況下,只允許按輸出欄位名稱或數字進行排序,而不能使用表示式進行排序。
WITH
provides a way to write auxiliary statements for use in a larger query. These statements, which are often referred to as Common Table Expressions or CTEs, can be thought of as defining temporary tables that exist just for one query. Each auxiliary statement in a WITH
clause can be a SELECT
, INSERT
, UPDATE
, or DELETE
; and the WITH
clause itself is attached to a primary statement that can also be a SELECT
, INSERT
, UPDATE
, or DELETE
.
SELECT
in WITH
The basic value of SELECT
in WITH
is to break down complicated queries into simpler parts. An example is:
which displays per-product sales totals in only the top sales regions. The WITH
clause defines two auxiliary statements named regional_sales
and top_regions
, where the output of regional_sales
is used in top_regions
and the output of top_regions
is used in the primary SELECT
query. This example could have been written without WITH
, but we'd have needed two levels of nested sub-SELECT
s. It's a bit easier to follow this way.
The optional RECURSIVE
modifier changes WITH
from a mere syntactic convenience into a feature that accomplishes things not otherwise possible in standard SQL. Using RECURSIVE
, a WITH
query can refer to its own output. A very simple example is this query to sum the integers from 1 through 100:
The general form of a recursive WITH
query is always a non-recursive term, then UNION
(or UNION ALL
), then a recursive term, where only the recursive term can contain a reference to the query's own output. Such a query is executed as follows:
Recursive Query Evaluation
Evaluate the non-recursive term. For UNION
(but not UNION ALL
), discard duplicate rows. Include all remaining rows in the result of the recursive query, and also place them in a temporary working table.
So long as the working table is not empty, repeat these steps:
Evaluate the recursive term, substituting the current contents of the working table for the recursive self-reference. For UNION
(but not UNION ALL
), discard duplicate rows and rows that duplicate any previous result row. Include all remaining rows in the result of the recursive query, and also place them in a temporary intermediate table.
Replace the contents of the working table with the contents of the intermediate table, then empty the intermediate table.
Strictly speaking, this process is iteration not recursion, but RECURSIVE
is the terminology chosen by the SQL standards committee.
In the example above, the working table has just a single row in each step, and it takes on the values from 1 through 100 in successive steps. In the 100th step, there is no output because of the WHERE
clause, and so the query terminates.
Recursive queries are typically used to deal with hierarchical or tree-structured data. A useful example is this query to find all the direct and indirect sub-parts of a product, given only a table that shows immediate inclusions:
When working with recursive queries it is important to be sure that the recursive part of the query will eventually return no tuples, or else the query will loop indefinitely. Sometimes, using UNION
instead of UNION ALL
can accomplish this by discarding rows that duplicate previous output rows. However, often a cycle does not involve output rows that are completely duplicate: it may be necessary to check just one or a few fields to see if the same point has been reached before. The standard method for handling such situations is to compute an array of the already-visited values. For example, consider the following query that searches a table graph
using a link
field:
This query will loop if the link
relationships contain cycles. Because we require a “depth” output, just changing UNION ALL
to UNION
would not eliminate the looping. Instead we need to recognize whether we have reached the same row again while following a particular path of links. We add two columns path
and cycle
to the loop-prone query:
Aside from preventing cycles, the array value is often useful in its own right as representing the “path” taken to reach any particular row.
In the general case where more than one field needs to be checked to recognize a cycle, use an array of rows. For example, if we needed to compare fields f1
and f2
:
Omit the ROW()
syntax in the common case where only one field needs to be checked to recognize a cycle. This allows a simple array rather than a composite-type array to be used, gaining efficiency.
The recursive query evaluation algorithm produces its output in breadth-first search order. You can display the results in depth-first search order by making the outer query ORDER BY
a “path” column constructed in this way.
A helpful trick for testing queries when you are not certain if they might loop is to place a LIMIT
in the parent query. For example, this query would loop forever without the LIMIT
:
This works because PostgreSQL's implementation evaluates only as many rows of a WITH
query as are actually fetched by the parent query. Using this trick in production is not recommended, because other systems might work differently. Also, it usually won't work if you make the outer query sort the recursive query's results or join them to some other table, because in such cases the outer query will usually try to fetch all of the WITH
query's output anyway.
A useful property of WITH
queries is that they are normally evaluated only once per execution of the parent query, even if they are referred to more than once by the parent query or sibling WITH
queries. Thus, expensive calculations that are needed in multiple places can be placed within a WITH
query to avoid redundant work. Another possible application is to prevent unwanted multiple evaluations of functions with side-effects. However, the other side of this coin is that the optimizer is not able to push restrictions from the parent query down into a multiply-referenced WITH
query, since that might affect all uses of the WITH
query's output when it should affect only one. The multiply-referenced WITH
query will be evaluated as written, without suppression of rows that the parent query might discard afterwards. (But, as mentioned above, evaluation might stop early if the reference(s) to the query demand only a limited number of rows.)
但是,如果 WITH 查詢是非遞迴且不會在執行中變動的(即它是一個不包含 volatile 函數的 SELECT),則可以將其合併到父查詢之中,從而可以對兩個查詢等級進行聯合語法最佳化。預設情況下,如果父查詢僅引用一次 WITH 語句,而不是多次引用 WITH 一次查詢,則會觸發這個機制。您可以透過指定 MATERIALIZED 強制執行 WITH 查詢的單獨計算,或者透過指定 NOT MATERIALIZED 強制執行將其合併到父查詢中來覆蓋該查詢計畫。後面一種選擇可能會冒著重複計算 WITH 查詢的風險,但如果 WITH 查詢的每次使用只需要 WITH 查詢全部輸出的一小部分,那麼它仍然可以節省成本。
A simple example of these rules is
This WITH
query will be folded, producing the same execution plan as
In particular, if there's an index on key
, it will probably be used to fetch just the rows having key = 123
. On the other hand, in
the WITH
query will be materialized, producing a temporary copy of big_table
that is then joined with itself — without benefit of any index. This query will be executed much more efficiently if written as
so that the parent query's restrictions can be applied directly to scans of big_table
.
An example where NOT MATERIALIZED
could be undesirable is
Here, materialization of the WITH
query ensures that very_expensive_function
is evaluated only once per table row, not twice.
The examples above only show WITH
being used with SELECT
, but it can be attached in the same way to INSERT
, UPDATE
, or DELETE
. In each case it effectively provides temporary table(s) that can be referred to in the main command.
WITH
You can use data-modifying statements (INSERT
, UPDATE
, or DELETE
) in WITH
. This allows you to perform several different operations in the same query. An example is:
This query effectively moves rows from products
to products_log
. The DELETE
in WITH
deletes the specified rows from products
, returning their contents by means of its RETURNING
clause; and then the primary query reads that output and inserts it into products_log
.
A fine point of the above example is that the WITH
clause is attached to the INSERT
, not the sub-SELECT
within the INSERT
. This is necessary because data-modifying statements are only allowed in WITH
clauses that are attached to the top-level statement. However, normal WITH
visibility rules apply, so it is possible to refer to the WITH
statement's output from the sub-SELECT
.
Data-modifying statements in WITH
usually have RETURNING
clauses (see Section 6.4), as shown in the example above. It is the output of the RETURNING
clause, not the target table of the data-modifying statement, that forms the temporary table that can be referred to by the rest of the query. If a data-modifying statement in WITH
lacks a RETURNING
clause, then it forms no temporary table and cannot be referred to in the rest of the query. Such a statement will be executed nonetheless. A not-particularly-useful example is:
This example would remove all rows from tables foo
and bar
. The number of affected rows reported to the client would only include rows removed from bar
.
Recursive self-references in data-modifying statements are not allowed. In some cases it is possible to work around this limitation by referring to the output of a recursive WITH
, for example:
This query would remove all direct and indirect subparts of a product.
Data-modifying statements in WITH
are executed exactly once, and always to completion, independently of whether the primary query reads all (or indeed any) of their output. Notice that this is different from the rule for SELECT
in WITH
: as stated in the previous section, execution of a SELECT
is carried only as far as the primary query demands its output.
The sub-statements in WITH
are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements in WITH
, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means that RETURNING
data is the only way to communicate changes between different WITH
sub-statements and the main query. An example of this is that in
the outer SELECT
would return the original prices before the action of the UPDATE
, while in
the outer SELECT
would return the updated data.
Trying to update the same row twice in a single statement is not supported. Only one of the modifications takes place, but it is not easy (and sometimes not possible) to reliably predict which one. This also applies to deleting a row that was already updated in the same statement: only the update is performed. Therefore you should generally avoid trying to modify a single row twice in a single statement. In particular avoid writing WITH
sub-statements that could affect the same rows changed by the main statement or a sibling sub-statement. The effects of such a statement will not be predictable.
At present, any table used as the target of a data-modifying statement in WITH
must not have a conditional rule, nor an ALSO
rule, nor an INSTEAD
rule that expands to multiple statements.
VALUES 提供了一種產生「靜態資料表」的方法,可以在查詢中使用,而不必實際創建和寫入磁碟上的資料表。其語法是
每個括號內的表示式列表在資料表中生成一個資料列。列表必須具有相同數量的元素(即資料表中的欄位數),並且每個列表中的對應條目必須具有兼容的資料型別。 分配給結果中每個欄位的實際資料型別,使用與 UNION 相同的規則來給定(請參閱)。
如下範例所示:
將回傳一個兩個欄位三個資料列的資料表。這實際上相當於:
預設情況下,PostgreSQL 會將名稱 column1、column2 等分配給 VALUES 資料表的欄位。欄位名稱並不是由 SQL 標準規定的,不同的資料庫系統會以不同的方式賦予,所以通常以資料表別名列表覆寫預設名稱會比較好,如下所示:
在語法上,VALUES 接在表示式列表之後被視為等同於:
並可以出現在任何一個 SELECT 可以使用的地方。例如,你可以將其用作為 UNION 的一部分,或者為其增加排序規則(ORDER BY、LIMIT 和 OFFSET)。在 INSERT 命令中,VALUES 最常來作為資料源,其次最常在子查詢。
關於更多訊息,請參閱 。
A table expression computes a table. The table expression contains a FROM
clause that is optionally followed by WHERE
, GROUP BY
, and HAVING
clauses. Trivial table expressions simply refer to a table on disk, a so-called base table, but more complex expressions can be used to modify or combine base tables in various ways.
The optional WHERE
, GROUP BY
, and HAVING
clauses in the table expression specify a pipeline of successive transformations performed on the table derived in the FROM
clause. All these transformations produce a virtual table that provides the rows that are passed to the select list to compute the output rows of the query.
FROM
ClauseThe FROM
Clause derives a table from one or more other tables given in a comma-separated table reference list.
A table reference can be a table name (possibly schema-qualified), or a derived table such as a subquery, a JOIN
construct, or complex combinations of these. If more than one table reference is listed in the FROM
clause, the tables are cross-joined (that is, the Cartesian product of their rows is formed; see below). The result of the FROM
list is an intermediate virtual table that can then be subject to transformations by the WHERE
, GROUP BY
, and HAVING
clauses and is finally the result of the overall table expression.
When a table reference names a table that is the parent of a table inheritance hierarchy, the table reference produces rows of not only that table but all of its descendant tables, unless the key word ONLY
precedes the table name. However, the reference produces only the columns that appear in the named table — any columns added in subtables are ignored.
Instead of writing ONLY
before the table name, you can write *
after the table name to explicitly specify that descendant tables are included. There is no real reason to use this syntax any more, because searching descendant tables is now always the default behavior. However, it is supported for compatibility with older releases.
7.2.1.1. Joined Tables
A joined table is a table derived from two other (real or derived) tables according to the rules of the particular join type. Inner, outer, and cross-joins are available. The general syntax of a joined table is
Joins of all types can be chained together, or nested: either or both T1
and T2
can be joined tables. Parentheses can be used around JOIN
clauses to control the join order. In the absence of parentheses, JOIN
clauses nest left-to-right.
Join TypesCross join
For every possible combination of rows from T1
and T2
(i.e., a Cartesian product), the joined table will contain a row consisting of all columns in T1
followed by all columns in T2
. If the tables have N and M rows respectively, the joined table will have N * M rows.
FROM
T1
CROSS JOIN T2
is equivalent to FROM
T1
INNER JOIN T2
ON TRUE (see below). It is also equivalent to FROM
T1
, T2
.
This latter equivalence does not hold exactly when more than two tables appear, because JOIN
binds more tightly than comma. For example FROM
T1
CROSS JOIN T2
INNER JOIN T3
ON condition
is not the same as FROM
T1
, T2
INNER JOIN T3
ON condition
because the condition
can referenceT1
in the first case but not the second.Qualified joins
The words INNER
and OUTER
are optional in all forms. INNER
is the default; LEFT
, RIGHT
, and FULL
imply an outer join.
The join condition is specified in the ON
or USING
clause, or implicitly by the word NATURAL
. The join condition determines which rows from the two source tables are considered to “match”, as explained in detail below.
The possible types of qualified join are:INNER JOIN
For each row R1 of T1, the joined table has a row for each row in T2 that satisfies the join condition with R1.LEFT OUTER JOIN
First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Thus, the joined table always has at least one row for each row in T1.RIGHT OUTER JOIN
First, an inner join is performed. Then, for each row in T2 that does not satisfy the join condition with any row in T1, a joined row is added with null values in columns of T1. This is the converse of a left join: the result table will always have a row for each row in T2.FULL OUTER JOIN
First, an inner join is performed. Then, for each row in T1 that does not satisfy the join condition with any row in T2, a joined row is added with null values in columns of T2. Also, for each row of T2 that does not satisfy the join condition with any row in T1, a joined row with null values in the columns of T1 is added.
The ON
clause is the most general kind of join condition: it takes a Boolean value expression of the same kind as is used in a WHERE
clause. A pair of rows from T1
and _T2
_match if the ON
expression evaluates to true.
The USING
clause is a shorthand that allows you to take advantage of the specific situation where both sides of the join use the same name for the joining column(s). It takes a comma-separated list of the shared column names and forms a join condition that includes an equality comparison for each one. For example, joining T1
and T2
_with USING (a, b)
produces the join condition ON
T1
.a = T2
.a AND T1
.b = T2
_.b.
Furthermore, the output of JOIN USING
suppresses redundant columns: there is no need to print both of the matched columns, since they must have equal values. While JOIN ON
produces all columns from T1
followed by all columns from T2
, JOIN USING
produces one output column for each of the listed column pairs (in the listed order), followed by any remaining columns from T1
, followed by any remaining columns from T2
.
Finally, NATURAL
is a shorthand form of USING
: it forms a USING
list consisting of all column names that appear in both input tables. As with USING
, these columns appear only once in the output table. If there are no common column names, NATURAL JOIN
behaves like JOIN ... ON TRUE
, producing a cross-product join.
USING
is reasonably safe from column changes in the joined relations since only the listed columns are combined. NATURAL
is considerably more risky since any schema changes to either relation that cause a new matching column name to be present will cause the join to combine that new column as well.
To put this together, assume we have tables t1
:
and t2
:
then we get the following results for the various joins:
The join condition specified with ON
can also contain conditions that do not relate directly to the join. This can prove useful for some queries but needs to be thought out carefully. For example:
Notice that placing the restriction in the WHERE
clause produces a different result:
This is because a restriction placed in the ON
clause is processed before the join, while a restriction placed in the WHERE
clause is processed after the join. That does not matter with inner joins, but it matters a lot with outer joins.
7.2.1.2. Table and Column Aliases
A temporary name can be given to tables and complex table references to be used for references to the derived table in the rest of the query. This is called a table alias.
To create a table alias, write
or
The AS
key word is optional noise. alias
can be any identifier.
A typical application of table aliases is to assign short identifiers to long table names to keep the join clauses readable. For example:
The alias becomes the new name of the table reference so far as the current query is concerned — it is not allowed to refer to the table by the original name elsewhere in the query. Thus, this is not valid:
Table aliases are mainly for notational convenience, but it is necessary to use them when joining a table to itself, e.g.:
Additionally, an alias is required if the table reference is a subquery (see Section 7.2.1.3).
Parentheses are used to resolve ambiguities. In the following example, the first statement assigns the alias b
to the second instance of my_table
, but the second statement assigns the alias to the result of the join:
Another form of table aliasing gives temporary names to the columns of the table, as well as the table itself:
If fewer column aliases are specified than the actual table has columns, the remaining columns are not renamed. This syntax is especially useful for self-joins or subqueries.
When an alias is applied to the output of a JOIN
clause, the alias hides the original name(s) within the JOIN
. For example:
is valid SQL, but:
is not valid; the table alias a
is not visible outside the alias c
.
7.2.1.3. Subqueries
Subqueries specifying a derived table must be enclosed in parentheses and must be assigned a table alias name (as in Section 7.2.1.2). For example:
This example is equivalent to FROM table1 AS alias_name
. More interesting cases, which cannot be reduced to a plain join, arise when the subquery involves grouping or aggregation.
A subquery can also be a VALUES
list:
Again, a table alias is required. Assigning alias names to the columns of the VALUES
list is optional, but is good practice. For more information see Section 7.7.
7.2.1.4. Table Functions
Table functions are functions that produce a set of rows, made up of either base data types (scalar types) or composite data types (table rows). They are used like a table, view, or subquery in the FROM
clause of a query. Columns returned by table functions can be included in SELECT
, JOIN
, or WHERE
clauses in the same manner as columns of a table, view, or subquery.
Table functions may also be combined using the ROWS FROM
syntax, with the results returned in parallel columns; the number of result rows in this case is that of the largest function result, with smaller results padded with null values to match.
If the WITH ORDINALITY
clause is specified, an additional column of type bigint
will be added to the function result columns. This column numbers the rows of the function result set, starting from 1. (This is a generalization of the SQL-standard syntax for UNNEST ... WITH ORDINALITY
.) By default, the ordinal column is called ordinality
, but a different column name can be assigned to it using an AS
clause.
The special table function UNNEST
may be called with any number of array parameters, and it returns a corresponding number of columns, as if UNNEST
(Section 9.18) had been called on each parameter separately and combined using the ROWS FROM
construct.
If no table_alias
is specified, the function name is used as the table name; in the case of a ROWS FROM()
construct, the first function's name is used.
If column aliases are not supplied, then for a function returning a base data type, the column name is also the same as the function name. For a function returning a composite type, the result columns get the names of the individual attributes of the type.
Some examples:
In some cases it is useful to define table functions that can return different column sets depending on how they are invoked. To support this, the table function can be declared as returning the pseudo-type record
. When such a function is used in a query, the expected row structure must be specified in the query itself, so that the system can know how to parse and plan the query. This syntax looks like:
When not using the ROWS FROM()
syntax, the column_definition
list replaces the column alias list that could otherwise be attached to the FROM
item; the names in the column definitions serve as column aliases. When using the ROWS FROM()
syntax, a column_definition
list can be attached to each member function separately; or if there is only one member function and no WITH ORDINALITY
clause, a column_definition
list can be written in place of a column alias list following ROWS FROM()
.
Consider this example:
The dblink function (part of the dblink module) executes a remote query. It is declared to return record
since it might be used for any kind of query. The actual column set must be specified in the calling query so that the parser knows, for example, what *
should expand to.
7.2.1.5. LATERAL Subqueries
Subqueries appearing in FROM
can be preceded by the key word LATERAL
. This allows them to reference columns provided by preceding FROM
items. (Without LATERAL
, each subquery is evaluated independently and so cannot cross-reference any other FROM
item.)
Table functions appearing in FROM
can also be preceded by the key word LATERAL
, but for functions the key word is optional; the function's arguments can contain references to columns provided by preceding FROM
items in any case.
A LATERAL
item can appear at top level in the FROM
list, or within a JOIN
tree. In the latter case it can also refer to any items that are on the left-hand side of a JOIN
that it is on the right-hand side of.
When a FROM
item contains LATERAL
cross-references, evaluation proceeds as follows: for each row of the FROM
item providing the cross-referenced column(s), or set of rows of multiple FROM
items providing the columns, the LATERAL
item is evaluated using that row or row set's values of the columns. The resulting row(s) are joined as usual with the rows they were computed from. This is repeated for each row or set of rows from the column source table(s).
A trivial example of LATERAL
is
This is not especially useful since it has exactly the same result as the more conventional
LATERAL
is primarily useful when the cross-referenced column is necessary for computing the row(s) to be joined. A common application is providing an argument value for a set-returning function. For example, supposing that vertices(polygon)
returns the set of vertices of a polygon, we could identify close-together vertices of polygons stored in a table with:
This query could also be written
or in several other equivalent formulations. (As already mentioned, the LATERAL
key word is unnecessary in this example, but we use it for clarity.)
It is often particularly handy to LEFT JOIN
to a LATERAL
subquery, so that source rows will appear in the result even if the LATERAL
subquery produces no rows for them. For example, if get_product_names()
returns the names of products made by a manufacturer, but some manufacturers in our table currently produce no products, we could find out which ones those are like this:
WHERE
ClauseThe syntax of the WHERE
Clause is
where search_condition
is any value expression (see Section 4.2) that returns a value of type boolean
.
After the processing of the FROM
clause is done, each row of the derived virtual table is checked against the search condition. If the result of the condition is true, the row is kept in the output table, otherwise (i.e., if the result is false or null) it is discarded. The search condition typically references at least one column of the table generated in the FROM
clause; this is not required, but otherwise the WHERE
clause will be fairly useless.
The join condition of an inner join can be written either in the WHERE
clause or in the JOIN
clause. For example, these table expressions are equivalent:
and:
or perhaps even:
Which one of these you use is mainly a matter of style. The JOIN
syntax in the FROM
clause is probably not as portable to other SQL database management systems, even though it is in the SQL standard. For outer joins there is no choice: they must be done in the FROM
clause. The ON
or USING
clause of an outer join is not equivalent to a WHERE
condition, because it results in the addition of rows (for unmatched input rows) as well as the removal of rows in the final result.
Here are some examples of WHERE
clauses:
fdt
is the table derived in the FROM
clause. Rows that do not meet the search condition of the WHERE
clause are eliminated from fdt
. Notice the use of scalar subqueries as value expressions. Just like any other query, the subqueries can employ complex table expressions. Notice also how fdt
is referenced in the subqueries. Qualifying c1
as fdt.c1
is only necessary if c1
is also the name of a column in the derived input table of the subquery. But qualifying the column name adds clarity even when it is not needed. This example shows how the column naming scope of an outer query extends into its inner queries.
GROUP BY
and HAVING
ClausesAfter passing the WHERE
filter, the derived input table might be subject to grouping, using the GROUP BY
clause, and elimination of group rows using the HAVING
clause.
The GROUP BY
Clause is used to group together those rows in a table that have the same values in all the columns listed. The order in which the columns are listed does not matter. The effect is to combine each set of rows having common values into one group row that represents all rows in the group. This is done to eliminate redundancy in the output and/or compute aggregates that apply to these groups. For instance:
In the second query, we could not have written SELECT * FROM test1 GROUP BY x
, because there is no single value for the column y
that could be associated with each group. The grouped-by columns can be referenced in the select list since they have a single value in each group.
In general, if a table is grouped, columns that are not listed in GROUP BY
cannot be referenced except in aggregate expressions. An example with aggregate expressions is:
Here sum
is an aggregate function that computes a single value over the entire group. More information about the available aggregate functions can be found in Section 9.20.
Grouping without aggregate expressions effectively calculates the set of distinct values in a column. This can also be achieved using the DISTINCT
clause (see Section 7.3.3).
Here is another example: it calculates the total sales for each product (rather than the total sales of all products):
In this example, the columns product_id
, p.name
, and p.price
must be in the GROUP BY
clause since they are referenced in the query select list (but see below). The columns.units
does not have to be in the GROUP BY
list since it is only used in an aggregate expression (sum(...)
), which represents the sales of a product. For each product, the query returns a summary row about all sales of the product.
If the products table is set up so that, say, product_id
is the primary key, then it would be enough to group by product_id
in the above example, since name and price would be functionally dependent on the product ID, and so there would be no ambiguity about which name and price value to return for each product ID group.
In strict SQL, GROUP BY
can only group by columns of the source table but PostgreSQL extends this to also allow GROUP BY
to group by columns in the select list. Grouping by value expressions instead of simple column names is also allowed.
If a table has been grouped using GROUP BY
, but only certain groups are of interest, the HAVING
clause can be used, much like a WHERE
clause, to eliminate groups from the result. The syntax is:
Expressions in the HAVING
clause can refer both to grouped expressions and to ungrouped expressions (which necessarily involve an aggregate function).
Example:
Again, a more realistic example:
In the example above, the WHERE
clause is selecting rows by a column that is not grouped (the expression is only true for sales during the last four weeks), while the HAVING
clause restricts the output to groups with total gross sales over 5000. Note that the aggregate expressions do not necessarily need to be the same in all parts of the query.
If a query contains aggregate function calls, but no GROUP BY
clause, grouping still occurs: the result is a single group row (or perhaps no rows at all, if the single row is then eliminated by HAVING
). The same is true if it contains a HAVING
clause, even without any aggregate function calls or GROUP BY
clause.
GROUPING SETS
, CUBE
, and ROLLUP
More complex grouping operations than those described above are possible using the concept of grouping sets. The data selected by the FROM
and WHERE
clauses is grouped separately by each specified grouping set, aggregates computed for each group just as for simple GROUP BY
clauses, and then the results returned. For example:
Each sublist of GROUPING SETS
may specify zero or more columns or expressions and is interpreted the same way as though it were directly in the GROUP BY
clause. An empty grouping set means that all rows are aggregated down to a single group (which is output even if no input rows were present), as described above for the case of aggregate functions with no GROUP BY
clause.
References to the grouping columns or expressions are replaced by null values in result rows for grouping sets in which those columns do not appear. To distinguish which grouping a particular output row resulted from, see Table 9.56.
A shorthand notation is provided for specifying two common types of grouping set. A clause of the form
represents the given list of expressions and all prefixes of the list including the empty list; thus it is equivalent to
This is commonly used for analysis over hierarchical data; e.g. total salary by department, division, and company-wide total.
A clause of the form
represents the given list and all of its possible subsets (i.e. the power set). Thus
is equivalent to
The individual elements of a CUBE
or ROLLUP
clause may be either individual expressions, or sublists of elements in parentheses. In the latter case, the sublists are treated as single units for the purposes of generating the individual grouping sets. For example:
is equivalent to
and
is equivalent to
The CUBE
and ROLLUP
constructs can be used either directly in the GROUP BY
clause, or nested inside a GROUPING SETS
clause. If one GROUPING SETS
clause is nested inside another, the effect is the same as if all the elements of the inner clause had been written directly in the outer clause.
If multiple grouping items are specified in a single GROUP BY
clause, then the final list of grouping sets is the cross product of the individual items. For example:
is equivalent to
The construct (a, b)
is normally recognized in expressions as a row constructor. Within the GROUP BY
clause, this does not apply at the top levels of expressions, and (a, b)
is parsed as a list of expressions as described above. If for some reason you need a row constructor in a grouping expression, use ROW(a, b)
.
If the query contains any window functions (see Section 3.5, Section 9.21 and Section 4.2.8), these functions are evaluated after any grouping, aggregation, and HAVING
filtering is performed. That is, if the query uses any aggregates, GROUP BY
, or HAVING
, then the rows seen by the window functions are the group rows instead of the original table rows from FROM
/WHERE
.
When multiple window functions are used, all the window functions having syntactically equivalent PARTITION BY
and ORDER BY
clauses in their window definitions are guaranteed to be evaluated in a single pass over the data. Therefore they will see the same sort ordering, even if the ORDER BY
does not uniquely determine an ordering. However, no guarantees are made about the evaluation of functions having different PARTITION BY
or ORDER BY
specifications. (In such cases a sort step is typically required between the passes of window function evaluations, and the sort is not guaranteed to preserve ordering of rows that its ORDER BY
sees as equivalent.)
Currently, window functions always require presorted data, and so the query output will be ordered according to one or another of the window functions' PARTITION BY
/ORDER BY
clauses. It is not recommended to rely on this, however. Use an explicit top-level ORDER BY
clause if you want to be sure the results are sorted in a particular way.