2.5. Querying a Table | ||||
---|---|---|---|---|
Prev | Up | Chapter 2. TheSQL Language | Home | Next |
2.5. Querying a Table#
To retrieve data from a table, the table isqueried. AnSQL Here The output should be: You can write expressions, not just simple column references, in the select list. For example, you can do: This should give: Notice how the A query can be“qualified” by adding a Result: You can request that the results of a query be returned in sorted order: 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: You can request that duplicate rows be removed from the result of a query: Here again, the result row ordering might vary. You can ensure consistent results by using [2] While [3] In some database systems, including older versions ofPostgreSQL, the implementation ofSELECT
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 tableweather
, type:SELECT * FROM weather;
*
is a shorthand for“all columns”.[2] So the same result would be had with:SELECT city, temp_lo, temp_hi, prcp, date FROM weather;
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)
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
city | temp_avg | date---------------+----------+------------ San Francisco | 48 | 1994-11-27 San Francisco | 50 | 1994-11-29 Hayward | 45 | 1994-11-29(3 rows)
AS
clause is used to relabel the output column. (TheAS
clause is optional.)WHERE
clause that specifies which rows are wanted. TheWHERE
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
, andNOT
) are allowed in 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;
city | temp_lo | temp_hi | prcp | date---------------+---------+---------+------+------------ San Francisco | 46 | 50 | 0.25 | 1994-11-27(1 row)
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
SELECT * FROM weather ORDER BY city, temp_lo;
SELECT DISTINCT city FROM weather;
city--------------- Hayward San Francisco(2 rows)
DISTINCT
andORDER BY
together:[3]SELECT DISTINCT city FROM weather ORDER BY city;
SELECT *
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.DISTINCT
automatically orders the rows and soORDER BY
is unnecessary. But this is not required by the SQL standard, and currentPostgreSQL does not guarantee thatDISTINCT
causes the rows to be ordered.