4 7.3.1. Select-List Items
8 As shown in the previous section, the table expression in the SELECT
9 command constructs an intermediate virtual table by possibly combining
10 tables, views, eliminating rows, grouping, etc. This table is finally
11 passed on to processing by the select list. The select list determines
12 which columns of the intermediate table are actually output.
14 7.3.1. Select-List Items #
16 The simplest kind of select list is * which emits all columns that the
17 table expression produces. Otherwise, a select list is a
18 comma-separated list of value expressions (as defined in Section 4.2).
19 For instance, it could be a list of column names:
20 SELECT a, b, c FROM ...
22 The columns names a, b, and c are either the actual names of the
23 columns of tables referenced in the FROM clause, or the aliases given
24 to them as explained in Section 7.2.1.2. The name space available in
25 the select list is the same as in the WHERE clause, unless grouping is
26 used, in which case it is the same as in the HAVING clause.
28 If more than one table has a column of the same name, the table name
29 must also be given, as in:
30 SELECT tbl1.a, tbl2.a, tbl1.b FROM ...
32 When working with multiple tables, it can also be useful to ask for all
33 the columns of a particular table:
34 SELECT tbl1.*, tbl2.a FROM ...
36 See Section 8.16.5 for more about the table_name.* notation.
38 If an arbitrary value expression is used in the select list, it
39 conceptually adds a new virtual column to the returned table. The value
40 expression is evaluated once for each result row, with the row's values
41 substituted for any column references. But the expressions in the
42 select list do not have to reference any columns in the table
43 expression of the FROM clause; they can be constant arithmetic
44 expressions, for instance.
46 7.3.2. Column Labels #
48 The entries in the select list can be assigned names for subsequent
49 processing, such as for use in an ORDER BY clause or for display by the
50 client application. For example:
51 SELECT a AS value, b + c AS sum FROM ...
53 If no output column name is specified using AS, the system assigns a
54 default column name. For simple column references, this is the name of
55 the referenced column. For function calls, this is the name of the
56 function. For complex expressions, the system will generate a generic
59 The AS key word is usually optional, but in some cases where the
60 desired column name matches a PostgreSQL key word, you must write AS or
61 double-quote the column name in order to avoid ambiguity. (Appendix C
62 shows which key words require AS to be used as a column label.) For
63 example, FROM is one such key word, so this does not work:
64 SELECT a from, b + c AS sum FROM ...
66 but either of these do:
67 SELECT a AS from, b + c AS sum FROM ...
68 SELECT a "from", b + c AS sum FROM ...
70 For greatest safety against possible future key word additions, it is
71 recommended that you always either write AS or double-quote the output
76 The naming of output columns here is different from that done in the
77 FROM clause (see Section 7.2.1.2). It is possible to rename the same
78 column twice, but the name assigned in the select list is the one that
83 After the select list has been processed, the result table can
84 optionally be subject to the elimination of duplicate rows. The
85 DISTINCT key word is written directly after SELECT to specify this:
86 SELECT DISTINCT select_list ...
88 (Instead of DISTINCT the key word ALL can be used to specify the
89 default behavior of retaining all rows.)
91 Obviously, two rows are considered distinct if they differ in at least
92 one column value. Null values are considered equal in this comparison.
94 Alternatively, an arbitrary expression can determine what rows are to
95 be considered distinct:
96 SELECT DISTINCT ON (expression [, expression ...]) select_list ...
98 Here expression is an arbitrary value expression that is evaluated for
99 all rows. A set of rows for which all the expressions are equal are
100 considered duplicates, and only the first row of the set is kept in the
101 output. Note that the “first row” of a set is unpredictable unless the
102 query is sorted on enough columns to guarantee a unique ordering of the
103 rows arriving at the DISTINCT filter. (DISTINCT ON processing occurs
104 after ORDER BY sorting.)
106 The DISTINCT ON clause is not part of the SQL standard and is sometimes
107 considered bad style because of the potentially indeterminate nature of
108 its results. With judicious use of GROUP BY and subqueries in FROM,
109 this construct can be avoided, but it is often the most convenient