Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 9b6cc37ce608401d44f6535a0c7cb777 > files > 640

postgresql11-docs-11.5-1.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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 11.5 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="queries-union.html" title="7.4. Combining Queries">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="QUERIES-SELECT-LISTS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">7.3. Select Lists</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>
   As shown in the previous section,
   the table expression in the <code class="command">SELECT</code> command
   constructs an intermediate virtual table by possibly combining
   tables, views, eliminating rows, grouping, etc.  This table is
   finally passed on to processing by the <em class="firstterm">select list</em>.  The select
   list determines which <span class="emphasis"><em>columns</em></span> of the
   intermediate table are actually output.
  </p><div class="sect2" id="QUERIES-SELECT-LIST-ITEMS"><div class="titlepage"><div><div><h3 class="title">7.3.1. Select-List Items</h3></div></div></div><a id="id-1.5.6.7.4.2" class="indexterm"></a><p>
    The simplest kind of select list is <code class="literal">*</code> which
    emits all columns that the table expression produces.  Otherwise,
    a select list is a comma-separated list of value expressions (as
    defined in <a class="xref" href="sql-expressions.html" title="4.2. Value Expressions">Section 4.2</a>).  For instance, it
    could be a list of column names:
</p><pre class="programlisting">
SELECT a, b, c FROM ...
</pre><p>
     The columns names <code class="literal">a</code>, <code class="literal">b</code>, and <code class="literal">c</code>
     are either the actual names of the columns of tables referenced
     in the <code class="literal">FROM</code> clause, or the aliases given to them as
     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
     space available in the select list is the same as in the
     <code class="literal">WHERE</code> clause, unless grouping is used, in which case
     it is the same as in the <code class="literal">HAVING</code> clause.
   </p><p>
    If more than one table has a column of the same name, the table
    name must also be given, as in:
</p><pre class="programlisting">
SELECT tbl1.a, tbl2.a, tbl1.b FROM ...
</pre><p>
    When working with multiple tables, it can also be useful to ask for
    all the columns of a particular table:
</p><pre class="programlisting">
SELECT tbl1.*, tbl2.a FROM ...
</pre><p>
    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
    the <em class="replaceable"><code>table_name</code></em><code class="literal">.*</code> notation.
   </p><p>
    If an arbitrary value expression is used in the select list, it
    conceptually adds a new virtual column to the returned table.  The
    value expression is evaluated once for each result row, with
    the row's values substituted for any column references.  But the
    expressions in the select list do not have to reference any
    columns in the table expression of the <code class="literal">FROM</code> clause;
    they can be constant arithmetic expressions, for instance.
   </p></div><div class="sect2" id="QUERIES-COLUMN-LABELS"><div class="titlepage"><div><div><h3 class="title">7.3.2. Column Labels</h3></div></div></div><a id="id-1.5.6.7.5.2" class="indexterm"></a><p>
    The entries in the select list can be assigned names for subsequent
    processing, such as for use in an <code class="literal">ORDER BY</code> clause
    or for display by the client application.  For example:
</p><pre class="programlisting">
SELECT a AS value, b + c AS sum FROM ...
</pre><p>
   </p><p>
    If no output column name is specified using <code class="literal">AS</code>,
    the system assigns a default column name.  For simple column references,
    this is the name of the referenced column.  For function
    calls, this is the name of the function.  For complex expressions,
    the system will generate a generic name.
   </p><p>
    The <code class="literal">AS</code> keyword is optional, but only if the new column
    name does not match any
    <span class="productname">PostgreSQL</span> keyword (see <a class="xref" href="sql-keywords-appendix.html" title="Appendix C. SQL Key Words">Appendix C</a>).  To avoid an accidental match to
    a keyword, you can double-quote the column name.  For example,
    <code class="literal">VALUE</code> is a keyword, so this does not work:
</p><pre class="programlisting">
SELECT a value, b + c AS sum FROM ...
</pre><p>
    but this does:
</p><pre class="programlisting">
SELECT a "value", b + c AS sum FROM ...
</pre><p>
    For protection against possible
    future keyword additions, it is recommended that you always either
    write <code class="literal">AS</code> or double-quote the output column name.
   </p><div class="note"><h3 class="title">Note</h3><p>
     The naming of output columns here is different from that done in
     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
     to rename the same column twice, but the name assigned in
     the select list is the one that will be passed on.
    </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></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><p>
    After the select list has been processed, the result table can
    optionally be subject to the elimination of duplicate rows.  The
    <code class="literal">DISTINCT</code> key word is written directly after
    <code class="literal">SELECT</code> to specify this:
</p><pre class="synopsis">
SELECT DISTINCT <em class="replaceable"><code>select_list</code></em> ...
</pre><p>
    (Instead of <code class="literal">DISTINCT</code> the key word <code class="literal">ALL</code>
    can be used to specify the default behavior of retaining all rows.)
   </p><a id="id-1.5.6.7.6.5" class="indexterm"></a><p>
    Obviously, two rows are considered distinct if they differ in at
    least one column value.  Null values are considered equal in this
    comparison.
   </p><p>
    Alternatively, an arbitrary expression can determine what rows are
    to be considered distinct:
</p><pre class="synopsis">
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> ...
</pre><p>
    Here <em class="replaceable"><code>expression</code></em> is an arbitrary value
    expression that is evaluated for all rows.  A set of rows for
    which all the expressions are equal are considered duplicates, and
    only the first row of the set is kept in the output.  Note that
    the <span class="quote">“<span class="quote">first row</span>”</span> of a set is unpredictable unless the
    query is sorted on enough columns to guarantee a unique ordering
    of the rows arriving at the <code class="literal">DISTINCT</code> filter.
    (<code class="literal">DISTINCT ON</code> processing occurs after <code class="literal">ORDER
    BY</code> sorting.)
   </p><p>
    The <code class="literal">DISTINCT ON</code> clause is not part of the SQL standard
    and is sometimes considered bad style because of the potentially
    indeterminate nature of its results.  With judicious use of
    <code class="literal">GROUP BY</code> and subqueries in <code class="literal">FROM</code>, this
    construct can be avoided, but it is often the most convenient
    alternative.
   </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="queries.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="queries-union.html">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">Home</a></td><td width="40%" align="right" valign="top"> 7.4. Combining Queries</td></tr></table></div></body></html>