-
+
Queries
+ The previous chapters explained how to create tables, how to fill
+ them with data, and how to manipulate that data. Now we finally
+ discuss how to retrieve the data out of the database.
+
+
Overview
- A query is the process of retrieving or the command
- to retrieve data from a database. In SQL the SELECT
- command is used to specify queries. The general syntax of the
- SELECT command is
+ The process of retrieving or the command to retrieve data from a
+ database is called a query. In SQL the
+ SELECT command is used to specify queries. The
+ general syntax of the SELECT command is
SELECT select_list FROM table_expression sort_specification
A joined table is a table derived from two other (real or
derived) tables according to the rules of the particular join
- type. INNER, OUTER, and CROSS JOIN are supported.
+ type. Inner, outer, and cross-joins are available.
Join Types
- CROSS JOIN
+ Cross-join
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 returned 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 returned 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.
After the processing of the FROM clause is done, each row of the
derived table is checked against the search condition. If the
result of the condition is true, the row is kept in the output
- table, otherwise (that is, if the result is false or NULL) it is
+ table, otherwise (that is, if the result is false or null) it is
discarded. The search condition typically references at least some
column in the table generated in the FROM clause; this is not
required, but otherwise the WHERE clause will be fairly useless.
Obviously, two rows are considered distinct if they differ in at
- least one column value. NULLs are considered equal in this
+ least one column value. Null values are considered equal in this
comparison.
-
+
+(define draft-mode #f)
+
;; Don't show manpage volume numbers
;; it needs extra work.)
(define %callout-graphics% #f)
-;; Don't show comments. (We ought to show them, at least during the
-;; development stage.)
-(define %show-comments% #f)
+;; Show comments during the development stage.
+(define %show-comments% draft-mode)
;; Don't append period if run-in title ends with any of these
;; characters. We had to add the colon here. This is fixed in
(element structname ($mono-seq$))
(element symbol ($mono-seq$))
(element type ($mono-seq$))
+(element (programlisting emphasis) ($bold-seq$)) ;; to highlight sections of code
;; Indentation of verbatim environments
(define %indent-programlisting-lines% " ")
SQL
- This chapter describes the syntax of SQL.
-
-
+ This chapter describes the syntax of SQL. It forms the foundation
+ for understanding the following chapters which will go into detail
+ about how the SQL commands are applied to define and modify data.
+
+
+ We also advise users who are already familiar with SQL to read this
+ chapter carefully because there are several rules and concepts that
+ are implemented inconsistently among SQL databases or that are
+ specific to
PostgreSQL.
+
Lexical Structure
-
Schemas and naming conventions
+
Schemas and Naming Conventions
+
+
Schema Object Names
+
A database contains one or more named schemas>, which
in turn contain tables. Schemas also contain other kinds of named
place pg_catalog> at the end of your search path if you
prefer to have user-defined names override built-in names.
+
Reserved names
&intro;
&syntax;
+ &ddl;
+ &dml;
&queries;
&datatype;
&func;
&typeconv;
- &array;
&indices;
- &inherit;
&mvcc;
&manage;
&perform;