]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/queries-select-lists.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / queries-select-lists.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>7.3. Select Lists</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="queries-table-expressions.html" title="7.2. Table Expressions" /><link rel="next" href="queries-union.html" title="7.4. Combining Queries (UNION, INTERSECT, EXCEPT)" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">7.3. Select Lists</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="queries-table-expressions.html" title="7.2. Table Expressions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><th width="60%" align="center">Chapter 7. Queries</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-union.html" title="7.4. Combining Queries (UNION, INTERSECT, EXCEPT)">Next</a></td></tr></table><hr /></div><div class="sect1" id="QUERIES-SELECT-LISTS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.3. Select Lists <a href="#QUERIES-SELECT-LISTS" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="queries-select-lists.html#QUERIES-SELECT-LIST-ITEMS">7.3.1. Select-List Items</a></span></dt><dt><span class="sect2"><a href="queries-select-lists.html#QUERIES-COLUMN-LABELS">7.3.2. Column Labels</a></span></dt><dt><span class="sect2"><a href="queries-select-lists.html#QUERIES-DISTINCT">7.3.3. <code class="literal">DISTINCT</code></a></span></dt></dl></div><a id="id-1.5.6.7.2" class="indexterm"></a><p>
3    As shown in the previous section,
4    the table expression in the <code class="command">SELECT</code> command
5    constructs an intermediate virtual table by possibly combining
6    tables, views, eliminating rows, grouping, etc.  This table is
7    finally passed on to processing by the <em class="firstterm">select list</em>.  The select
8    list determines which <span class="emphasis"><em>columns</em></span> of the
9    intermediate table are actually output.
10   </p><div class="sect2" id="QUERIES-SELECT-LIST-ITEMS"><div class="titlepage"><div><div><h3 class="title">7.3.1. Select-List Items <a href="#QUERIES-SELECT-LIST-ITEMS" class="id_link">#</a></h3></div></div></div><a id="id-1.5.6.7.4.2" class="indexterm"></a><p>
11     The simplest kind of select list is <code class="literal">*</code> which
12     emits all columns that the table expression produces.  Otherwise,
13     a select list is a comma-separated list of value expressions (as
14     defined in <a class="xref" href="sql-expressions.html" title="4.2. Value Expressions">Section 4.2</a>).  For instance, it
15     could be a list of column names:
16 </p><pre class="programlisting">
17 SELECT a, b, c FROM ...
18 </pre><p>
19      The columns names <code class="literal">a</code>, <code class="literal">b</code>, and <code class="literal">c</code>
20      are either the actual names of the columns of tables referenced
21      in the <code class="literal">FROM</code> clause, or the aliases given to them as
22      explained in <a class="xref" href="queries-table-expressions.html#QUERIES-TABLE-ALIASES" title="7.2.1.2. Table and Column Aliases">Section 7.2.1.2</a>.  The name
23      space available in the select list is the same as in the
24      <code class="literal">WHERE</code> clause, unless grouping is used, in which case
25      it is the same as in the <code class="literal">HAVING</code> clause.
26    </p><p>
27     If more than one table has a column of the same name, the table
28     name must also be given, as in:
29 </p><pre class="programlisting">
30 SELECT tbl1.a, tbl2.a, tbl1.b FROM ...
31 </pre><p>
32     When working with multiple tables, it can also be useful to ask for
33     all the columns of a particular table:
34 </p><pre class="programlisting">
35 SELECT tbl1.*, tbl2.a FROM ...
36 </pre><p>
37     See <a class="xref" href="rowtypes.html#ROWTYPES-USAGE" title="8.16.5. Using Composite Types in Queries">Section 8.16.5</a> for more about
38     the <em class="replaceable"><code>table_name</code></em><code class="literal">.*</code> notation.
39    </p><p>
40     If an arbitrary value expression is used in the select list, it
41     conceptually adds a new virtual column to the returned table.  The
42     value expression is evaluated once for each result row, with
43     the row's values substituted for any column references.  But the
44     expressions in the select list do not have to reference any
45     columns in the table expression of the <code class="literal">FROM</code> clause;
46     they can be constant arithmetic expressions, for instance.
47    </p></div><div class="sect2" id="QUERIES-COLUMN-LABELS"><div class="titlepage"><div><div><h3 class="title">7.3.2. Column Labels <a href="#QUERIES-COLUMN-LABELS" class="id_link">#</a></h3></div></div></div><a id="id-1.5.6.7.5.2" class="indexterm"></a><p>
48     The entries in the select list can be assigned names for subsequent
49     processing, such as for use in an <code class="literal">ORDER BY</code> clause
50     or for display by the client application.  For example:
51 </p><pre class="programlisting">
52 SELECT a AS value, b + c AS sum FROM ...
53 </pre><p>
54    </p><p>
55     If no output column name is specified using <code class="literal">AS</code>,
56     the system assigns a default column name.  For simple column references,
57     this is the name of the referenced column.  For function
58     calls, this is the name of the function.  For complex expressions,
59     the system will generate a generic name.
60    </p><p>
61     The <code class="literal">AS</code> key word is usually optional, but in some
62     cases where the desired column name matches a
63     <span class="productname">PostgreSQL</span> key word, you must write
64     <code class="literal">AS</code> or double-quote the column name in order to
65     avoid ambiguity.
66     (<a class="xref" href="sql-keywords-appendix.html" title="Appendix C. SQL Key Words">Appendix C</a> shows which key words
67     require <code class="literal">AS</code> to be used as a column label.)
68     For example, <code class="literal">FROM</code> is one such key word, so this
69     does not work:
70 </p><pre class="programlisting">
71 SELECT a from, b + c AS sum FROM ...
72 </pre><p>
73     but either of these do:
74 </p><pre class="programlisting">
75 SELECT a AS from, b + c AS sum FROM ...
76 SELECT a "from", b + c AS sum FROM ...
77 </pre><p>
78     For greatest safety against possible
79     future key word additions, it is recommended that you always either
80     write <code class="literal">AS</code> or double-quote the output column name.
81    </p><div class="note"><h3 class="title">Note</h3><p>
82      The naming of output columns here is different from that done in
83      the <code class="literal">FROM</code> clause (see <a class="xref" href="queries-table-expressions.html#QUERIES-TABLE-ALIASES" title="7.2.1.2. Table and Column Aliases">Section 7.2.1.2</a>).  It is possible
84      to rename the same column twice, but the name assigned in
85      the select list is the one that will be passed on.
86     </p></div></div><div class="sect2" id="QUERIES-DISTINCT"><div class="titlepage"><div><div><h3 class="title">7.3.3. <code class="literal">DISTINCT</code> <a href="#QUERIES-DISTINCT" class="id_link">#</a></h3></div></div></div><a id="id-1.5.6.7.6.2" class="indexterm"></a><a id="id-1.5.6.7.6.3" class="indexterm"></a><a id="id-1.5.6.7.6.4" class="indexterm"></a><p>
87     After the select list has been processed, the result table can
88     optionally be subject to the elimination of duplicate rows.  The
89     <code class="literal">DISTINCT</code> key word is written directly after
90     <code class="literal">SELECT</code> to specify this:
91 </p><pre class="synopsis">
92 SELECT DISTINCT <em class="replaceable"><code>select_list</code></em> ...
93 </pre><p>
94     (Instead of <code class="literal">DISTINCT</code> the key word <code class="literal">ALL</code>
95     can be used to specify the default behavior of retaining all rows.)
96    </p><a id="id-1.5.6.7.6.6" class="indexterm"></a><p>
97     Obviously, two rows are considered distinct if they differ in at
98     least one column value.  Null values are considered equal in this
99     comparison.
100    </p><p>
101     Alternatively, an arbitrary expression can determine what rows are
102     to be considered distinct:
103 </p><pre class="synopsis">
104 SELECT DISTINCT ON (<em class="replaceable"><code>expression</code></em> [<span class="optional">, <em class="replaceable"><code>expression</code></em> ...</span>]) <em class="replaceable"><code>select_list</code></em> ...
105 </pre><p>
106     Here <em class="replaceable"><code>expression</code></em> is an arbitrary value
107     expression that is evaluated for all rows.  A set of rows for
108     which all the expressions are equal are considered duplicates, and
109     only the first row of the set is kept in the output.  Note that
110     the <span class="quote">“<span class="quote">first row</span>”</span> of a set is unpredictable unless the
111     query is sorted on enough columns to guarantee a unique ordering
112     of the rows arriving at the <code class="literal">DISTINCT</code> filter.
113     (<code class="literal">DISTINCT ON</code> processing occurs after <code class="literal">ORDER
114     BY</code> sorting.)
115    </p><p>
116     The <code class="literal">DISTINCT ON</code> clause is not part of the SQL standard
117     and is sometimes considered bad style because of the potentially
118     indeterminate nature of its results.  With judicious use of
119     <code class="literal">GROUP BY</code> and subqueries in <code class="literal">FROM</code>, this
120     construct can be avoided, but it is often the most convenient
121     alternative.
122    </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queries-table-expressions.html" title="7.2. Table Expressions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html" title="Chapter 7. Queries">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-union.html" title="7.4. Combining Queries (UNION, INTERSECT, EXCEPT)">Next</a></td></tr><tr><td width="40%" align="left" valign="top">7.2. Table Expressions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 7.4. Combining Queries (<code class="literal">UNION</code>, <code class="literal">INTERSECT</code>, <code class="literal">EXCEPT</code>)</td></tr></table></div></body></html>