Académique Documents
Professionnel Documents
Culture Documents
Edition
by Tom Coffing, Michael Larkins and Steve Wilmes
Coffing Data Warehousing. (c) 2006. Copying Prohibited.
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
Preface
Overview
"You miss 100% of the shots you don't take"
Wayne Gretzky
Asking the right questions is half the battle to greatly impacting the company's bottom line, but sometimes knowing the proper SQL inhibits
users from taking their shot at corporate greatness. This book is dedicated to assisting users in asking the right questions by providing an
easy to understand guide that takes the difficulty out of SQL.
The purpose of "SQL Reference Guide Simplicity by Design" is simple: Users need an easy and concise reference book at their fingertips
that they can refer to, without having to search through larger books for SQL help. After the generic syntax, there are applied examples that
have been written based on the SQL_CLASS database, which can be downloaded from our website at: www.CoffingDW.Com.
The "SQL Reference Guide Simplicity by Design" follows this format for each command:
n
Short Definition
Syntax
Examples
Examples explained
<table-name>
<comparison>
<column-name>
<data-value>
[ optional entry ]
Everything between the [ ] is optional, not required to be valid syntax , use when needed
Use one of the keywords or symbols on either side of the "|", but not both. I.e. { LEFT | RIGHT } use either "LEFT" or "RIGHT" but not both
The message, "3807: Table/view/trigger '<table-name> does not exist." will occur when the table being selected from does not reside on Teradata.
Or, you are in the wrong database.
Solutions:
Check the spelling of the <table-name>, use the SELECT DATABASE syntax to confirm you are in the correct database or HELP DATABASE
<database-name> to see all objects in the database.
SELECT { <column-name> }
[,<column-name> ]
FROM
<table-name>;
SELECT Customer_Number FROM Customer_Table;
Page 2 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
SELECT Dept_No
,Dept_Name FROM Department_Table;
Hint - Man When requesting multiple columns in a table each column is separated from the next by a comma. Experienced users often
place the comma at the beginning for easy debugging.
Issues:
A missing comma could be a problem with this syntax. Here is the Error
Message:
Solution:
Search through the SQL code to locate the two columns with no comma between the two, and place a comma. Or you could have an extra comma
at the end of the last column selected. Take the extra comma out.
3535: A character string failed conversion to a numeric value. The <data- value> in the WHERE clause does not match the data type of the
<column- name>. Also remember that numeric values are not in single quotes ' '. Character data should be in single quotes.
Solutions:
The first thing to do is to find out the data type of the <column-name> involved by running HELP TABLE <table-name>. Next, adjust the <data-value>
to match the data type.
Comparators: AND/OR
Many times a single comparison in a WHERE clause is not enough to specify the desired rows. The AND/OR commands are called logical
operators. The AND syntax requires both comparisons to be true. The OR syntax however, only requires one of the two to be true.
SELECT <column-name>
[,<column-name> ]
FROM
<table-name>
WHERE <column-name> <comparison> <data-value> AND | OR
<column-name> <comparison> <data-value>;
Solutions:
Remember that when you use AND that you must utilize two different columns. For example, an employee could not have a SALARY of $50,000
AND $75,000. You could however have an employee in department 100 AND have a salary of $50,000.
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
SELECT <column-name>
[...,<column-name> ]
FROM
<table-name>
WHERE ( <column-name> <comparison> <data-value> OR
<column-name> <comparison> <data-value> ) AND
<column-name> <comparison> <data-value> ;
Solutions:
You need to remember that when combining AND/OR comparators that Teradata will analyze the AND first unless you place parenthesis around the
OR statements.
Page 4 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
= NULL is not a valid SQL statement. Change the syntax to IS NULL. 3731: The user must use IS NULL or IS NOT NULL to test for NULL values.
Solution:
Adjust the SQL syntax from '=NULL' to 'IS NULL' or 'IS NOT NULL'.
NOT Clause
The NOT Clause will check for values that are NOT <, > or equal to a value. Teradata searches for the logical opposite of the
<comparison> statement.
SELECT <column-name>
FROM
<table-name>
WHERE <column-name> NOT <comparison> <data-value> ;
SELECT Course_Name FROM Course_Table
WHERE Seats NOT >= 20;
English: Above we find all courses where there are less than 20 seats in the course.
SELECT Customer_Name, Phone_Number
FROM Customer_Table
WHERE Customer_Name NOT = 'ACE Consulting';
English: Above we SELECT two columns for all Customers except 'ACE Consulting'.
Alternative Syntax:
SELECT <column-name>
FROM
<table-name>
WHERE NOT ( <column-name> <comparison> <data-value> );
SELECT Course_Name FROM Course_Table
WHERE NOT ( Seats >= 20 );
SELECT Customer_Name, Phone_Number
FROM Customer_Table
WHERE NOT ( Customer_Name = 'ACE Consulting' );
Hint - Man Running this syntax without parentheses is permissible, but not recommended.
Issues:
Teradata returns the message: 3707: Syntax error, expected something like an 'IN' keyword between the 'NOT' keyword and '<comparison>'. This is a
problem caused by the 'NOT' not being properly placed in the SQL.
Solutions:
Locate the 'NOT' in the SQL and place it directly before the <column-name>, with the <comparison> after that.
IN and NOT IN
The IN comparison is an alternative to using one or more OR comparisons on the same column in the WHERE clause of a SELECT.
Teradata actually views the IN clause as a multiple OR statement. If the criteria are in the <value-list>, a row will be returned.
SELECT <column-name>
[...,<column-name> ]
FROM
<table-name>
WHERE <column-name> IN | NOT IN (<value-list>) ;
SELECT Dept_Name, Mgr_No FROM Department_Table
WHERE Dept_No IN ( 200, 300,100 );
English: Above we select two columns from each row where the Dept_no is 200 or 300 or 100.
SELECT * FROM Order_Table
WHERE Order_Number NOT IN (123585, 123456);
English: Above we select all columns unless the Order_Number is 123585 or 123456.
SELECT * FROM Department_Table
WHERE Dept_Name IN ('Sales', 'Marketing');
Page 5 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
English: Above we select all columns from the Department_Table if the department name is Sales or Marketing. Notice the single quotes
around 'Sales' and 'Marketing'. They are not numbers so they need single quotes.
Hint - Man Do not place the word 'NULL' in the <value-list> for a 'NOT IN', being that no rows will be returned every time.
Issues:
3706: Syntax error: expected something between an {integer | character-string} and ';'. This error is caused by a lack of parentheses around the
<value-list>.
Solutions:
Place a '(' before the <value-list> and a ')' after the final entry in the<value- list>.
Solutions:
Add the equal sign before the 'ANY' keyword, or after the 'NOT' keyword.
BETWEEN
The BETWEEN statement is another technique to request multiple values for a column that are all in a specific range. The key item to
remember is that BETWEEN is inclusive and most widely used with numeric values. If you write WHERE AGE is BETWEEN 13 and 15 then
the answer set would contain individuals whose ages are 13, 14, or 15.
SELECT <column-name>
[...,<column-name> ]
FROM
<table-name>
WHERE <column-name> BETWEEN <low-value> AND <high-value>;
Page 6 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
a date is an index ORDERED BY VALUES run the SHOW TABLE <Tablename> command.
LIKE Clause
The LIKE Command searches character data strings and can use wildcards to find data that is similar. For example, a user can use the
LIKE command to request rows where the last_name starts with 'SM'.
SELECT <column-list>
FROM <table-name>
WHERE <column-name> LIKE '[<wildcard(s)>]<search-string>[<wildcard(s)>]';
The wildcard characters are:
Wildcard symbol
What it does
_ (underscore)
% (percent sign)
The LIKE statement is not returning all the rows expected to be in the result set. More than likely you are dealing with Character strings of CHAR
data type. Remember 'Smith' in the eyes of Teradata is 'Smith+ empty space'. This empty space resides in the rest of the fixed data space on the
system.
Solutions:
There are two solutions to this problem. The first solution is to place a '%' at the end of the <search-string>, as in the second example. The second
solution would be to place the <column-name> in a TRIM function as so: TRIM(<column-name>. Either solution will take care of the problem.
Aliasing Columns
You create an ALIAS on a column for the purpose of giving the column a new name. You might do this to shorten the name or to better
describe a title you want to see as a column header. If you ALIAS and use a reserved word such as SELECT place double quotes around it.
In giving a column an alias, there are two options:
ANSI Standard:
SELECT <column-name> AS <alias-name>
FROM
<table-name>;
Teradata Standard:
SELECT <column-name> <alias-name>
FROM
<table-name>;
SELECT Dept_No
AS DEPT
,Dept_Name
AS "Department Name"
,Mgr_No
Manager
FROM Department_Table
ORDER BY "Department Name";
English: We have aliased three columns. The first column does not need double quotes. The second column needs them because there is a
space between Department and Name. The third ALIAS does not have the word AS. The word AS is optional when aliasing. Also notice we
are sorting by "Department Name". We once again use the double quotes.
Hint - Man If a user desires to assign an alias with a space like "Department Name", be sure to use double quotes around the alias
name, otherwise you don't need the quotes. For example, Department_Name does not need quotes.
Issues:
Page 7 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
Solutions:
You can place double quotes around the ALIAS name and it is no longer considered a keyword in the query. For example, if you had an ALIAS
named MAX you could place double quotes around it to become "MAX".
( NAMED <alias-name> )
Solutions:
For reserved word errors place double quotes around the ALIAS name and Teradata will accept the ALIAS. For example, if you had an ALIAS named
MAX you could place double quotes around it to become "MAX".
ORDER BY Clause
Teradata brings back data unsorted unless you use the ORDER BY statement. The ORDER BY statement will sort the given <columnname> in the desired order. The default for Teradata is ascending order.
SELECT <column-name>
[...,<column-name>]
FROM
<table-name>
ORDER BY { <column-name> | <relative-column-number> } [ ASC | DESC ]
{, <column-name> | <relative-column-number> } [ ASC | DESC ] ;
3637: Invalid ORDER BY constant. This error occurs when using the number of a column rather than its name in the ORDER BY clause. In most
cases the number used is larger than the actual count of columns involved in the query.
Solutions:
Identifying how many columns should be returned to the result set is the first thing to do. Next, find the proper number for the column, or columns
desired in the ORDER BY and adjust the SQL.
DISTINCT
If there are duplicate values in <column-name> Teradata will return all rows. Distinct allows the user to eliminate all the duplicates and only
return one instance of that value.
SELECT DISTINCT <column-name>
[, <column-name> ]
FROM <table-name>;
Page 8 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited
TeradataSQLQuickReferenceGuide:SimplicitybyDesign,SecondEdition
FROM Student_Course_Table;
English: SELECT every distinct Student_Id from the Student_Course_Table. No duplicate Student_Ids will be returned.
SELECT DISTINCT First_Name, Dept_No
FROM Employee_Table ;
English: SELECT every distinct combination of First_Name and Dept_No. For example, Mary in Dept_No 10 is different than Mary in
Dept_No 20. Both would be returned.
Hint - Man This is a useful function but this may cause inaccurate results. The issue with DISTINCT is that it affects all the columns
desired in the result set. If there are two columns in the query, Teradata will search for a unique combination of both. In the
second example, the rows returned will be any distinct combinations of First_Name and Dept_No.
Hint - Man Often users will either use DISTINCT or GROUP BY for the same purpose. DISTINCT Is faster when there are only a few
duplicates and GROUP BY is faster when there are Many duplicates.
Page 9 / 9
Reprintedforibm\mc.sekhar@in.ibm.com,IBM
CoffingDataWarehousing,CoffingPublishing(c)2006,CopyingProhibited