6.1.2. The SELECT Columns List

The columns list contains one or more comma-separated value expressions. Each expression provides a value for one output column. Alternatively, * (“select star”) can be used to stand for all the columns in a relation (i.e. a table, view or selectable stored procedure).

Syntax

  1. SELECT
  2. [...]
  3. [DISTINCT | ALL] <output-column> [, <output-column> ...]
  4. [...]
  5. FROM ...
  6. <output-column> ::=
  7. [<qualifier>.]*
  8. | <value-expression> [COLLATE collation] [[AS] alias]
  9. <value-expression> ::=
  10. [<qualifier>.]table-column
  11. | [<qualifier>.]view-column
  12. | [<qualifier>.]selectable-SP-outparm
  13. | <literal>
  14. | <context-variable>
  15. | <function-call>
  16. | <single-value-subselect>
  17. | <CASE-construct>
  18. | any other expression returning a single
  19. value of a Firebird data type or NULL
  20. <qualifier> ::= a relation name or alias
Table 63. Arguments for the SELECT Columns List
ArgumentDescription

qualifier

Name of relation (view, stored procedure, derived table); or an alias for it

collation

Only for character-type columns: a collation name that exists and is valid for the character set of the data

alias

Column or field alias

table-column

Name of a table column

view-column

Name of a view column

selectable-SP-outparm

Declared name of an output parameter of a selectable stored procedure

constant

A constant

context-variable

Context variable

function-call

Scalar or aggregate function call expression

single-value-subselect

A subquery returning one scalar value (singleton)

CASE-construct

CASE construct setting conditions for a return value

other-single-value-expr

Any other expression returning a single value of a Firebird data type; or NULL

Description

It is always valid to qualify a column name (or “*”) with the name or alias of the table, view or selectable SP to which it belongs, followed by a dot (‘.’). For example, relationname.columnname, relationname.*, alias.columnname, alias.*. Qualifying is required if the column name occurs in more than one relation taking part in a join. Qualifying “*” is always mandatory if it is not the only item in the column list.

Aliases obfuscate the original relation name: once a table, view or procedure has been aliased, only the alias can be used as its qualifier throughout the query. The relation name itself becomes unavailable.

The column list may optionally be preceded by one of the keywords DISTINCT or ALL:

  • DISTINCT filters out any duplicate rows. That is, if two or more rows have the same values in every corresponding column, only one of them is included in the result set

  • ALL is the default: it returns all of the rows, including duplicates. ALL is rarely used; it is supported for compliance with the SQL standard.

A COLLATE clause will not change the appearance of the column as such. However, if the specified collation changes the case or accent sensitivity of the column, it may influence:

  • The ordering, if an ORDER BY clause is also present and it involves that column

  • Grouping, if the column is part of a GROUP BY clause

  • The rows retrieved (and hence the total number of rows in the result set), if DISTINCT is used

Examples of SELECT queries with different types of column lists

A simple SELECT using only column names:

  1. select cust_id, cust_name, phone
  2. from customers
  3. where city = 'London'

A query featuring a concatenation expression and a function call in the columns list:

  1. select 'Mr./Mrs. ' || lastname, street, zip, upper(city)
  2. from contacts
  3. where date_last_purchase(id) = current_date

A query with two subselects:

  1. select p.fullname,
  2. (select name from classes c where c.id = p.class) as class,
  3. (select name from mentors m where m.id = p.mentor) as mentor
  4. from pupils p

The following query accomplishes the same as the previous one using joins instead of subselects:

  1. select p.fullname,
  2. c.name as class,
  3. m.name as mentor
  4. join classes c on c.id = p.class
  5. from pupils p
  6. join mentors m on m.id = p.mentor

This query uses a CASE construct to determine the correct title, e.g. when sending mail to a person:

  1. select case upper(sex)
  2. when 'F' then 'Mrs.'
  3. when 'M' then 'Mr.'
  4. else ''
  5. end as title,
  6. lastname,
  7. address
  8. from employees

Querying a selectable stored procedure:

  1. select * from interesting_transactions(2010, 3, 'S')
  2. order by amount

Selecting from columns of a derived table. A derived table is a parenthesized SELECT statement whose result set is used in an enclosing query as if it were a regular table or view. The derived table is shown in bold here:

  1. select fieldcount,
  2. count(relation) as num_tables
  3. from (select r.rdb$relation_name as relation,
  4. count(*) as fieldcount
  5. from rdb$relations r
  6. join rdb$relation_fields rf
  7. on rf.rdb$relation_name = r.rdb$relation_name
  8. group by relation)
  9. group by fieldcount

Asking the time through a context variable (CURRENT_TIME):

  1. select current_time from rdb$database

For those not familiar with RDB$DATABASE: this is a system table that is present in all Firebird databases and is guaranteed to contain exactly one row. Although it wasn’t created for this purpose, it has become standard practice among Firebird programmers to select from this table if you want to select “from nothing”, i.e., if you need data that are not bound to a any table or view, but can be derived from the expressions in the output columns alone. Another example is:

  1. select power(12, 2) as twelve_squared, power(12, 3) as twelve_cubed
  2. from rdb$database

Finally, an example where you select some meaningful information from RDB$DATABASE itself:

  1. select rdb$character_set_name from rdb$database

As you may have guessed, this will give you the default character set of the database.

See also

Functions, Aggregate Functions, Context Variables, CASE, Subqueries