Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/query.sgml`
2a5220e2d2920169069a3cddf17aca4f89e0c700e001bdd90000000100000fa3
 on the order implicitly.
   </para>

   <para>
    Please enter all the commands shown above so you have some data to
    work with in the following sections.
   </para>

   <para>
    <indexterm>
     <primary>COPY</primary>
    </indexterm>

    You could also have used <command>COPY</command> to load large
    amounts of data from flat-text files.  This is usually faster
    because the <command>COPY</command> command is optimized for this
    application while allowing less flexibility than
    <command>INSERT</command>.  An example would be:

<programlisting>
COPY weather FROM '/home/user/weather.txt';
</programlisting>

    where the file name for the source file must be available on the
    machine running the backend process, not the client, since the backend process
    reads the file directly.  You can read more about the
    <command>COPY</command> command in <xref linkend="sql-copy"/>.
   </para>
  </sect1>


  <sect1 id="tutorial-select">
   <title>Querying a Table</title>

   <para>
    <indexterm><primary>query</primary></indexterm>
    <indexterm><primary>SELECT</primary></indexterm>

    To retrieve data from a table, the table is
    <firstterm>queried</firstterm>.  An <acronym>SQL</acronym>
    <command>SELECT</command> statement is used to do this.  The
    statement is divided into a select list (the part that lists the
    columns to be returned), a table list (the part that lists the
    tables from which to retrieve the data), and an optional
    qualification (the part that specifies any restrictions).  For
    example, to retrieve all the rows of table
    <structname>weather</structname>, type:
<programlisting>
SELECT * FROM weather;
</programlisting>
    Here <literal>*</literal> is a shorthand for <quote>all columns</quote>.
     <footnote>
      <para>
       While <literal>SELECT *</literal> is useful for off-the-cuff
       queries, it is widely considered bad style in production code,
       since adding a column to the table would change the results.
      </para>
     </footnote>
    So the same result would be had with:
<programlisting>
SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
</programlisting>

    The output should be:

<screen>
     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
 San Francisco |      43 |      57 |    0 | 1994-11-29
 Hayward       |      37 |      54 |      | 1994-11-29
(3 rows)
</screen>
   </para>

   <para>
    You can write expressions, not just simple column references, in the
    select list.  For example, you can do:
<programlisting>
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
</programlisting>
    This should give:
<screen>
     city      | temp_avg |    date
---------------+----------+------------
 San Francisco |       48 | 1994-11-27
 San Francisco |       50 | 1994-11-29
 Hayward       |       45 | 1994-11-29
(3 rows)
</screen>
    Notice how the <literal>AS</literal> clause is used to relabel the
    output column.  (The <literal>AS</literal> clause is optional.)
   </para>

   <para>
    A query can be <quote>qualified</quote> by adding a <literal>WHERE</literal>
    clause that specifies which rows are wanted.  The <literal>WHERE</literal>
    clause contains a Boolean (truth value) expression, and only rows for
    which the Boolean expression is true are returned.  The usual
    Boolean operators (<literal>AND</literal>,
    <literal>OR</literal>, and <literal>NOT</literal>) are allowed in
    the qualification.  For example, the following
    retrieves the weather of San Francisco on rainy days:

<programlisting>
SELECT * FROM weather
    WHERE city = 'San Francisco' AND prcp &gt; 0.0;
</programlisting>
    Result:
<screen>
     city      | temp_lo | temp_hi | prcp |    date
---------------+---------+---------+------+------------
 San Francisco |      46 |      50 | 0.25 | 1994-11-27
(1 row)
</screen>
   </para>

Title: Querying a Table in PostgreSQL
Summary
This section explains how to retrieve data from a table using SQL SELECT statements, including specifying columns, using expressions, and qualifying queries with WHERE clauses and Boolean operators to filter results.