Table of Contents Teradata SQL Features Overview Version 14.10.0 Module 0 - Introduction Course Objectives... 0-4 Course Description... 0-6 Course Content... 0-8 Module 1 - Teradata Studio Features Optimize Effectiveness... 1-4 Key Features... 1-6 Modular Display... 1-8 Teradata Studio Toolbar... 1-10 Data Source Explorer View... 1-12 Creating Connections... 1-14 Creating Database Objects... 1-16 Creating a SQL Statement (SQL Editor)... 1-20 Project Explorer View... 1-22 HELP TABLE Command... 1-24 Other SQL HELP Commands... 1-26 Setting a Default Database... 1-28 The Teradata SHOW Command... 1-30 Other SQL SHOW Commands... 1-32 Session Information via SELECT... 1-34 Session Information via HELP SESSION... 1-36 SQL Editor... 1-38 SQL Editor with Outline View... 1-40 Using SQL Query Builder... 1-42 Result Set Viewer... 1-44 SQL History View... 1-46 Teradata View... 1-48 Shortcuts to Data Source Objects... 1-50 Shortcuts to Typing Object Names... 1-52 Commenting Lines of SQL... 1-54 Logging on to Multiple Systems... 1-56 Teradata Load Wizard... 1-58 Choosing Studio or Studio Express... 1-60
The Employee_Sales Database... 1-62 The Emp_Views Database... 1-64 Module 1: Summary... 1-66 Module 1: Review Questions... 1-68 Module 1: Lab Exercises... 1-70 Module 2 - Basic SELECT Clauses A Simple SQL SELECT... 2-4 Projecting All Columns and All Rows... 2-6 Selecting Columns Individually... 2-8 Aliasing Mistake?... 2-10 Ordering Options... 2-12 Using WHERE to Eliminate Rows... 2-14 The ASCII Collating Sequence and Teradata Mode... 2-16 The ASCII Collating Sequence and ANSI Mode... 2-18 Basic Logical Operators... 2-20 DISTINCT Option... 2-22 Module 2: Summary... 2-24 Module 2: Review Questions... 2-26 Module 2: Lab Exercise... 2-28 Module 3 - Some Basic Logical Operators Logical Operators... 3-4 The AND Condition... 3-6 The OR Condition... 3-8 Parentheses and the Predicate... 3-10 The IN Operator... 3-12 The NOT IN Operator... 3-14 The BETWEEN Operator... 3-16 NULL... 3-18 Conditional Expressions and NULL... 3-20 Negating Conditions and Operators... 3-22 NULL Literal in an IN-List... 3-24 NULL Literal in a NOT IN-List... 3-26 Module 3: Summary... 3-28 Module 3: Review Questions... 3-30 Module 3: Lab Exercise... 3-32
Module 4 - Data Types and Functionality Data Types... 4-4 Character Data Types... 4-6 Character Functionality... 4-8 BETWEEN Functionality with CHARACTER... 4-10 Integer Data Types... 4-12 Decimal Data Types... 4-14 Float Data Type... 4-16 Byte Data Types... 4-18 Date Data Type... 4-20 ARRAY Data Type... 4-22 NUMBER Data Type (14.0)... 4-24 More Data Types... 4-26 Arithmetic Operators... 4-28 Arithmetic and Derived Values... 4-30 Arithmetic Functions... 4-32 DATE Arithmetic... 4-34 Data Type Conversions Using CAST... 4-36 Data Type Conversions and Rounding... 4-38 Concatenated Example Results... 4-40 FORMAT... 4-42 Methods for FORMAT in ODBC and JDBC Tools... 4-44 Year, Month and Day Formatting Options... 4-46 Module 4: Summary... 4-48 Module 4: Review Questions... 4-50 Module 4: Lab Exercise... 4-52 Module 5 - Basic SQL Functions UPPER & LOWER... 5-4 UPPER & LOWER for Case Sensitivity... 5-6 CHARACTER_LENGTH... 5-8 TRIM... 5-10 Trimming Other Than Space... 5-12 POSITION... 5-14 SUBSTRING... 5-16 SUBSTRING and Numbers... 5-18 LIKE... 5-20 LIKE Examples Using %... 5-22 LIKE Examples Using _... 5-24 LIKE and ESCAPE... 5-26 CASESPECIFIC... 5-28 EXTRACT... 5-30 ADD_MONTHS... 5-32 DEFAULT... 5-34 The Calendars... 5-36
Calendar Differences... 5-38 Additional Calendar Functions... 5-40 Module 5: Summary... 5-44 Module 5: Review Questions... 5-46 Module 5: Lab Exercise... 5-48 Module 6 - Subqueries The Subquery... 6-4 Adding Conditions... 6-6 Nesting Subqueries... 6-8 Multiple Column Matching... 6-10 NOT EXISTS... 6-12 EXISTS... 6-14 NULL and NOT IN Subquery... 6-16 Module 6: Summary... 6-18 Module 6: Review Questions... 6-20 Module 6: Lab Exercise... 6-22 Module 7 - Inner Join Inner Join vs. Subquery... 7-4 Varied Forms of Inner Join... 7-6 Many-Table Inner Joins... 7-8 Using Parentheses to Understand Order... 7-10 Using Parentheses with Other Forms... 7-12 Self Joins... 7-14 Guaranteeing Uniqueness... 7-16 Cross Join... 7-18 Mistakes on Table Aliasing... 7-20 Mistakes on Column Aliasing... 7-22 Module 7: Summary... 7-24 Module 7: Review Questions... 7-26 Module 7: Lab Exercise... 7-28 Module 8 - Set Operators What are Set Operators?... 8-4 UNION... 8-6 UNION ALL... 8-8 INTERSECT... 8-10 EXCEPT (MINUS)... 8-12 EXCEPT and ALL... 8-14 Module 8: Summary... 8-16 Module 8: Review Questions... 8-18
Module 9 - Outer Join Outer Join Concepts... 9-4 Types of Outer Joins... 9-6 Employee as Left Outer... 9-8 Nulls and the Inner Table... 9-10 Department as Outer... 9-12 Outer Joins and WHERE... 9-14 Syntax Variations... 9-16 Three Table Outer Join... 9-18 Full Outer Join... 9-20 Module 9: Summary... 9-22 Module 9: Review Questions... 9-24 Module 9: Lab Exercise... 9-26 Module 10 - Aggregation Aggregate Functionality... 10-4 COUNT(*)... 10-6 Aggregating Groups... 10-8 Adding Grouping Columns... 10-10 The HAVING Clause... 10-12 WHERE Clause Explain... 10-14 HAVING on Non-Aggregates... 10-16 Aggregation and Joins... 10-18 COUNT DISTINCT... 10-20 Module 10: Summary... 10-22 Module 10: Review Questions... 10-24 Module 10: Lab Exercise... 10-26 Module 11 - Correlated Subqueries Correlated Subquery Terminology... 11-4 Correlated Subquery Processing... 11-6 Correlated Subqueries and Aggregation... 11-8 A Complex Example... 11-10 NOT IN vs. NOT EXISTS... 11-12 NOT EXISTS Logic... 11-14 Module 11: Summary... 11-16 Module 11: Review Questions... 11-18 Module 11: Lab Exercise... 11-20
Module 12 - CASE CASE... 12-4 Valued Form (Project List)... 12-6 Valued Form and Null... 12-8 Searched Form... 12-10 NULLIF Function... 12-12 COALESCE Function... 12-14 COALESCE and Multiple Arguments... 12-16 Module 12: Summary... 12-18 Module 12: Review Questions... 12-20 Module 12: Lab Exercise... 12-22 Module 13 - Permanent and Derived Tables Data Definition Language... 13-4 Set vs. Multiset... 13-6 Table Level Options... 13-8 Column Level Options... 13-10 Index Level Options... 13-12 Creating and Dropping Secondary Indexes... 13-14 Help Index... 13-16 Derived Tables... 13-18 Forms of Derived Tables... 13-20 Complex Derived Table Join... 13-22 Module 13: Summary... 13-24 Module 13: Review Questions... 13-26 Module 13: Lab Exercise... 13-28 Module 14 - Update-Insert-Delete Inserting a Single Row... 14-4 Inserting an Apostrophe... 14-6 Inserting Default Values... 14-8 Insert-Select... 14-10 CASESPECIFIC and SET Tables... 14-12 CASESPECIFIC and MULTISET Tables... 14-14 UPDATE... 14-16 Updating with Joins... 14-18 UPDATE and FROM... 14-20 DELETE... 14-22 Deleting with Joins... 14-24 Module 14: Summary... 14-26 Module 14: Review Questions... 14-28 Module 14: Lab Exercise... 14-30
Module 15 - SAMPLE and RANDOM SAMPLE Introduction... 15-4 SAMPLE Syntax... 15-6 Multiple Samples (Number of Rows)... 15-8 Multiple Samples (Percentage of Rows)... 15-10 SAMPLE WITH REPLACEMENT... 15-12 Other Considerations... 15-14 Using Derived Tables... 15-16 Stratified Sampling (No Replacement)... 15-18 Stratified Sampling (With Replacement)... 15-20 RANDOMIZED ALLOCATION... 15-22 The RANDOM Function... 15-24 RANDOM and Limitations... 15-26 Module 15: Summary... 15-28 Module 15: Review Questions... 15-30 Module 15: Lab Exercise... 15-32 Module 16 - TOP N TOP N Defined... 16-4 TOP N Limitations... 16-6 TOP N Example... 16-8 TOP N WITH TIES... 16-10 Without Ties Same Result... 16-12 Getting Bottom Results... 16-14 Unordered Rows... 16-16 The PERCENT Option... 16-18 PERCENT Option WITH TIES... 16-20 PERCENT Option No ORDER BY... 16-22 Module 16: Summary... 16-24 Module 16: Review Questions... 16-26 Module 16: Lab Exercise... 16-28 Module 17 - Creating Tables from Existing Tables Create Table AS... 17-4 Cloning Attributes... 17-6 Changing Table Attributes... 17-8 Using Subqueries to Customize Tables... 17-10 Renaming Columns... 17-12 Changing Column Attributes... 17-14 Using Inner Joins in a Subquery... 17-16 Using CAST... 17-18 Adding Unique and Primary Key Constraints... 17-20 Copying Statistics... 17-22
Module 17: Summary... 17-24 Module 17: Review Questions... 17-26 Module 17: Lab Exercise... 17-28 Module 18 - Views What is a View?... 18-4 Creating and Using Views... 18-6 Replacing a View via SQL Assistant... 18-8 Using Views to Rename Columns... 18-10 Join View... 18-12 Joining Views... 18-14 Aggregates and HAVING... 18-16 Updating Through Views... 18-18 Views WITH CHECK OPTION... 18-20 Restrictions on Views... 18-22 Advantages and Suggestions... 18-24 Module 18: Review Questions... 18-26 Module 18: Lab Exercise... 18-28 Module 19 - Macros What is a Macro?... 19-4 CREATE, EXECUTE, DROP Macro... 19-6 Replacing a Macro... 19-8 Multiple Statement Macros... 19-10 Macros vs. Multi-Statement Requests... 19-12 Simple Parameterized Macros... 19-14 A More Complex Macro... 19-16 EXECUTE Form 1... 19-18 EXEC Form 2... 19-20 DDL and Macros... 19-22 Module 19: Summary... 19-24 Module 19: Review Questions... 19-26 Appendix A: Review Question Solutions Appendix B: Lab Exercise Solutions Appendix C: SQL Assistant