Preface
Purpose
Audience
Supported Software Release
Prerequisites
Changes to This Book
Additional Information
References to Microsoft Windows and Linux
Table of Contents
Chapter 1 The SELECT Statement
SELECT
ANSI Compliance
Authorization
Rules for UDTs
Rules for Embedded SQL
Rules for Using the DEFAULT Function With SELECT
Rules for Returning a Varying Column Table UDF Result
System Exerts a READ LOCK on the Tables or Rows Referenced By a SELECT Statement
Only SELECT Uses Set Operators
Joins
Success Response Indicates the Number of Rows Returned
Uses of the SELECT Statement
SELECT Subqueries
SELECT and Queue Tables
Related Topics
Simple SELECT Statement
SELECT AND CONSUME
ANSI Compliance
Authorization
Attributes of a Queue Table
Transaction Processing
Locks
FIFO Order Is Approximate
Rules for Embedded SQL
Restrictions
Performance Characteristics
Related Topics
SELECT … INTO
WITH RECURSIVE Clause
ANSI Compliance
How Nonrecursive WITH Works
How WITH RECURSIVE Works
WITH RECURSIVE Without a Recursive Statement
Using a WITH Clause Result as Input to a Table Function in the FROM Clause
Depth Control to Avoid Infinite Recursion
WITH and WITH RECURSIVE Restrictions
SQL Objects That Cannot Specify WITH or WITH RECURSIVE
Restrictions for Embedded SQL
Related Topics
DISTINCT, ALL, and .ALL Options
ANSI Compliance
DISTINCT Operator and UDTs
DISTINCT and Large Objects
DISTINCT and the WITH Clause
DISTINCT and the TOP n Operator
DISTINCT and Recursive Queries
Reason for Unexpected Row Length Errors: Sorting Rows Before Eliminating Duplicates
.ALL Option and Structured UDTs
UDT And Table Definitions For the Example Sets
TOP n Operator
ANSI Compliance
About TOP n
Restrictions
Evaluation Order
TOP n Operator Replaces QUALIFY RANK and QUALIFY ROW_NUMBER
FROM Clause
ANSI Compliance
When Fully Qualified Names Are Required
FROM Clause Required for Subqueries
Self-Joins
FROM Clause Using Both a Permanent Table Name and a Correlation Name
How Column References Affect Self-Joins
Derived Tables
Introduction
ANSI Compliance
Uses for Derived Tables
Rules for Derived Tables
Derived Tables and Multilevel Aggregates
Restricted Use With SELECT AND CONSUME Requests
WHERE Clause
ANSI Compliance
Restricted Use With Aggregates
Restricted Use With Large Objects
Restricted Use With UDTs
Restricted Use With SELECT AND CONSUME Requests
Rules For Using a DEFAULT Function As Part of the Search Condition Of a WHERE Clause
WHERE With AND and OR Operators
WHERE In Character String Searches
WHERE Usage In Join Conditions
Join Efficiency and Indexes
EXISTS Quantifier
Unconstrained Joins
System-Derived PARTITION and PARTITION#Ln Columns
Subqueries in Search Conditions
ANSI Compliance
Restriction on Number of Subqueries Permitted
Restricted Use With SELECT AND CONSUME Statements
Restricted Use With TOP n Operator
Correlated Subqueries
Definitions
ANSI Compliance
Behavior of Outer References
Rules for Using Correlated Subqueries
When Uncorrelated Subqueries Are Desired
Comparing the Behavior of Uncorrelated and Correlated Subqueries
Combining Equality and Inequality Constraints in a Correlated Subquery
Correlated Subqueries and the SELECT Statement
Rules for Using Correlated Subqueries in a SELECT Statement
SELECT COUNT(*)
EXISTS Quantifier and Correlated Subqueries
GROUP BY Clause
ANSI Compliance
GROUP BY Clause Terminology
Aggregate Handling
GROUP BY and Ordered Analytical Functions
GROUP BY and Large Objects
GROUP BY and Recursive Queries
Combining GROUP BY With ORDER BY
Reason for Unexpected Row Length Errors: Sorting Rows for Grouping
Evaluation Order of WHERE, GROUP BY, and HAVING Clauses
Examples
How Extended GROUP BY Operator Specifications Are Resolved
Introduction
CUBE Option
ANSI Compliance
SAMPLE Clause and CUBE
How CUBE Summarizes Data
GROUPING SETS Option
ANSI Compliance
SAMPLE Clause and GROUPING SETS
How GROUPING SETS Summarizes Data
ROLLUP Option
ANSI Compliance
SAMPLE Clause and ROLLUP
How ROLLUP Summarizes Data
HAVING Clause
ANSI Compliance
HAVING And UDTs
HAVING And Large Objects
HAVING And Aggregate Functions
HAVING and Recursive Queries
Rules For Using a DEFAULT Function As Part of the Search Condition Of a HAVING Clause
Evaluation Order of WHERE, GROUP BY, and HAVING Clauses
HAVING Error Conditions
Aggregating a Join In a HAVING Clause
QUALIFY Clause
ANSI Compliance
QUALIFY And UDTs
QUALIFY And Large Objects
QUALIFY and Complex Joins Involving Subqueries
QUALIFY and Statistical Functions
Rules For Using a DEFAULT Function As Part of the Search Condition Of a QUALIFY Clause
Evaluation Order of WHERE, GROUP BY, and QUALIFY Clauses
TOP n Operator and QUALIFY
QUALIFY Error Conditions
SAMPLE Clause
ANSI Compliance
About SAMPLE
Rules for Using the SAMPLE Clause
GROUP BY and SAMPLE
TOP n Operator and SAMPLE
Simple Random Sampling
Stratified Random Sampling
Sampling With Or Without Replacement
Randomized and Proportional Row Allocation
Examples Using Stratified Sampling
SAMPLEID Expression
ANSI Compliance
Definition
Rules for Using SAMPLEID
Using SAMPLEID With Stratified Sampling
ORDER BY Clause
ANSI Compliance
Column Name
Column Position
Combining ORDER BY And WITH Clauses
ORDER BY and UDTs
Reason for Unexpected Row Length Errors: Sorting and Default Sort Order
Specifying Collation
Case Sensitivity
Japanese Character Sort Order Considerations
International Sort Orders
European Sort Order
WITH Clause
ANSI Compliance
Expressions In A WITH Clause
WITH And UDTs
WITH And LOBs
A WITH Clause Is Not Allowed When You Specify The TOP n Operator
Using TITLE Phrases
How Multiple WITH Clauses Work Together
Combining WITH And ORDER BY Clauses
Combining WITH and GROUP BY Clauses
Chapter 2 Join Expressions
Joins
Definition
Join Varieties
Natural and Theta Joins
Inner and Outer Joins
Inner Joins
Ordinary Inner Join
Definitions: Join and Inner Join
Components of an Inner Join
The Default Join
Joins on Views Containing an Aggregate
Cross Join
Definition
Why Perform a Cross Join?
How to Write a Cross Join
Self-Join
Definition
Outer Joins
Definition of the Outer Join
Definition: Outer Join
Syntax
Simple Outer Join Example
Outer Join Is Equivalent to Inner Join for Selected Foreign Key-Primary Key Relationships
Components of an Outer Join
Types of Outer Join
Terminology
Inner/Outer Table Example
Projected Column List
Nulls and the Outer Join
Example
Rules for Using the DEFAULT Function as a Search Condition for an Outer Join ON Clause
Use of the DEFAULT Function in an Outer Join ON Condition
Review of Relevant Relational Algebra for the Outer Join
Basic SQL Query
Relational Algebra for the Outer Join
Left Outer Join
Definition
Inner/Outer Table Example
Practical Example
Right Outer Join
Definition
Inner/Outer Table Example
Practical Example
Full Outer Join
Definition
Inner/Outer Table Example
Practical Example
Multitable Joins
Introduction
Definition: Temporary Derived Table
ON Clause Evaluation Order
Example 1a
Example 1b
Example 2
Coding ON Clauses for Outer Joins
Introduction
Comparing the ON Clause Join Conditions of Examples 1 and 2
Related Topics
Coding ON Clauses With WHERE Clauses for Outer Joins
Introduction
Use Join Conditions in ON Clauses, Not Search Conditions
Using Search Conditions In ON Clauses
A Guide to Placing Search Conditions in a Join
Related Topics
Rules And Recommendations for Coding ON and WHERE Clauses for Outer Joins
Outer Join Case Study
Acknowledgement
Purpose of the Case Study
Topics for the Case Study
Case Study Examples
Introduction
Defining the Business Question
Heuristics for Determining a Reasonable Answer Set
Introduction
Single Table Cardinality Estimate
Inner Join Cardinality Estimate
First Attempt
Brief Analysis of the Result
EXPLAIN Text for Example 1
Analysis of the EXPLAIN Text
The Coding Error
Second Attempt
Example 2: Outer Join But Does Not Return the Correct Answer
Brief Analysis of the Result
EXPLAIN Text for Example 2
Analysis of the EXPLAIN Text
The Coding Error
Third Attempt
Example 3: Outer Join That Is Really an Inner Join
Brief Analysis of the Result
EXPLAIN Text for Example 3
Analysis of the EXPLAIN Text
The Coding Error
Final Attempt: The Correct Answer
Example 4: Outer Join
Brief Analysis of the Result
EXPLAIN Text for Example 4
Analysis of the EXPLAIN Text
Guidelines for Getting the Desired Answer Using Outer Joins
Introduction
Guidelines
Chapter 3 SQL Data Manipulation Language Statement Syntax
Null
ANSI Compliance
Authorization
Invocation
Definition: Null Statement
ABORT
ANSI Compliance
Authorization
How ANSI Transactions Are Defined and Terminated
ABORT Is Explicit
ABORT and ROLLBACK Are Synonyms
ABORT With a WHERE Clause
ABORT With a UDT In Its WHERE Clause
Rules For Using Correlated Subqueries In An ABORT Request
Multiple ABORT Requests
Two Types of ABORT Requests
Rules For Using ABORT In Embedded SQL
Related Topics
BEGIN TRANSACTION
ANSI Compliance
Authorization
Rules for Embedded SQL
Explicit Transactions
Implicit Transactions
Rules for Transactions Containing DDL Requests
Teradata Database Transaction Handling Protocol
Using Request Modifiers With BEGIN TRANSACTION
Nested BEGIN TRANSACTION/END TRANSACTION Pairs
Scenarios
Related Topics
CALL
ANSI Compliance
Authorization
Privileges Granted Automatically
Invocation
Rules for Executing SQL Stored Procedures and External Stored Procedures
Delimiting The CALL Statement
General Rules For Specifying Input And Output Parameters
Rules For Call Arguments In BTEQ And CLIv2
Rules For Call Arguments In ODBC And JDBC
Rules For Call Arguments In Nested Procedures
Rules For Calling Stored Procedures From Embedded SQL
Rules for Calling a Stored Procedure With Dynamic Result Sets From Embedded SQL
Session Dateform and Called Procedures
Retrieving Values Of Output Parameters
Status of Unqualified Referenced Objects
Dropped, Renamed, or Replaced Objects
Java External Stored Procedure-Specific Behavior
CALL Request Priority
Errors and Failures in Procedure Execution
Errors and Failures in Nested Procedures
Aborting a CALL Request
Asynchronous and Synchronous Abort Logic
Related Topics
CHECKPOINT
Interactive Syntax
Embedded SQL and Stored Procedure Syntax
ANSI Compliance
Authorization
General Usage Notes
Usage Notes for Stored Procedures and Embedded SQL
COMMENT (Comment-Retrieving Form)
ANSI Compliance
Authorization
Privileges Granted Automatically
Rules for Using COMMENT (Comment-Retrieving Form)
Related Topics
COMMIT
ANSI Compliance
Authorization
How ANSI Transactions Are Defined and Terminated
COMMIT Is Explicit
Rules for Embedded SQL
Relation to ABORT and ROLLBACK
COMMIT and BTEQ
BTEQ Example
Examples
Related Topics
DELETE (Basic/Searched Form)
Basic Syntax
Join Condition Syntax
ANSI Compliance
Authorization
Recommendation
Locks
DELETE Processing Time
Unconstrained (Fast Path) DELETE Processing
Constrained (Slow Path) DELETE Processing
Duplicate Rows and DELETE Processing
General Rules for Using DELETE
Deleting Rows Using Views
Subqueries in DELETE Requests
Rules for Using Correlated Subqueries In a DELETE Statement
DELETE and Partitioned Primary Indexes
Queue Tables and DELETE
DELETE Support For UDT Expressions
Embedded SQL and Stored Procedure Error Condition Handling
DELETE (Positioned Form)
ECHO
ANSI Compliance
Authorization
ECHO Not Supported for Embedded SQL
How ECHO Works
ECHO and BTEQ
END TRANSACTION
ANSI Compliance
Authorization
Usage Notes
Rules for Embedded SQL
Related Topics
EXECUTE (Macro Form)
ANSI Compliance
Authorization
Recommendation
Rules for Performing Macros
Access Logging and Errors
Related Topics
EXPLAIN Request Modifier
ANSI Compliance
Authorization
EXPLAIN and USING Request Modifier
EXPLAIN Report Overview
EXPLAIN Processes SQL Requests Only
Teradata Visual Explain Utility
EXPLAIN and Embedded SQL
EXPLAIN and Stored Procedures
Effect of Request Cache Peeking on EXPLAIN Reports
Standard Form of Display for EXPLAIN
Using EXPLAIN to Determine All Database Objects a View Accesses
2PC Session Mode
Related Topics
GROUP BY Clause
HAVING Clause
INSERT/INSERT … SELECT
ANSI Compliance
Authorization
DEFAULT Function Option
DEFAULT VALUES Option
Inserting Into Identity Columns When DEFAULT VALUES Is Specified
Inserting Into NOT NULL Columns
Inserting Into Distinct UDT Columns
Inserting Into Structured UDT Columns
Inserting Into PPI Tables
Inserting Into Global Temporary Tables
Rules for Inserting When Using a DEFAULT Function
Rules for Using INSERT … SELECT With a DEFAULT Function
INSERT, UDTs, and Stored Procedures
Inserting Into Queue Tables
Inserting Into Queue Tables Using Iterated Requests
Valid INSERT Operations
Nonvalid INSERT Operations
Large Objects and INSERT
Duplicate Rows and INSERT
Duplicate Rows and INSERT … SELECT
The INSERT Process
Inserting Rows Through Views
Subqueries In INSERT Statements
SELECT AND CONSUME Subqueries in INSERT Statements
Data Takes the Attributes of the New Table
Logging Errors In an Error Table With INSERT … SELECT
Fast Path INSERT … SELECT Statements
INSERT … SELECT Performance and Target Table Identity Column Primary Indexes
Rules for Fast Path INSERT … SELECT Statements
General Rules for INSERT in Embedded SQL and Stored Procedures
Rules for Valued INSERT in Embedded SQL
Rules for INSERT … SELECT in Embedded SQL and Stored Procedures
Related Topics
LOCKING Request Modifier
ANSI Compliance
Authorization
Applicability
Locking Severities
LOCKING Request Modifier Use With Different DML Statements
Cancelling a Lock
When Explicit Locking Is Necessary
Positioning Explicit Lock Requests
Using LOCKING ROW
Using Locks with NULL SQL Statements
Multiple Locks
Referencing a Locked Object
Specify the Keyword For the Object To Be Locked
Locks and Views
When Both the Request and View Referenced Include LOCKING Request Modifiers
READ Locks and Cancelled Rollback Operations
Determining Which Locks Are Set
Related Topics
MERGE
ANSI Compliance
Authorization
Exceptions to Full ANSI Compliance
Differences in MERGE Statement Behavior Between Teradata Database V2R5.0 and Teradata Database 12.0 Releases
Locking and Concurrency Issues
About the MERGE Statement
Rules and Limitations for MERGE
Rules for Using the DEFAULT Function With MERGE Requests
Large Objects and MERGE
Rules for Using MERGE With PPI Tables
Logging MERGE Errors In an Error Table
MERGE Insert Operations and UDTs
MERGE Update Operations and UDTs
Queue Tables and MERGE
MERGE as a Triggering Action
MERGE As a Triggered Action
Related Topics
ORDER BY Clause
QUALIFY Clause
ROLLBACK
ANSI Compliance
Authorization
How ANSI Transactions Are Defined and Terminated
ROLLBACK Is Explicit
Rules for Embedded SQL
ROLLBACK and ABORT Are Synonyms
How ROLLBACK Works
How ROLLBACK Works With Embedded SQL
ROLLBACK With A WHERE Clause
ROLLBACK With a UDT In Its WHERE Clause
Rules for Using Correlated Subqueries in a ROLLBACK Statement
Multiple ROLLBACK Requests
Two Types of ROLLBACK Requests
ROLLBACK With BTEQ
Related Topics
SAMPLE Clause
SELECT
SELECT … INTO
UPDATE
Basic Form, No FROM Clause Syntax
Basic Form, FROM Clause Syntax
Joined Tables Syntax
ANSI Compliance
Authorization
Locks Set by UPDATE
Activity Count
Duplicate Rows and UPDATE
Duplicate Row Checks
Large Objects and UPDATE
UPDATE Processing Time
Rules for Embedded SQL and Stored Procedures
Queue Tables and UPDATE
Rules for Updating Partitioning Columns of a PPI Table
Identity Columns and UPDATE
Update of GENERATED ALWAYS Identity Columns and PARTITION Columns Is Not Permitted
Updating Distinct UDT Columns
Updating Structured UDT Columns
Updating Structured UDTs Using a Mutator SET Clause
Rules for Updating Rows Using Views
Rules for Using the DEFAULT Function With Update
Nonvalid Uses of UPDATE
FROM Clause
UPDATEs With a Join
UPDATE (Positioned Form)
UPDATE (Upsert Form)
ANSI Compliance
Authorization
UDTs and UPDATE (Upsert Form)
Large Objects and UPDATE (Upsert Form)
UPDATE (Upsert Form) Insert Operations and Partitioned Primary Indexes
UPDATE (Upsert Form) Update Operations and Partitioned Primary Indexes
UPDATE (Upsert Form) and Subqueries
Queue Tables and UPDATE (Upsert Form)
Definition: Upsert
Purpose of the Atomic Upsert Operation
Rules for Using Atomic Upsert
UPDATE (Upsert Form) As a Triggering Action
UPDATE (Upsert Form) As a Triggered Action
Restrictions
Using the DEFAULT Function With the Upsert Form of UPDATE
Examples
Related Topics
USING Request Modifier
ANSI Compliance
Authorization
USING Not Supported for Embedded SQL
How USING Works
USING Variables
Valid Request Types
USING Support for UDTs
USING Support for Large Objects
Application-Specific Restrictions on USING With Large Objects
USING and DateTime System Functions
ANSI DateTime Considerations
Example of ANSI DateTime and Interval With USING
ANSI DateTime and Parameterized Requests
Character String Definitions in a USING Request Modifier
data_type Considerations for a Japanese Character Site
CHARACTER and GRAPHIC Server Character Set Limitations for INSERT Operations
Non-GRAPHIC Character Data Representation
Character String Assignment and GRAPHIC Columns
Character Data Import Process
UPPERCASE Option and Character Parameter Definition in USING
WHERE Clause
WITH Clause
Appendix A Notation Conventions
Syntax Diagram Conventions
Notation Conventions
Paths
Required Items
Optional Items
Abbreviations
Loops
Excerpts
Character Shorthand Notation Used In This Book
Introduction
Character Symbols
Pad Characters
Predicate Calculus Notation Used in This Book
Appendix B ANSI SQL Standards
The Individual ANSI SQL Standards
Glossary
Index