This article introduces the relevant concepts of data queries.
Overview
The process of retrieving or the statement to retrieve data from a database is called a query. In SQL the SELECT statement is used to specify queries. See the following general syntax of the SELECT statement:
[WITH with_queries] SELECT select_list FROM table_expression [sort_specification]The following sections describe the details of the select list, the table expression, and the sort specification.WITH queries are treated last since they are an advanced feature.
See the following example of a simple query:
SELECT * FROM table1;This statement retrieves all rows and all user-defined columns from a table named table1. The method of retrieval depends on the client application. For example, the psql program displays an ASCII-art table on the screen, while client libraries offers functions to extract individual values from the query result. The select list specification * means all columns that the table expression provides. A select list can also select a subset of the available columns or make calculations using the columns. For example, if table1 contains multiple columns including a, b, and c, you can make the following query:
SELECT a, b + c FROM table1;In the preceding example, column b and c are of the numerical data type.
FROM table1 is a simple table expression that reads just one table. In general, table expressions can be complex constructs of base tables, joins, and subqueries. But you can also omit the table expression entirely and use the SELECT statement as a calculator:
SELECT 3 * 4;This is useful if the expressions in the select list return varying results. For example, you can call a function by using the following method:
SELECT random();