-
+
Queries
The FROM Clause
- The FROM> clause derives a table from one or more other
- tables given in a comma-separated table reference list.
+ The derives a
+ table from one or more other tables given in a comma-separated
+ table reference list.
FROM table_reference , table_reference , ...
- The syntax of the WHERE> clause is
+ The syntax of the
+ endterm="sql-where-title"> clause is
WHERE search_condition
- The GROUP BY> clause is used to group together those rows in
- a table that share the same values in all the columns listed. The
- order in which the columns are listed does not matter. The
- purpose is to reduce each group of rows sharing common values into
- one group row that is representative of all rows in the group.
- This is done to eliminate redundancy in the output and/or compute
- aggregates that apply to these groups. For instance:
+ The is
+ used to group together those rows in a table that share the same
+ values in all the columns listed. The order in which the columns
+ are listed does not matter. The purpose is to reduce each group
+ of rows sharing common values into one group row that is
+ representative of all rows in the group. This is done to
+ eliminate redundancy in the output and/or compute aggregates that
+ apply to these groups. For instance:
=>> SELECT * FROM test1;>
x | y
SELECT DISTINCT select_list ...
- (Instead of DISTINCT> the word ALL
+ (Instead of DISTINCT> the key word ALL
can be used to select the default behavior of retaining all rows.)
- FROM-clause elements can contain:
+ The FROM clause can contain the following
+ elements:
A JOIN clause combines two
FROM> items. Use parentheses if necessary to
determine the order of nesting. In the absence of parentheses,
- JOINs nest left-to-right. In any case
- JOIN binds more tightly than the commas
- separating FROM> items.
+ JOINs nest left-to-right. In any case
+ JOIN binds more tightly than the commas
+ separating FROM> items.
CROSS JOIN> and INNER JOIN
produce a simple Cartesian product, the same result as you get from
listing the two items at the top level of FROM>,
- but restricted by the join condition (if any).
+ but restricted by the join condition (if any).
CROSS JOIN> is equivalent to INNER JOIN ON
(TRUE)>, that is, no rows are removed by qualification.
These join types are just a notational convenience, since they
INTERSECT>, or EXCEPT> clause may only
specify an output column name or number, not an expression.
-
+
If an ORDER BY> expression is a simple name that
matches both a result column name and an input column name,
UPDATE changes the values of the specified
columns in all rows that satisfy the condition. Only the columns to
- be modified need be mentioned in the statement; columns not explicitly
- SET> retain their previous values.
+ be modified need be mentioned in the SET clause;
+ columns not explicitly modified retain their previous values.
clause.
+ There are two ways to modify a table using information contained in
+ other tables in the database: using sub-selects, or specifying
+ additional tables in the FROM clause. Which
+ technique is more appropriate depends on the specific
+ circumstances.
+
+
You must have the UPDATE privilege on the table
to update it, as well as the SELECT
A list of table expressions, allowing columns from other tables
- to appear in the WHERE> condition and the update expressions.
+ to appear in the WHERE> condition and the update
+ expressions. This is similar to the list of tables that can be
+ specified in the
+ endterm="sql-from-title"> of a SELECT
+ statement; for example, an alias for the table name can be
+ specified.
Change the word Drama> to Dramatic> in the
- column kind> of the table <literal>films>:
+ column kind> of the table <structname>films>:
UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
Adjust temperature entries and reset precipitation to its default
- value in one row of the table <literal>weather>:
+ value in one row of the table <structname>weather>:
UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
+ Increment the sales count of the salesperson who manages the
+ account for Acme Corporation, using the FROM
+ clause syntax:
+UPDATE employees SET sales_count = sales_count + 1 FROM accounts
+ WHERE accounts.name = 'Acme Corporation'
+ AND employees.id = accounts.sales_person;
+
+
+ Perform the same operation, using a sub-select in the
+ WHERE clause:
+UPDATE employees SET sales_count = sales_count + 1 WHERE id =
+ (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
+
+
Compatibility
- This command conforms to the SQL standard. The
+ This command conforms to the
SQL standard. The
FROM clause is a
The function trigf> reports the number of rows in the
- table <literal>ttest> and skips the actual operation if the
+ table <structname>ttest> and skips the actual operation if the
command attempts to insert a null value into the column
- <literal>x>. (So the trigger acts as a not-null constraint but
+ <structfield>x>. (So the trigger acts as a not-null constraint but
doesn't abort the transaction.)