SQLinaNutshell4thedition Preview
SQLinaNutshell4thedition Preview
Meander Smith KS
Morningstar Greene TN
Reginald Blotchet-Halls OR
Innes del Castillo MI
The resulting data is sometimes called a result set, work table, or derived table,
differentiating it from the base table in the database that is the target of the SELECT
statement.
It is important to note that the relational operation of projection, not selection, is
specified using the SELECT clause (that is, the keyword SELECT followed by a list
of expressions to be retrieved) of a SELECT statement. Selection—the operation of
retrieving specific rows of data—is specified using the WHERE clause in a SELECT
statement. WHERE filters out unwanted rows of data and retrieves only the reques‐
ted rows. Continuing with the previous example, the following statement selects
authors from states other than California:
SELECT au_fname, au_lname, state
FROM authors
WHERE state <> 'CA';
Whereas the first query retrieved all authors, the result of this second query is a
much smaller set of records:
au_fname au_lname state
---------------- ----------------------------------- -------------
Meander Smith KS
Morningstar Greene TN
Reginald Blotchet-Halls OR
Innes del Castillo MI
SQL History
use SQL to retrieve only the columns and records that you need at any given time.
Joins are the next, and last, relational operation covered in this section. A join
relates one table to another in order to return a result set consisting of related data
from both tables.
The ANSI/ISO standard method of performing joins is to use the JOIN clause
in a SELECT statement. An older method, sometimes called a theta join, analyzes
the join search argument in the WHERE clause. The following example shows both
approaches. Each statement retrieves employee information from the employee base
table as well as job descriptions from the jobs base table. The first SELECT uses the
newer ANSI/ISO SQL JOIN clause, while the second SELECT uses a theta join:
-- ANSI/ISO style
SELECT a.au_fname, a.au_lname, t.title_id
FROM authors AS a
JOIN titleauthor AS t ON a.au_id = t.au_id
WHERE a.state <> 'CA';
-- Theta style
SELECT a.au_fname, a.au_lname, t.title_id
FROM authors AS a,
titleauthor AS t
WHERE a.au_id = t.au_id
AND a.state <> 'CA';
Although theta joins are universally supported across the various platforms and
incur no performance penalty, this is considered an inferior coding pattern because
anyone reading or maintaining the query cannot immediately discern the argu‐
ments used to define the join condition from those used as filtering conditions.
There are more issues with theta joins than ease of readability.
For example, Oracle allows only one outer join comparison
using (+), which can be quite a problem when logically con‐
structing an outer join on a multicolumn key. In a situation
like this, it can be difficult to avoid syntax errors or even
retrieving the wrong result set.
For more information about joins, see the <JOIN Subclause= on page 278.
Each revision of the SQL standard, officially known as ISO/IEC 9075 Database
Language SQL, adds new features and incorporates new commands and capabilities
into the language. Here is a brief list of the releases and some of their major
contributions (features or specifications):
SQL-87
Standard first formalized by ANSI; support for transactions and CREATE, READ,
UPDATE, and DELETE operations
SQL-89
Minor revision, added referential integrity constraints
SQL-92
Major revision (ISO 9075), added support for internationalization, etc.
SQL:1999
Added support for user-defined types, regular expression mapping, triggers,
procedural and control-flow statements, and more
SQL:2003
Added support for XML and OLAP (window functions), sampling, and
enhanced numeric functions
SQL:2006
Clarified how SQL and SML interact and added support for XQuery
SQL:2008
Incorporated various improvements and enhancements that had been made
in several of the most prominent RDBMS platforms (INSTEAD OF triggers,
TRUNCATE statement, FETCH clause, etc.) and expanded the XML specification
SQL:2011
Introduced new features for managing temporal data
SQL:2016
Described how SQL interacts with JavaScript Object Notation (JSON) and
added support for polymorphic table functions and row pattern matching
SQL:2019
Described how SQL interacts with multidimensional arrays (MDAs)
SQL History
guage) extension are not yet supported in the products considered in this edition of
SQL in a Nutshell, so our coverage in this book extends only to SQL:2016. SQL:2019
coverage will come in future editions.
Levels of Conformance
SQL-92 introduced three levels of conformance indicating degrees of compliance
with the standard: Entry, Intermediate, and Full (the U.S. National Institute of
Standards and Technology later added a Transitional level between the first two).
Vendors had to achieve at least Entry-level conformance to claim ANSI/ISO SQL
compliance. Each higher level of the standard was a superset of the subordinate
level, meaning that each higher level included all the features of the lower levels of
conformance.
Later, SQL:1999 altered the base levels of conformance, doing away with the original
three categories. Instead, it required vendors to implement a minimum set of
mandatory features collectively called <Core SQL= in order to claim (and advertise)
that they are SQL:1999 compliant. This included the old Entry Level SQL-92 feature
set, features from other SQL-92 levels, and some new features. Vendors could also
opt to implement additional parts of the standard, described in the next section.
This book details the SQL implementations of four popular RDBMSs. These ven‐
dors do not fully support everything defined in the SQL standard; all RDBMS