]> begriffs open source - ai-pg/blob - full-docs/txt/tutorial-select.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / tutorial-select.txt
1
2 2.5. Querying a Table #
3
4    To retrieve data from a table, the table is queried. An SQL SELECT
5    statement is used to do this. The statement is divided into a select
6    list (the part that lists the columns to be returned), a table list
7    (the part that lists the tables from which to retrieve the data), and
8    an optional qualification (the part that specifies any restrictions).
9    For example, to retrieve all the rows of table weather, type:
10 SELECT * FROM weather;
11
12    Here * is a shorthand for “all columns”. ^[2] So the same result would
13    be had with:
14 SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
15
16    The output should be:
17      city      | temp_lo | temp_hi | prcp |    date
18 ---------------+---------+---------+------+------------
19  San Francisco |      46 |      50 | 0.25 | 1994-11-27
20  San Francisco |      43 |      57 |    0 | 1994-11-29
21  Hayward       |      37 |      54 |      | 1994-11-29
22 (3 rows)
23
24    You can write expressions, not just simple column references, in the
25    select list. For example, you can do:
26 SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
27
28    This should give:
29      city      | temp_avg |    date
30 ---------------+----------+------------
31  San Francisco |       48 | 1994-11-27
32  San Francisco |       50 | 1994-11-29
33  Hayward       |       45 | 1994-11-29
34 (3 rows)
35
36    Notice how the AS clause is used to relabel the output column. (The AS
37    clause is optional.)
38
39    A query can be “qualified” by adding a WHERE clause that specifies
40    which rows are wanted. The WHERE clause contains a Boolean (truth
41    value) expression, and only rows for which the Boolean expression is
42    true are returned. The usual Boolean operators (AND, OR, and NOT) are
43    allowed in the qualification. For example, the following retrieves the
44    weather of San Francisco on rainy days:
45 SELECT * FROM weather
46     WHERE city = 'San Francisco' AND prcp > 0.0;
47
48    Result:
49      city      | temp_lo | temp_hi | prcp |    date
50 ---------------+---------+---------+------+------------
51  San Francisco |      46 |      50 | 0.25 | 1994-11-27
52 (1 row)
53
54    You can request that the results of a query be returned in sorted
55    order:
56 SELECT * FROM weather
57     ORDER BY city;
58
59      city      | temp_lo | temp_hi | prcp |    date
60 ---------------+---------+---------+------+------------
61  Hayward       |      37 |      54 |      | 1994-11-29
62  San Francisco |      43 |      57 |    0 | 1994-11-29
63  San Francisco |      46 |      50 | 0.25 | 1994-11-27
64
65    In this example, the sort order isn't fully specified, and so you might
66    get the San Francisco rows in either order. But you'd always get the
67    results shown above if you do:
68 SELECT * FROM weather
69     ORDER BY city, temp_lo;
70
71    You can request that duplicate rows be removed from the result of a
72    query:
73 SELECT DISTINCT city
74     FROM weather;
75
76      city
77 ---------------
78  Hayward
79  San Francisco
80 (2 rows)
81
82    Here again, the result row ordering might vary. You can ensure
83    consistent results by using DISTINCT and ORDER BY together: ^[3]
84 SELECT DISTINCT city
85     FROM weather
86     ORDER BY city;
87
88    ^[2] While SELECT * is useful for off-the-cuff queries, it is widely
89    considered bad style in production code, since adding a column to the
90    table would change the results.
91
92    ^[3] In some database systems, including older versions of PostgreSQL,
93    the implementation of DISTINCT automatically orders the rows and so
94    ORDER BY is unnecessary. But this is not required by the SQL standard,
95    and current PostgreSQL does not guarantee that DISTINCT causes the rows
96    to be ordered.