About the Author Larry Rockoff has been involved with SQL and Business Intelligence (BI) de- velopment for many years. His main area of expertise is with data warehouse systems and reporting tools. He recently developed a suite of BI tools for ASA P Software, a subsidiary of Dell Inc. He holds an MBA from the University of Chicago, with a specialization in Management Science. For more information on his current activities or to contact the author, please visit LarryRockoff.com. v Contents Introduction . . xii Chapter 1 Relational Databases and SQL 1 Language and Logic 1 SQL Defined. . . 3 Microsoft SQL Server, Oracle, and MySQL 3 Other Databases 5 Relational Databases 6 Primary and Foreign Keys . . 7 Datatypes 8 NULL Values. . . 10 The Significance of SQL 10 Looking Ahead 11 Chapter 2 Basic Data Retrieval 13 A Simple SELECT 13 Syntax Notes . . 15 Specifying Columns 16 Column Names with Embedded Spaces . . 17 Looking Ahead 18 Chapter 3 Calculations and Aliases 19 Calculated Fields 19 Literal Values . . 20 vi Arithmetic Calculations 22 Concatenating Fields 23 Column Aliases . 24 Table Aliases . . . 26 Looking Ahead . 27 Chapter 4 Using Functions 29 The Function of Functions . . . 29 Character Functions 30 Composite Functions 36 Date/Time Functions 37 Numeric Functions 40 Conversion Functions 41 Looking Ahead . 44 Chapter 5 Sorting Data 47 Adding a Sort . . 47 Sorting in Ascending Order. . 48 Sorting in Descending Order . 49 Sorting by Multiple Columns. 50 Sorting by a Calculated Field. 51 More on Sort Sequences 52 Looking Ahead . 55 Chapter 6 Column-Based Logic . 57 IF-THEN-ELSE Logic 57 The Simple Format 58 The Searched Format 60 Looking Ahead . 62 Chapter 7 Row-Based Logic 63 Applying Selection Criteria . . 63 WHERE Clause Operators 65 Limiting Rows . . 66 Limiting Rows with a Sort . . . 68 Looking Ahead . 70 Chapter 8 Boolean Logic 71 Complex Logical Conditions . 71 Contents vii The AND Operator 72 The OR Operator 73 Using Parentheses 73 Multiple Sets of Parentheses 75 The NOT Operator 76 The BETWEEN Operator 79 The IN Operator 80 Boolean Logic and NULL Values 82 Looking Ahead 84 Chapter 9 Inexact Matches . . . 85 Pattern Matching 85 Wildcards 88 Matching by Sound 91 Looking Ahead 93 Chapter 10 Summarizing Data . 95 Eliminating Duplicates 95 Aggregate Functions 97 The COUNT Function 99 Grouping Data 101 Multiple Columns and Sorting 103 Selection Criteria on Aggregates 105 Looking Ahead 107 Chapter 11 Combining Tables with an Inner Join 109 Joining Two Tables 110 The Inner Join . 112 Table Order in Inner Joins . . 114 Alternate Specification of Inner Joins . . . 114 Table Aliases Revisited 115 Looking Ahead 116 Chapter 12 Combining Tables with an Outer Join 119 The Outer Join. 119 Left Joins 122 Testing for NULL Values 124 Right Joins 125 Table Order in Outer Joins. . 125 Contentsviii Full Joins 126 Looking Ahead . 128 Chapter 13 Self Joins and Views . 131 Self Joins 131 Creating Views . 134 Referencing Views 136 Benefits of Views 137 Modifying and Deleting Views 138 Looking Ahead . 139 Chapter 14 Subqueries 141 Types of Subqueries 141 Using a Subquery as a Data Source 142 Using a Subquery in Selection Criteria 145 Correlated Subqueries 147 The EXISTS Operator 149 Using a Subquery as a Calculated Column. . 150 Looking Ahead . 151 Chapter 15 Set Logic 153 Using the UNION Operator . . 154 Distinct and Non-Distinct Unions 157 Intersecting Queries 158 Looking Ahead . 160 Chapter 16 Stored Procedures and Parameters . . 163 Creating Stored Procedures. . 164 Parameters in Stored Procedures 166 Executing Stored Procedures. 168 Modifying and Deleting Stored Procedures 169 Functions Revisited 170 Looking Ahead . 171 Chapter 17 Modifying Data 173 Modification Strategies 173 Inserting Data . . 174 Deleting Data . . 178 Updating Data. . 180 Contents ix . Conditions . 71 Contents vii The AND Operator 72 The OR Operator 73 Using Parentheses 73 Multiple Sets of Parentheses 75 The NOT Operator 76 The BETWEEN Operator 79 The IN Operator 80 Boolean Logic. and reporting tools. He recently developed a suite of BI tools for ASA P Software, a subsidiary of Dell Inc. He holds an MBA from the University of Chicago, with a specialization in Management Science. For. or to contact the author, please visit LarryRockoff.com. v Contents Introduction . . xii Chapter 1 Relational Databases and SQL 1 Language and Logic 1 SQL Defined. . . 3 Microsoft SQL Server,