+ ...
SELECT * FROM weather;
- (here * means all columns
).
+ Here * is a shorthand for all columns
.
While SELECT * is useful for off-the-cuff
since adding a column to the table would change the results.
+ So the same result would be had with:
+SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
+
+
The output should be:
- You may specify any arbitrary expressions in the select list. For
- example, you can do:
+ You can write expressions, not just simple column references, in the
+ select list. For example, you can do:
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
- Arbitrary Boolean operators (AND,
+ A query can be qualified> by adding a WHERE>
+ clause that specifies which rows are wanted. The WHERE>
+ clause contains a Boolean (truth value) expression, and only rows for
+ which the Boolean expression is true are returned. The usual
+ Boolean operators (AND,
OR, and NOT) are allowed in
- the qualification of a query. For example, the following
+ the qualification. For example, the following
retrieves the weather of San Francisco on rainy days:
SELECT * FROM weather
- WHERE city = 'San Francisco'
- AND prcp > 0.0;
+ WHERE city = 'San Francisco' AND prcp > 0.0;
Result:
+
+ You can request that the results of a query
+ be returned in sorted order:
+
+SELECT * FROM weather
+ ORDER BY city;
+
+
+
+ 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
+
+
+ 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
+
+SELECT * FROM weather
+ ORDER BY city, temp_lo;
+
+
+
- As a final note, you can request that the results of a query can
- be returned in sorted order or with duplicate rows removed:
+ You can request that duplicate rows be removed from the result of
+ a query:
SELECT DISTINCT city
- FROM weather
- ORDER BY city;
+ FROM weather;
(2 rows)
- DISTINCT and ORDER BY can be
- used separately, of course.
+ Here again, the result row ordering might vary.
+ You can ensure consistent results by using DISTINCT and
+ ORDER BY together:
+
+ In some database systems, including older versions of
+
PostgreSQL, the implementation of
+ DISTINCT automatically orders the rows and
+ so ORDER BY is redundant. But this is not
+ required by the SQL standard, and current
+
PostgreSQL doesn't guarantee that
+ DISTINCT causes the rows to be ordered.
+
+
+
+SELECT DISTINCT city
+ FROM weather
+ ORDER BY city;
+
Possibly, your site administrator has already created a database
for your use. He should have told you what the name of your
- database is. In this case you can omit this step and skip ahead
+ database is. In that case you can omit this step and skip ahead
to the next section.
- If you do not have the privileges required to create a database,
- you will see the following:
+ Another response could be this:
+
+createdb: could not connect to database template1: FATAL: user "joe" does not
+exist
+
+ where your own login name is mentioned. This will happen if the
+ administrator has not created a
PostgreSQL> user account
+ for you. (
PostgreSQL> user accounts are distinct from
+ operating system user accounts.) If you are the administrator, see
+ for help creating accounts. You will need to
+ become the operating system user under which
PostgreSQL>
+ was installed (usually postgres>) to create the first user
+ account. It could also be that you were assigned a
+
PostgreSQL> user name that is different from your
+ operating system user name; in that case you need to use the
+ switch or set the PGUSER> environment variable to specify your
+
+
+ If you have a user account but it does not have the privileges required to
+ create a database, you will see the following:
createdb: database creation failed: ERROR: permission denied to create database
- If you have encountered problems starting psql
+ If you encounter problems starting psql
then go back to the previous section. The diagnostics of
- psql and createdb are
- similar, and if the latter worked the former should work as well.
+ createdb and psql are
+ similar, and if the former worked the latter should work as well.