2 2.5. Querying a Table #
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;
12 Here * is a shorthand for “all columns”. ^[2] So the same result would
14 SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
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
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;
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
36 Notice how the AS clause is used to relabel the output column. (The AS
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:
46 WHERE city = 'San Francisco' AND prcp > 0.0;
49 city | temp_lo | temp_hi | prcp | date
50 ---------------+---------+---------+------+------------
51 San Francisco | 46 | 50 | 0.25 | 1994-11-27
54 You can request that the results of a query be returned in sorted
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
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:
69 ORDER BY city, temp_lo;
71 You can request that duplicate rows be removed from the result of a
82 Here again, the result row ordering might vary. You can ensure
83 consistent results by using DISTINCT and ORDER BY together: ^[3]
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.
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