]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/tutorial-select.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / tutorial-select.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>2.5. Querying a Table</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="tutorial-populate.html" title="2.4. Populating a Table With Rows" /><link rel="next" href="tutorial-join.html" title="2.6. Joins Between Tables" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">2.5. Querying a Table</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-populate.html" title="2.4. Populating a Table With Rows">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Up</a></td><th width="60%" align="center">Chapter 2. The <acronym class="acronym">SQL</acronym> Language</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="tutorial-join.html" title="2.6. Joins Between Tables">Next</a></td></tr></table><hr /></div><div class="sect1" id="TUTORIAL-SELECT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">2.5. Querying a Table <a href="#TUTORIAL-SELECT" class="id_link">#</a></h2></div></div></div><p>
3     <a id="id-1.4.4.6.2.1" class="indexterm"></a>
4     <a id="id-1.4.4.6.2.2" class="indexterm"></a>
5
6     To retrieve data from a table, the table is
7     <em class="firstterm">queried</em>.  An <acronym class="acronym">SQL</acronym>
8     <code class="command">SELECT</code> statement is used to do this.  The
9     statement is divided into a select list (the part that lists the
10     columns to be returned), a table list (the part that lists the
11     tables from which to retrieve the data), and an optional
12     qualification (the part that specifies any restrictions).  For
13     example, to retrieve all the rows of table
14     <code class="structname">weather</code>, type:
15 </p><pre class="programlisting">
16 SELECT * FROM weather;
17 </pre><p>
18     Here <code class="literal">*</code> is a shorthand for <span class="quote">“<span class="quote">all columns</span>”</span>.
19      <a href="#ftn.id-1.4.4.6.2.10" class="footnote"><sup class="footnote" id="id-1.4.4.6.2.10">[2]</sup></a>
20     So the same result would be had with:
21 </p><pre class="programlisting">
22 SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
23 </pre><p>
24
25     The output should be:
26
27 </p><pre class="screen">
28      city      | temp_lo | temp_hi | prcp |    date
29 ---------------+---------+---------+------+------------
30  San Francisco |      46 |      50 | 0.25 | 1994-11-27
31  San Francisco |      43 |      57 |    0 | 1994-11-29
32  Hayward       |      37 |      54 |      | 1994-11-29
33 (3 rows)
34 </pre><p>
35    </p><p>
36     You can write expressions, not just simple column references, in the
37     select list.  For example, you can do:
38 </p><pre class="programlisting">
39 SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
40 </pre><p>
41     This should give:
42 </p><pre class="screen">
43      city      | temp_avg |    date
44 ---------------+----------+------------
45  San Francisco |       48 | 1994-11-27
46  San Francisco |       50 | 1994-11-29
47  Hayward       |       45 | 1994-11-29
48 (3 rows)
49 </pre><p>
50     Notice how the <code class="literal">AS</code> clause is used to relabel the
51     output column.  (The <code class="literal">AS</code> clause is optional.)
52    </p><p>
53     A query can be <span class="quote">“<span class="quote">qualified</span>”</span> by adding a <code class="literal">WHERE</code>
54     clause that specifies which rows are wanted.  The <code class="literal">WHERE</code>
55     clause contains a Boolean (truth value) expression, and only rows for
56     which the Boolean expression is true are returned.  The usual
57     Boolean operators (<code class="literal">AND</code>,
58     <code class="literal">OR</code>, and <code class="literal">NOT</code>) are allowed in
59     the qualification.  For example, the following
60     retrieves the weather of San Francisco on rainy days:
61
62 </p><pre class="programlisting">
63 SELECT * FROM weather
64     WHERE city = 'San Francisco' AND prcp &gt; 0.0;
65 </pre><p>
66     Result:
67 </p><pre class="screen">
68      city      | temp_lo | temp_hi | prcp |    date
69 ---------------+---------+---------+------+------------
70  San Francisco |      46 |      50 | 0.25 | 1994-11-27
71 (1 row)
72 </pre><p>
73    </p><p>
74     <a id="id-1.4.4.6.5.1" class="indexterm"></a>
75
76     You can request that the results of a query
77     be returned in sorted order:
78
79 </p><pre class="programlisting">
80 SELECT * FROM weather
81     ORDER BY city;
82 </pre><p>
83
84 </p><pre class="screen">
85      city      | temp_lo | temp_hi | prcp |    date
86 ---------------+---------+---------+------+------------
87  Hayward       |      37 |      54 |      | 1994-11-29
88  San Francisco |      43 |      57 |    0 | 1994-11-29
89  San Francisco |      46 |      50 | 0.25 | 1994-11-27
90 </pre><p>
91
92     In this example, the sort order isn't fully specified, and so you
93     might get the San Francisco rows in either order.  But you'd always
94     get the results shown above if you do:
95
96 </p><pre class="programlisting">
97 SELECT * FROM weather
98     ORDER BY city, temp_lo;
99 </pre><p>
100    </p><p>
101     <a id="id-1.4.4.6.6.1" class="indexterm"></a>
102     <a id="id-1.4.4.6.6.2" class="indexterm"></a>
103
104     You can request that duplicate rows be removed from the result of
105     a query:
106
107 </p><pre class="programlisting">
108 SELECT DISTINCT city
109     FROM weather;
110 </pre><p>
111
112 </p><pre class="screen">
113      city
114 ---------------
115  Hayward
116  San Francisco
117 (2 rows)
118 </pre><p>
119
120     Here again, the result row ordering might vary.
121     You can ensure consistent results by using <code class="literal">DISTINCT</code> and
122     <code class="literal">ORDER BY</code> together:
123      <a href="#ftn.id-1.4.4.6.6.7" class="footnote"><sup class="footnote" id="id-1.4.4.6.6.7">[3]</sup></a>
124
125 </p><pre class="programlisting">
126 SELECT DISTINCT city
127     FROM weather
128     ORDER BY city;
129 </pre><p>
130    </p><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.4.4.6.2.10" class="footnote"><p><a href="#id-1.4.4.6.2.10" class="para"><sup class="para">[2] </sup></a>
131        While <code class="literal">SELECT *</code> is useful for off-the-cuff
132        queries, it is widely considered bad style in production code,
133        since adding a column to the table would change the results.
134       </p></div><div id="ftn.id-1.4.4.6.6.7" class="footnote"><p><a href="#id-1.4.4.6.6.7" class="para"><sup class="para">[3] </sup></a>
135        In some database systems, including older versions of
136        <span class="productname">PostgreSQL</span>, the implementation of
137        <code class="literal">DISTINCT</code> automatically orders the rows and
138        so <code class="literal">ORDER BY</code> is unnecessary.  But this is not
139        required by the SQL standard, and current
140        <span class="productname">PostgreSQL</span> does not guarantee that
141        <code class="literal">DISTINCT</code> causes the rows to be ordered.
142       </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-populate.html" title="2.4. Populating a Table With Rows">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-sql.html" title="Chapter 2. The SQL Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-join.html" title="2.6. Joins Between Tables">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2.4. Populating a Table With Rows </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"> 2.6. Joins Between Tables</td></tr></table></div></body></html>