This page in other versions: Unsupported versions: 6.3
PostgreSQL
Prev Chapter 4. The Query Language Next

Querying a Class

The weather class can be queried with normal relational selection and projection queries. A SQL select statement is used to do this. The statement is divided into a target list (the part that lists the attributes to be returned) and a qualification (the part that specifies any restrictions). For example, to retrieve all the rows of weather, type:

SELECT * FROM WEATHER;
and the output should be:
+--------------+---------+---------+------+------------+
|city          | temp_lo | temp_hi | prcp | date       |
+--------------+---------+---------+------+------------+
|San Francisco | 46      | 50      | 0.25 | 11-27-1994 |
+--------------+---------+---------+------+------------+
|San Francisco | 43      | 57      | 0    | 11-29-1994 |
+--------------+---------+---------+------+------------+
|Hayward       | 37      | 54      |      | 11-29-1994 |
+--------------+---------+---------+------+------------+
You may specify any arbitrary expressions in the target list. For example, you can do:
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

Arbitrary Boolean operators (and, or and not) are allowed in the qualification of any query. For example,

SELECT * FROM weather
    WHERE city = 'San Francisco'
    AND prcp > 0.0;

+--------------+---------+---------+------+------------+
|city          | temp_lo | temp_hi | prcp | date       |
+--------------+---------+---------+------+------------+
|San Francisco | 46      | 50      | 0.25 | 11-27-1994 |
+--------------+---------+---------+------+------------+

As a final note, you can specify that the results of a select can be returned in a sorted order or with duplicate instances removed.

SELECT DISTINCT city
    FROM weather
    ORDER BY city;

Prev Home Next
Populating a Class with Instances Up Redirecting SELECT Queries
Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group