]> begriffs open source - ai-pg/blob - full-docs/txt/queries-overview.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / queries-overview.txt
1
2 7.1. Overview #
3
4    The process of retrieving or the command to retrieve data from a
5    database is called a query. In SQL the SELECT command is used to
6    specify queries. The general syntax of the SELECT command is
7 [WITH with_queries] SELECT select_list FROM table_expression [sort_specification
8 ]
9
10    The following sections describe the details of the select list, the
11    table expression, and the sort specification. WITH queries are treated
12    last since they are an advanced feature.
13
14    A simple kind of query has the form:
15 SELECT * FROM table1;
16
17    Assuming that there is a table called table1, this command would
18    retrieve all rows and all user-defined columns from table1. (The method
19    of retrieval depends on the client application. For example, the psql
20    program will display an ASCII-art table on the screen, while client
21    libraries will offer functions to extract individual values from the
22    query result.) The select list specification * means all columns that
23    the table expression happens to provide. A select list can also select
24    a subset of the available columns or make calculations using the
25    columns. For example, if table1 has columns named a, b, and c (and
26    perhaps others) you can make the following query:
27 SELECT a, b + c FROM table1;
28
29    (assuming that b and c are of a numerical data type). See Section 7.3
30    for more details.
31
32    FROM table1 is a simple kind of table expression: it reads just one
33    table. In general, table expressions can be complex constructs of base
34    tables, joins, and subqueries. But you can also omit the table
35    expression entirely and use the SELECT command as a calculator:
36 SELECT 3 * 4;
37
38    This is more useful if the expressions in the select list return
39    varying results. For example, you could call a function this way:
40 SELECT random();