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>
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;
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;
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
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;
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
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.)
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:
62 </p><pre class="programlisting">
64 WHERE city = 'San Francisco' AND prcp > 0.0;
67 </p><pre class="screen">
68 city | temp_lo | temp_hi | prcp | date
69 ---------------+---------+---------+------+------------
70 San Francisco | 46 | 50 | 0.25 | 1994-11-27
74 <a id="id-1.4.4.6.5.1" class="indexterm"></a>
76 You can request that the results of a query
77 be returned in sorted order:
79 </p><pre class="programlisting">
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
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:
96 </p><pre class="programlisting">
98 ORDER BY city, temp_lo;
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>
104 You can request that duplicate rows be removed from the result of
107 </p><pre class="programlisting">
112 </p><pre class="screen">
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>
125 </p><pre class="programlisting">
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>