- linkend="queries-select-lists">. For example, if
+ subset of the available columns or make calculations using the
+ columns. For example, if
table1 has columns named a>,
b>, and c> (and perhaps others) you can make
the following query:
(assuming that b> and c> are of a numerical
data type).
+ See for more details.
SELECT 3 * 4;
This is more useful if the expressions in the select list return
- varying results. For example, you could call a function this way.
+ varying results. For example, you could call a function this way:
SELECT random();
First, an inner join is performed. Then, for each row in
T1 that does not satisfy the join condition with any row in
- T2, a joined row is returned with null values in columns of
+ T2, a joined row is added with null values in columns of
T2. Thus, the joined table unconditionally has at least
one row for each row in T1.
First, an inner join is performed. Then, for each row in
T2 that does not satisfy the join condition with any row in
- T1, a joined row is returned with null values in columns of
+ T1, a joined row is added with null values in columns of
T1. This is the converse of a left join: the result table
will unconditionally have a row for each row in T2.
First, an inner join is performed. Then, for each row in
T1 that does not satisfy the join condition with any row in
- T2, a joined row is returned with null values in columns of
+ T2, a joined row is added with null values in columns of
T2. Also, for each row of T2 that does not satisfy the
join condition with any row in T1, a joined row with null
- values in the columns of T1 is returned.
+ values in the columns of T1 is added.
JOIN> syntax in the FROM> clause is
probably not as portable to other SQL database products. For
outer joins there is no choice in any case: they must be done in
- the FROM> clause. A ON>/USING>
+ the FROM> clause. An ON>/USING>
clause of an outer join is not> equivalent to a
WHERE> condition, because it determines the addition
of rows (for unmatched input rows) as well as the removal of rows
In the second query, we could not have written SELECT *
- FROM test1 GROUP BY x;, because there is no single value
+ FROM test1 GROUP BY x, because there is no single value
for the column y> that could be associated with each
group. In general, if a table is grouped, columns that are not
used in the grouping cannot be referenced except in aggregate
- expressions, for example:
+ expressions. An example with aggregate expressions is:
=>> SELECT x, sum(y) FROM test1 GROUP BY x;>
x | sum
they have a known constant value per group.
- <note>
+ <tip>
Grouping without aggregate expressions effectively calculates the
set of distinct values in a column. This can also be achieved
using the
DISTINCT> clause (see
linkend="queries-distinct">).
- note>
+ tip>
- Here is another example: A sum(sales) on a
+ Here is another example: sum(sales) on a
table grouped by product code gives the total sales for each
product, not the total sales on all products.
SELECT product_id, p.name, (sum(s.units) * p.price) AS sales
FROM products p LEFT JOIN sales s USING (product_id)
- GROUP BY pid, p.name, p.price;
+ GROUP BY product_id, p.name, p.price;
- In this example, the columns pid,
+ In this example, the columns product_id,
p.name, and p.price must be
in the GROUP BY> clause since they are referenced in
the query select list. (Depending on how exactly the products
SELECT select_list FROM ... WHERE ... GROUP BY ... HAVING boolean_expression
Expressions in the HAVING> clause can refer both to
- grouped expressions and to ungrouped expression (which necessarily
+ grouped expressions and to ungrouped expressions (which necessarily
involve an aggregate function).
Again, a more realistic example:
SELECT product_id, p.name, (sum(s.units) * (p.price - p.cost)) AS profit
- FROM products p LEFT JOIN sales s USING (pid)
+ FROM products p LEFT JOIN sales s USING (product_id)
WHERE s.date > CURRENT_DATE - INTERVAL '4 weeks'
GROUP BY product_id, p.name, p.price, p.cost
HAVING sum(p.price * s.units) > 5000;
If more than one sort column is specified, the later entries are
used to sort rows that are equal under the order imposed by the
- earlier sort specifications.
+ earlier sort columns.
Constants that are not simple numeric values usually must be
surrounded by single quotes ('>), as in the example.
The
- date column is actually quite flexible in what it
+ date type is actually quite flexible in what it
accepts, but for this tutorial we will stick to the unambiguous
format shown here.
where the file name for the source file must be available to the
backend server machine, not the client, since the backend server
reads the file directly. You can read more about the
- COPY command in the Reference
- Manual.
+ COPY command in &cite-reference;.
As a final note, you can request that the results of a select can
- be returned in sorted order or with duplicate rows removed. (Just
- to make sure the following won't confuse you,
- DISTINCT and ORDER BY can be
- used separately.)
+ be returned in sorted order or with duplicate rows removed:
SELECT DISTINCT city
San Francisco
(2 rows)
+
+ DISTINCT and ORDER BY can be
+ used separately, of course.
If you are installing
PostgreSQL
- yourself, then refer to the Administrator's
- Guide for instructions on installation, and return to
+ yourself, then refer to &cite-admin;
+ for instructions on installation, and return to
this guide when the installation is complete. Be sure to follow
closely the section about setting up the appropriate environment
variables.
The user's client (frontend) application that wants to perform
database operations. Client applications can be very diverse
- in nature: They could be a text-oriented tool, a graphical
+ in nature: a client could be a text-oriented tool, a graphical
application, a web server that accesses the database to
display web pages, or a specialized database maintenance tool.
Some client applications are supplied with the
Writing a custom application, using one of the several
available language bindings. These possibilities are discussed
- further in The PostgreSQL Programmer's
- Guide.
+ further in &cite-programmer;.
version
----------------------------------------------------------------
- PostgreSQL 7.2devel on i586-pc-linux-gnu, compiled by GCC 2.96
+ PostgreSQL 7.3devel on i586-pc-linux-gnu, compiled by GCC 2.96
(1 row)
mydb=> SELECT current_date;
date
------------
- 2001-08-31
+ 2002-08-31
(1 row)
and psql will quit and return you to your
command shell. (For more internal commands, type
\? at the psql prompt.) The
- full capabilities of psql are documented in the
-
Reference Manual. If
PostgreSQL> is
+ full capabilities of psql are documented in
+
&cite-reference;. If
PostgreSQL> is
installed correctly you can also type man psql
at the operating system shell prompt to see the documentation. In
this tutorial we will not use these features explicitly, but you
a SET token to appear in a certain position, and
this particular variation of INSERT also
requires a VALUES in order to be complete. The
- precise syntax rules for each command are described in the
- Reference Manual.
+ precise syntax rules for each command are described in
+ &cite-reference;.
- There are four kinds of implicitly-typed
+ There are three kinds of implicitly-typed
- strings, bit strings, integers, and floating-point numbers.
+ strings, bit strings, and numbers.
Constants can also be specified with explicit types, which can
enable more accurate representation and more efficient handling by
the system. The implicit constants are described below; explicit
SELECT 'foo' 'bar';
- is not valid syntax, and
PostgreSQL is
- consistent with
SQL9x in this regard.
+ is not valid syntax. (This slightly bizarre behavior is specified
+ by
SQL9x;
PostgreSQL is
+ following the standard.)
opening quote (no intervening whitespace), e.g.,
B'1001'. The only characters allowed within
bit-string constants are 0 and
- 1. Bit-string constants can be continued
- across lines in the same way as regular string constants.
+ 1.
-
-
-
Integer Constants
+ Alternatively, bit-string constants can be specified in hexadecimal
+ notation, using a leading X (upper or lower case),
+ e.g., X'1FF'. This notation is equivalent to
+ a bit-string constant with four binary digits for each hex digit.
+
- Integer constants in SQL are sequences of decimal digits (0
- though 9) with no decimal point and no exponent. The range of legal values
- depends on which integer data type is used, but the plain
- integer type accepts values ranging from -2147483648
- to +2147483647. (The optional plus or minus sign is actually a
- separate unary operator and not part of the integer constant.)
+ Both forms of bit-string constant can be continued
+ across lines in the same way as regular string constants.
-
Floating-Point Constants
+
Numeric Constants
constants
- Floating-point constants are accepted in these general forms:
+ Numeric constants are accepted in these general forms:
+digits
digits.digitse+-digits
digits.digitse+-digits
digitse+-digits
where digits is one or more decimal
- digits. At least one digit must be before or after the decimal
- point. At least one digit must follow the exponent delimiter
- (e) if that field is present.
- Thus, a floating-point constant is distinguished from an integer
- constant by the presence of either the decimal point or the
- exponent clause (or both). There must not be a space or other
- characters embedded in the constant.
+ digits (0 through 9). At least one digit must be before or after the
+ decimal point, if one is used. At least one digit must follow the
+ exponent marker (e), if one is present.
+ There may not be any spaces or other characters embedded in the
+ constant. Notice that any leading plus or minus sign is not actually
+ considered part of the constant; it is an operator applied to the
+ constant.
- These are some examples of valid floating-point constants:
+ These are some examples of valid numeric constants:
+42
3.5
4.
.001
- Floating-point constants are of type DOUBLE
- PRECISION. REAL can be specified explicitly
- by using
SQL string notation or
-
PostgreSQL type notation:
+ A numeric constant that contains neither a decimal point nor an
+ exponent is initially presumed to be type integer> if its
+ value fits in type integer> (32 bits); otherwise it is
+ presumed to be type bigint> if its
+ value fits in type bigint> (64 bits); otherwise it is
+ taken to be type numeric>. Constants that contain decimal
+ points and/or exponents are always initially presumed to be type
+ numeric>.
+
+
+ The initially assigned data type of a numeric constant is just a
+ starting point for the type resolution algorithms. In most
+ cases the constant will be automatically coerced to the most
+ appropriate type depending on context. When necessary, you
+ can force a numeric value to be interpreted as a specific
+ data type by casting it. For example, you can force a numeric
+ value to be treated as type real> (float4>)
+ by writing
REAL '1.23' -- string style
-'1.23'::REAL -- PostgreSQL (historical) style
+1.23::REAL -- PostgreSQL (historical) style
table (possibly qualified), or an alias for a table defined by means of a
FROM clause, or
the key words NEW or OLD.
- (NEW and OLD can only appear in the action portion of a rule,
+ (NEW and OLD can only appear in rules,
while other correlation names can be used in any SQL statement.)
The correlation name and separating dot may be omitted if the column name
is unique
A positional parameter reference is used to indicate a parameter
- in an SQL function. Typically this is used in SQL function
- definition statements. The form of a parameter is:
+ that is supplied externally to an SQL statement. Parameters are
+ used in SQL function definitions and in prepared queries.
+ The form of a parameter reference is:
$number
- After you have worked through this tutorial you might want to move on
- to reading the User's
- Guide]]>]]> to
- gain a more formal knowledge of the SQL language, or the
- Programmer's
- Guide]]>]]>
- for information about developing applications for
+ After you have worked through this tutorial you might want to move on to
+ reading &cite-user; to gain a more formal knowledge of the SQL language,
+ or &cite-programmer; for information about developing applications for