1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.5.Querying a Table</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="tutorial-sql.html" title="Chapter2.The SQL Language">
<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">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="tutorial-select"></a>2.5.Querying a Table</h2></div></div></div>
<p> <a name="id568390"></a>
<a name="id568398"></a>
To retrieve data from a table, the table is
<em class="firstterm">queried</em>. An <acronym class="acronym">SQL</acronym>
<code class="command">SELECT</code> 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
<code class="classname">weather</code>, type:
</p>
<pre class="programlisting">SELECT * FROM weather;</pre>
<p>
Here <code class="literal">*</code> is a shorthand for “<span class="quote">all columns</span>”.
<sup>[<a name="id568448" href="#ftn.id568448">2</a>]</sup>
So the same result would be had with:
</p>
<pre class="programlisting">SELECT city, temp_lo, temp_hi, prcp, date FROM weather;</pre>
<p>
The output should be:
</p>
<pre class="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)</pre>
<p>
</p>
<p> You can write expressions, not just simple column references, in the
select list. For example, you can do:
</p>
<pre class="programlisting">SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;</pre>
<p>
This should give:
</p>
<pre class="screen"> city | temp_avg | date
---------------+----------+------------
San Francisco | 48 | 1994-11-27
San Francisco | 50 | 1994-11-29
Hayward | 45 | 1994-11-29
(3 rows)</pre>
<p>
Notice how the <code class="literal">AS</code> clause is used to relabel the
output column. (The <code class="literal">AS</code> clause is optional.)
</p>
<p> A query can be “<span class="quote">qualified</span>” by adding a <code class="literal">WHERE</code>
clause that specifies which rows are wanted. The <code class="literal">WHERE</code>
clause contains a Boolean (truth value) expression, and only rows for
which the Boolean expression is true are returned. The usual
Boolean operators (<code class="literal">AND</code>,
<code class="literal">OR</code>, and <code class="literal">NOT</code>) are allowed in
the qualification. For example, the following
retrieves the weather of San Francisco on rainy days:
</p>
<pre class="programlisting">SELECT * FROM weather
WHERE city = 'San Francisco' AND prcp > 0.0;</pre>
<p>
Result:
</p>
<pre class="screen"> city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
(1 row)</pre>
<p>
</p>
<p> <a name="id568585"></a>
You can request that the results of a query
be returned in sorted order:
</p>
<pre class="programlisting">SELECT * FROM weather
ORDER BY city;</pre>
<p>
</p>
<pre class="screen"> city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
Hayward | 37 | 54 | | 1994-11-29
San Francisco | 43 | 57 | 0 | 1994-11-29
San Francisco | 46 | 50 | 0.25 | 1994-11-27</pre>
<p>
In this example, the sort order isn't fully specified, and so you
might get the San Francisco rows in either order. But you'd always
get the results shown above if you do
</p>
<pre class="programlisting">SELECT * FROM weather
ORDER BY city, temp_lo;</pre>
<p>
</p>
<p> <a name="id568625"></a>
<a name="id568633"></a>
You can request that duplicate rows be removed from the result of
a query:
</p>
<pre class="programlisting">SELECT DISTINCT city
FROM weather;</pre>
<p>
</p>
<pre class="screen"> city
---------------
Hayward
San Francisco
(2 rows)</pre>
<p>
Here again, the result row ordering might vary.
You can ensure consistent results by using <code class="literal">DISTINCT</code> and
<code class="literal">ORDER BY</code> together:
<sup>[<a name="id568672" href="#ftn.id568672">3</a>]</sup>
</p>
<pre class="programlisting">SELECT DISTINCT city
FROM weather
ORDER BY city;</pre>
<p>
</p>
<div class="footnotes">
<br><hr width="100" align="left">
<div class="footnote"><p><sup>[<a name="ftn.id568448" href="#id568448">2</a>] </sup> While <code class="literal">SELECT *</code> 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.
</p></div>
<div class="footnote"><p><sup>[<a name="ftn.id568672" href="#id568672">3</a>] </sup> In some database systems, including older versions of
<span class="productname">PostgreSQL</span>, the implementation of
<code class="literal">DISTINCT</code> automatically orders the rows and
so <code class="literal">ORDER BY</code> is redundant. But this is not
required by the SQL standard, and current
<span class="productname">PostgreSQL</span> doesn't guarantee that
<code class="literal">DISTINCT</code> causes the rows to be ordered.
</p></div>
</div>
</div></body>
</html>
|