TERADATA SQL
By
B M Kumar
Agenda
• Basic RDBMS Concepts and Teradata SQL
• Teradata Extensions ( HELP & SHOW)
• Data Types and Conversions
• Logical & Conditional Expressions, Attribute & String Functions
• Data Definitions
• Data Manipulations
• Views and Macros
• Joins
• Sub queries
• Set Operators
• Aggregations, Totals and Subtotals
• Rankings, Samplings, and Built-In Functions
• BTEQ ( Basic Teradata Query)
Teradata COE - Alchemy 2
Solutions
Basic RDBMS Concepts and
Teradata SQL
Teradata COE - Alchemy 3
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 4
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 5
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 6
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 7
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 8
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 9
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 10
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 11
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 12
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 13
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 14
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 15
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 16
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 17
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 18
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 19
Solutions
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy 20
Solutions
Teradata Extensions
Teradata COE - Alchemy 21
Solutions
Teradata Extensions
Teradata COE - Alchemy 22
Solutions
Teradata Extensions
Teradata COE - Alchemy 23
Solutions
Teradata Extensions
Teradata COE - Alchemy 24
Solutions
Teradata Extensions
Teradata COE - Alchemy 25
Solutions
Teradata Extensions
Teradata COE - Alchemy 26
Solutions
Teradata Extensions
Teradata COE - Alchemy 27
Solutions
Teradata Extensions
Teradata COE - Alchemy 28
Solutions
Data Types and Conversions
Teradata COE - Alchemy 29
Solutions
Data Types and Conversions
Teradata COE - Alchemy 30
Solutions
Data Types and Conversions
Teradata COE - Alchemy 31
Solutions
Data Types and Conversions
Teradata COE - Alchemy 32
Solutions
Data Types and Conversions
Teradata COE - Alchemy 33
Solutions
Data Types and Conversions
Teradata COE - Alchemy 34
Solutions
Data Types and Conversions
Teradata COE - Alchemy 35
Solutions
Data Types and Conversions
Teradata COE - Alchemy 36
Solutions
Data Types and Conversions
Teradata COE - Alchemy 37
Solutions
Data Types and Conversions
Teradata COE - Alchemy 38
Solutions
Data Types and Conversions
Teradata COE - Alchemy 39
Solutions
Data Types and Conversions
Teradata COE - Alchemy 40
Solutions
Data Types and Conversions
Teradata COE - Alchemy 41
Solutions
Data Types and Conversions
Teradata COE - Alchemy 42
Solutions
Data Types and Conversions
Teradata COE - Alchemy 43
Solutions
Data Types and Conversions
Teradata COE - Alchemy 44
Solutions
Data Types and Conversions
Teradata COE - Alchemy 45
Solutions
Logical & Conditional Expressions
And Output Attributes
Teradata COE - Alchemy 46
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 47
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 48
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 49
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 50
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 51
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 52
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 53
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 54
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 55
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 56
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 57
Solutions
Logical and Conditional Expressions
Teradata COE - Alchemy 58
Solutions
Output Attributes
Teradata COE - Alchemy 59
Solutions
Output Attributes
Teradata COE - Alchemy 60
Solutions
Output Attributes
Teradata COE - Alchemy 61
Solutions
Output Attributes
Teradata COE - Alchemy 62
Solutions
Output Attributes
Teradata COE - Alchemy 63
Solutions
Output Attributes
Teradata COE - Alchemy 64
Solutions
Output Attributes
Teradata COE - Alchemy 65
Solutions
Output Attributes
Teradata COE - Alchemy 66
Solutions
Output Attributes
Teradata COE - Alchemy 67
Solutions
Output Attributes
Teradata COE - Alchemy 68
Solutions
Output Attributes
Teradata COE - Alchemy 69
Solutions
Output Attributes
Teradata COE - Alchemy 70
Solutions
Data Definition
Teradata COE - Alchemy 71
Solutions
Data Definition
Teradata COE - Alchemy 72
Solutions
Data Definition
Teradata COE - Alchemy 73
Solutions
Data Definition
Teradata COE - Alchemy 74
Solutions
Data Definition
Teradata COE - Alchemy 75
Solutions
Data Definition
Teradata COE - Alchemy 76
Solutions
Data Definition
Teradata COE - Alchemy 77
Solutions
Data Definition
Teradata COE - Alchemy 78
Solutions
Data Definition
Teradata COE - Alchemy 79
Solutions
Data Definition
Teradata COE - Alchemy 80
Solutions
Data Definition
Teradata COE - Alchemy 81
Solutions
Data Definition
Teradata COE - Alchemy 82
Solutions
Data Definition
Teradata COE - Alchemy 83
Solutions
Data Definition
Teradata COE - Alchemy 84
Solutions
Data Definition
Teradata COE - Alchemy 85
Solutions
Data Definition
Teradata COE - Alchemy 86
Solutions
Data Definition
Teradata COE - Alchemy 87
Solutions
Data Definition
Teradata COE - Alchemy 88
Solutions
Data Definition
Teradata COE - Alchemy 89
Solutions
Data Definition
Teradata COE - Alchemy 90
Solutions
Data Manipulation
Teradata COE - Alchemy 91
Solutions
Data Manipulation
Teradata COE - Alchemy 92
Solutions
Data Manipulation
Teradata COE - Alchemy 93
Solutions
Data Manipulation
Teradata COE - Alchemy 94
Solutions
Data Manipulation
Teradata COE - Alchemy 95
Solutions
Data Manipulation
Teradata COE - Alchemy 96
Solutions
Data Manipulation
Teradata COE - Alchemy 97
Solutions
Data Manipulation
Teradata COE - Alchemy 98
Solutions
Data Manipulation
Teradata COE - Alchemy 99
Solutions
Data Manipulation
Teradata COE - Alchemy 100
Solutions
Data Manipulation
Teradata COE - Alchemy 101
Solutions
Data Manipulation
Teradata COE - Alchemy 102
Solutions
Views and Macros
Teradata COE - Alchemy 103
Solutions
Views and Macros
Teradata COE - Alchemy 104
Solutions
Views and Macros
Teradata COE - Alchemy 105
Solutions
Views and Macros
Teradata COE - Alchemy 106
Solutions
Views and Macros
Teradata COE - Alchemy 107
Solutions
Views and Macros
Teradata COE - Alchemy 108
Solutions
Views and Macros
Teradata COE - Alchemy 109
Solutions
Views and Macros
Teradata COE - Alchemy 110
Solutions
Views and Macros
Teradata COE - Alchemy 111
Solutions
Views and Macros
Teradata COE - Alchemy 112
Solutions
Views and Macros
Teradata COE - Alchemy 113
Solutions
Views and Macros
Teradata COE - Alchemy 114
Solutions
Views and Macros
Teradata COE - Alchemy 115
Solutions
Views and Macros
Teradata COE - Alchemy 116
Solutions
Views and Macros
Teradata COE - Alchemy 117
Solutions
Views and Macros
Teradata COE - Alchemy 118
Solutions
Views and Macros
Teradata COE - Alchemy 119
Solutions
JOINS
Teradata COE - Alchemy 120
Solutions
Joins
Teradata COE - Alchemy 121
Solutions
Joins
Teradata COE - Alchemy 122
Solutions
Joins
Teradata COE - Alchemy 123
Solutions
Joins
Teradata COE - Alchemy 124
Solutions
Joins
Teradata COE - Alchemy 125
Solutions
Joins
Teradata COE - Alchemy 126
Solutions
Joins
Teradata COE - Alchemy 127
Solutions
Joins
Teradata COE - Alchemy 128
Solutions
Joins
Teradata COE - Alchemy 129
Solutions
Joins
Teradata COE - Alchemy 130
Solutions
Joins
Teradata COE - Alchemy 131
Solutions
Joins
Teradata COE - Alchemy 132
Solutions
Joins
Teradata COE - Alchemy 133
Solutions
Joins
Teradata COE - Alchemy 134
Solutions
Joins
Teradata COE - Alchemy 135
Solutions
Joins
Teradata COE - Alchemy 136
Solutions
Joins
Teradata COE - Alchemy 137
Solutions
Joins
Teradata COE - Alchemy 138
Solutions
Joins
Teradata COE - Alchemy 139
Solutions
Joins
Teradata COE - Alchemy 140
Solutions
Joins
Teradata COE - Alchemy 141
Solutions
Joins
Teradata COE - Alchemy 142
Solutions
Sub Queries
Teradata COE - Alchemy 143
Solutions
Sub Queries
Teradata COE - Alchemy 144
Solutions
Sub Queries
Teradata COE - Alchemy 145
Solutions
Sub Queries
Teradata COE - Alchemy 146
Solutions
Sub Queries
Teradata COE - Alchemy 147
Solutions
Sub Queries
Teradata COE - Alchemy 148
Solutions
Set Operators
Teradata COE - Alchemy 149
Solutions
Set Operators
Teradata COE - Alchemy 150
Solutions
Set Operators
Teradata COE - Alchemy 151
Solutions
Set Operators
Teradata COE - Alchemy 152
Solutions
Set Operators
Teradata COE - Alchemy 153
Solutions
Set Operators
Teradata COE - Alchemy 154
Solutions
Set Operators
Teradata COE - Alchemy 155
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 156
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 157
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 158
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 159
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 160
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 161
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 162
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 163
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 164
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 165
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 166
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 167
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 168
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 169
Solutions
Aggregations, Totals and Subtotals
Teradata COE - Alchemy 170
Solutions
Rankings, Samplings,
and Built-In Functions
Teradata COE - Alchemy 171
Solutions
Rankings
Teradata COE - Alchemy 172
Solutions
Rankings
Teradata COE - Alchemy 173
Solutions
Rankings
Teradata COE - Alchemy 174
Solutions
Rankings
Teradata COE - Alchemy 175
Solutions
Rankings
Teradata COE - Alchemy 176
Solutions
Rankings
Teradata COE - Alchemy 177
Solutions
Rankings with Partition BY
SELECT storeid ,prodid
,sales
,RANK( ) OVER (PARTITION BY storeid, prodid
ORDER BY sales DESC) AS "Ranking"
FROM salestbl
QUALIFY Ranking <= 3;
storeid prodid sales Ranking
----------- ------ ----------- -----------
1001 A 100000.00 1
1001 C 60000.00 1
1001 D 35000.00 1
1001 F 150000.00 1
1002 A 40000.00 1
1002 C 35000.00 1
1002 D 25000.00 1
1003 A 30000.00 1
1003 B 65000.00 1
1003 C 20000.00 1
1003 D 50000.00 1
Things To Notice
•An additional PARTITION BY clause is added.
•In this case, the scope is sales by product within store.
•Each product exists only once in each store.
•Thus, each row gets a rank of 1. each store.
Teradata COE - Alchemy 178
Solutions
TOP N Overview
The TOP N function provides the ability to produce:
•The top (or bottom) N rows of results based on specified criteria.
•The top (or bottom) N percentage of rows based on specified criteria.
•These results either with or without ties (more than one row has identical criteria
values).
•Sample rows arbitrarily without regard to rankings or order.
This improvement has been achieved by implementing the following new
syntax to the SQL lexicon:
TOP {decimal | integer} [PERCENT] [WITH TIES]
The following meaning is associated with these options:
•TOP 10 – Return the top ten rows according to criteria
•TOP 15 PERCENT – Return the top 15% of rows according to criteria
•TOP 10 WITH TIES – If more that one row has the same criteria value, return all
•TOP 15 PERCENT WITH TIES – If more than one row has the same criteria
value, return all
Teradata COE - Alchemy 179
Solutions
TOP N
• Show all department budgets ordered by budget amount descending. To
answer this query, the TOP N feature is not needed.
SELECT department_number ,
budget_amount
FROM department
ORDER BY 2 DESC;
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
402 308000.00
501 308000.00
201 293800.00
302 226000.00
600 ?
Note: Null sorts following the valid budget
amounts.
Teradata COE - Alchemy 180
Solutions
TOP N
TOP N With and Without TIES
SELECT TOP 5
department_number
, budget_amount
FROM department
ORDER BY 2 DESC;
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
501 308000.00
Things to notice:
ORDER BY defines the sequencing of the result set.
It therefore defines the ranking criteria.
To get the TOP highest amounts, you must use ORDER with DESC.
TOP N where N is an integer up to 18 digits in length.
SELECT TOP 5 WITH TIES
department_number
, budget_amount
FROM department
ORDER BY 2 DESC;
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
501 308000.00
402 308000.00
Things to notice:
Even though TOP 5 is specified, six rows are returned.
Because there is a tie for the fifth position, both rows are returned.
This only occurs when WITH TIES is specified.
WITH TIES returns multiple tied rows when there is a tie for the 'last' position.
It will return all rows containing the 'tied' value, but it will only count it as one row.
Tied rows which are not in the last position, are each counted separately toward the N total.
Teradata COE - Alchemy 181
Solutions
SAMPLE
The SAMPLE function allows a user to specify a sample of results returned by a specified SELECT statement. The sample
can be represented as either a percentage of the returned rows or as an absolute number.
Sampling is useful for many purposes, including generating samples of production data for purposes of testing out new
applications.
Stratified sampling:
the ability to generate different sized samples from different groupings of the data.
Consider the distribution of departments within the employee table.
SELECT department_number
,COUNT (*)
FROM employee
GROUP BY 1
ORDER BY 1;
• department_number Count(*)
----------------- -----------
100 1
201 2
301 3
302 1
401 7
402 2
403 6
501 4
Teradata COE - Alchemy 182
Solutions
SAMPLE
Create five groupings of employees based on the range of department numbers.
SELECT CASE WHEN department_number BETWEEN 100 AND 199 THEN 'GROUP 1'
WHEN department_number BETWEEN 200 AND 299 THEN 'GROUP 2'
WHEN department_number BETWEEN 300 AND 399 THEN 'GROUP 3'
WHEN department_number BETWEEN 400 AND 499 THEN 'GROUP 4'
WHEN department_number BETWEEN 500 AND 599 THEN 'GROUP 5'
ELSE NULL END AS Group_Name ,COUNT (*) FROM employee
GROUP BY 1 ORDER BY 1;
Group_Name Count(*)
---------- -----------
GROUP 1 1
GROUP 2 2
GROUP 3 4
GROUP 4 15
GROUP 5 4
SELECT last_name (CHAR(10)) ,department_number (FORMAT '999') AS Deptno , SAMPLEID FROM employee SAMPLE
WHEN department_number BETWEEN 100 AND 199 THEN .25
WHEN department_number BETWEEN 200 AND 299 THEN .25
WHEN department_number BETWEEN 300 AND 399 THEN .25
WHEN department_number BETWEEN 400 AND 499 THEN .25
WHEN department_number BETWEEN 500 AND 599 THEN .25
END ORDER BY 2;
last_name Deptno SampleId
---------- ------ -----------
Short 201 2 (.25 * 2 = .50 ROUNDING TO 1)
Stein 301 3 (.25 * 4 = 1)
Phillips 401 4 (.25 * 15 = 3.75 ROUNDING TO 4)
Rogers 401 4
Villegas 403 4
Hopkins 403 4
Ratzlaff 501 5 (.25 * 4 = 1)
Things to notice:
•SAMPLEID reflects the sample pool into which each row falls.
•In some cases, the fractional amount is less than a full row, thus no rows are returned for that sampling.
•No rows are returned for SAMPLEID 1 because 25% of one row rounds to zero.
Teradata COE - Alchemy 183
Solutions
Built-In Functions
Teradata COE - Alchemy 184
Solutions
String Functions
Teradata COE - Alchemy 185
Solutions
String Functions
Teradata COE - Alchemy 186
Solutions
String Functions
Teradata COE - Alchemy 187
Solutions
String Functions
Teradata COE - Alchemy 188
Solutions
String Functions
Teradata COE - Alchemy 189
Solutions
String Functions
Teradata COE - Alchemy 190
Solutions
String Functions
Teradata COE - Alchemy 191
Solutions
String Functions
Teradata COE - Alchemy 192
Solutions
BTEQ (Basic Teradata Query)
Teradata COE - Alchemy 193
Solutions
BTEQ
Teradata COE - Alchemy 194
Solutions
BTEQ
Teradata COE - Alchemy 195
Solutions
BTEQ
Teradata COE - Alchemy 196
Solutions
BTEQ
Teradata COE - Alchemy 197
Solutions
BTEQ
Teradata COE - Alchemy 198
Solutions
Questions
Teradata COE - Alchemy 199
Solutions