Intro To Teradata SQL
Intro To Teradata SQL
Objectives
What is an RDBMS?
Question
Who is the manager of employee 1004? [Answer]
2:
What is SQL?
Structured Query Language is used to define the answer set that is returned
from the Teradata Database.
GO TO
PERFORM
DO LOOP
OPEN FILE
CLOSE FILE
END OF FILE
SQL Commands
SQL Function
statement
SELECT Select data from one or more tables.
INSERT Place a new row into a table.
UPDATE Change data values in one or more existing rows.
DELETE Remove one or more rows from a table.
Relational Concepts
The intersection of a row and a column is a data value. Data values come
from a particular domain. Domains represent the pool of legal values for a
column. For example, the data values in the EMPLOYEE NUMBER and
MANAGER EMPLOYEE NUMBER come from the same domain because both
columns come from the pool of employee numbers. The domain of
employee numbers might be defined as the pool of integers greater than
zero.
The EMPLOYEE NUMBER column is marked PK, which indicates that this
column holds the Primary Key. The next 3 columns are marked FK, which
stands for Foreign Key.
The purpose of the Primary Key is to uniquely identify each record. No
two values in a primary key column can be identical.
A Foreign Key represents the Primary Key of another table. Relationships
between tables are formed through the use of Foreign Keys.
2.) Teradata SQL
Objectives
SELECT
Structured Query Language (SQL) consists of three types of statements,
previously defined as:
Our focus in this course will be mostly on the DML portion of the language. We
will first look at the SELECT statement.
The SELECT statement allows you to retrieve data from one or more tables. In its
most common form, you specify certain rows to be returned as shown.
SELECT *
FROM employee
WHERE department_number = 401;
The asterisk, "*", indicates that we wish to see all of the columns in the table.
The FROM clause specifies from which table in our database to retrieve the rows.
The WHERE clause acts as a filter which returns only rows that meet the specified
condition, in this case, records of employees in department 401.
Note: SQL does not require a trailing semicolon to end a statement but the Basic
Teradata Query (BTEQ) utility that we use to enter SQL commands does require
it. All examples in this course will include the semicolon.
This query would return all columns and all rows from the employee table.
Instead of using the asterisk symbol to specify all columns, we could name
specific columns separated by commas:
SELECT employee_number
,hire_date
,last_name
,first_name
FROM employee
WHERE department_number = 401;
employee_number hire_date last_name first_name
-------------------- ---------- ----------- -----------
1004 76/10/15 Johnson Darlene
1003 76/07/31 Trader James
1013 77/04/01 Phillips Charles
1010 77/03/01 Rogers Frank
1022 79/03/01 Machado Albert
1001 76/06/18 Hoover William
1002 76/07/31 Brown Alan
Unsorted Results
Results come back unsorted unless you specify that you want them sorted in a
certain way. How to retrieve ordered results is covered in the next section.
ORDER BY Clause
Use the ORDER BY clause to have your results displayed in a sorted order.
Without the ORDER BY clause, resulting output rows are displayed in a random
sequence.
SELECT employee_number
,last_name
,first_name
,hire_date
FROM employee
WHERE department_number = 401
ORDER BY hire_date;
Sort Direction
In the example above, results will be returned in ascending order by hire date.
Ascending order is the default sort sequence for an ORDER BY clause. To explicitly
specify ascending or descending order, add ASC or DESC, to the end of the ORDER
BY clause. The following is an example of a sort using descending sequence.
You may indicate the sort column by naming it directly (e.g., hire_date) or by
specifying its position within the SELECT statement. Since hire_date is the
fourth column in the SELECT statement, the following ORDER BY clause is
equivalent to saying ORDER BY hire_date.
ORDER BY 4;
The order in which columns are listed in the ORDER BY clause is significant. The
column named first is the major sort column. The second and subsequent are
minor sort columns. In the following example, results are sorted by department
number in ascending order. Where multiple records share the same department
number, those rows are sorted by job_code in ascending order. The following
are examples:
SELECT employee_number
,department_number
,job_code
FROM employee
WHERE department_number < 302
ORDER BY department_number
,job_code;
employee_number department_number job_code
-------------------- ---------------------- ----------
801 100 111100
1025 201 211100
1021 201 222101
1019 301 311100
1006 301 312101
1008 301 312102
Note: Each column specified in the ORDER BY clause can have its own sort order,
either ascending or descending.
SELECT employee_number
,department_number
,job_code
FROM employee
WHERE department_number < 302
ORDER BY department_number ASC
,job_code DESC;
employee_number department_number job_code
-------------------- ---------------------- ----------
801 100 111100
1021 201 222101
1025 201 211100
1008 301 312102
1006 301 312101
1019 301 311100
DISTINCT
SELECT department_number
,job_code
FROM employee
WHERE department_number = 501;
department_number job_code
---------------------- ----------
501 512101
501 512101
501 511100
Note: Two people in department 501 have the same job code (512101). If our
purpose is simply to find out which job codes exist in department 501, we could
use DISTINCT to avoid seeing duplicate rows.
Note: DISTINCT appears directly after SELECT, and before the first named
column. It may appear to apply only to the first column, but in fact, DISTINCT
applies to all columns named in the query. Two rows in our result example set
both have department_number 501. The combination of department_number and
job_code are distinct since the job codes differ.
All Teradata objects, such as tables, must be assigned a name by the user when
they are created.
Accounts
Accounts_2005
accounts_over_$2000
Account#
Equivalence
Accounts and accounts represent the same table. Case is not considered.
Naming Rules
Naming Syntax
Database names and User names must be unique within the Teradata
Database.
Table, view, macro, trigger, index and stored procedure names must be
unique within a Database or User.
Column names must be unique within a Table.
Databasename.Tablename.Columnname
Example
NAME (unqualified)
Coding Conventions
SQL is considered a 'free form' language, that is, it can cover multiple lines of
code and there is no restriction on how much 'white space' (i.e., blanks, tabs,
carriage returns) may be embedded in the query. Having said that, SQL is
syntactically a very precise language. Misplaced commas, periods and
parenthesis will always generate a syntax error.
The following coding conventions are recommended because they make it easier
to read, create, and change SQL statements.
Recommended Practice
SELECT last_name
,first_name
,hire_date
,salary_amount
FROM employee
WHERE department_number = 401
ORDER BY last_name;
Not Recommended Practice
The first example is easy to read and troubleshoot (if necessary). The second
example appears to be a jumble of words. Both, however, are valid SQL
statements.
Default Database
As a valid user, you will normally have access rights to your own user database
and the objects it contains. You may also have permission to access objects in
other databases.
The user name you logon with is usually your default database. (This depends on
how you were created as a user.)
.logon rayc;
password: xyz
Note the dot (.) before "logon". Commands that begin with a dot are BTEQ
commands, not SQL commands. The BTEQ command processor will read this
command; the SQL processor will not see it.
Queries you make that do not specify the database name will be made against
your default database.
For example:
DATABASE payroll;
sets your default database to payroll. Subsequent queries (assuming the proper
privileges are held) are made against the payroll database.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
Save the file with your answers, as you will enter and run
these exercises using BTEQ scripts for the Labs in Module 3.
Objectives
What is BTEQ?
BTEQ is a front end tool for submitting SQL queries. 'BTEQ' stands for Basic
Teradata Query program.
TDP's come in two varieties - the standard TDP for channel-attached clients, and
the Micro-TDP (or MTDP) for network-attached clients.
They are involved in getting your SQL requests routed to a Parsing Engine (PE)
which validates your request and passes it to the Access Module Processors
(AMPs). AMPs retrieve the answer sets from the disks and send the response set
back to the PE which in turn forwards it to the TDP and ultimately back to you at
your session.
BTEQ Commands
For purposes of this course, instructions for starting a lab session are contained
on each lab page.
There are two ways to submit SQL statements to BTEQ in interactive mode:
When users log on to BTEQ, they are by default assigned a single session.
Session Parameters
Example
You may also activate the ANSI Flagger, which automatically flags non-ANSI
compliant syntax with a warning but still returns the expected answer set.
Example
.LOGON etc.....
*** SQL Warning 5821 Built-in values DATE and TIME are not ANSI.
*** Query completed. One row found. One column returned.
*** Total elapsed time was 1 seconds.
Date
--------
05/01/12
Example
.LOGON etc.........
Date
--------
05/01/12
Teradata extensions cannot be disabled, but they can be flagged for warning
using the ANSI flagger.
The BTEQ .SHOW CONTROL command displays BTEQ settings. The following output
shows the result of this command.
.SHOW CONTROL
EXPORT RESET
IMPORT FIELD
LOGON L7544/tdxxx
RUN
[SET] ECHOREQ = ON
[SET] ERRORLEVEL = ON
[SET] FOLDLINE = OFF ALL
[SET] FOOTING = NULL
[SET] FORMAT = OFF
[SET] FORMCHAR = OFF
[SET] FULLYEAR = OFF
[SET] HEADING = NULL
[SET] INDICDATA = OFF
[SET] NOTIFY = OFF
[SET] NULL = ?
[SET] OMIT = OFF ALL
[SET] PAGEBREAK = OFF ALL
[SET] PAGELENGTH = 55
[SET] QUIET = OFF
[SET] RECORDMODE = OFF
[SET] REPEATSTOP = OFF
[SET] RETCANCEL = OFF
[SET] RETLIMIT = No Limit
[SET] REPEATSTOP = OFF
[SET] RETCANCEL = OFF
[SET] RETLIMIT = No Limit
[SET] RETRY = ON
[SET] RTITLE = NULL
[SET] SECURITY = NONE
[SET] SEPARATOR = two blanks
[SET] SESSION CHARSET = ASCII
[SET] SESSION SQLFLAG = NONE
[SET] SESSION TRANSACTION = BTET
[SET] SESSIONS = 1
[SET] SIDETITLES = OFF for the normal report.
[SET] SKIPDOUBLE = OFF ALL
[SET] SKIPLINE = OFF ALL
[SET] SUPPRESS = OFF ALL
[SET] TDP = L7544
[SET] TIMEMSG = DEFAULT
[SET] TITLEDASHES = ON for the normal report.
[SET] UNDERLINE = OFF ALL
[SET] WIDTH = 75
BTEQ Scripts
A BTEQ script is a file that contains BTEQ commands and SQL statements. A
script is built for sequences of commands that are to be executed on more than
one occasion, i.e. monthly, weekly, daily.
How to create a BTEQ Script
To create and edit a BTEQ script, use an editor on your client workstation. For
example, on a UNIX workstation, use either the vi or text editor.
Start BTEQ, then enter the following BTEQ command to submit a BTEQ script:
The Teradata RDBMS supports comments that span multiple lines by using the
"/*" and "*/" as delimiters.
Script Example
Let's look at an example of a BTEQ script using ANSI standard comments. The
ANSI comment delimiter is the double dash, --.
SELECT department_number
,department_name
FROM department;
.QUIT
Note: Both Teradata style (/* */) and ANSI style (--) comments may be
included in any SQL command script.
.run file=mod2exer.scr
Teradata BTEQ 04.00.01.00 for UNIX5. Enter your logon or BTEQ command:
.run file=mod2exer.scr (this is the only user input; the rest of the commands came
from file mod2exer.scr)
.run file=mod2exer.scr
Teradata BTEQ 04.00.01.00 for UNIX5. Enter your logon or BTEQ command:
.LOGON L7544/tdxxx,
SELECT department_number
,department_name
FROM department;
department_number department_name
------------------ -----------------
401 customer support
<rest of result rows are not shown in this screen capture>
.QUIT
*** You are now logged off from the DBC.
*** Exiting BTEQ...
*** RC (return code) = 0
# exit
If you prefer to write all SQL statements in a single script file, there are two
ways you may execute the script:
Start BTEQ and redirect standard input to use the script file.
From a UNIX workstation, use the script <filename> command to capture the
output of your session :
The contents of <scriptname> and responses from the Teradata database will be
output to both the screen and the designated <filename> if one has been
designated.
From a UNIX workstation, issue the following UNIX command to stop the script
command:
$exit
This closes the logging file but does not terminate the UNIX session.
BTEQ output results will display a "$" to indicate the location where the error was
detected in either the script or request. The following is an example of captured
output showing a syntax error:
Teradata BTEQ 04.00.00.00 for UNIX5. Enter your logon or BTEQ command:
.logon L7544/tdxxx,
SELECT department_number
,DISTINCT department_name
FROM department;
,DISTINCT department_name
$
*** Failure 3706 Syntax error, 'DISTINCT' that follows the ','
should be deleted.
The dollar sign points to the command DISTINCT. The error text tells us that
DISTINCT should be deleted. The DISTINCT command must directly follow the
SELECT command in your request.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
Do this by:
Objectives
• CREATE
• REPLACE
• DROP
• EXECUTE
NAMED NULLIFZERO/ZEROIFNULL
SHOW SUBSTR
TITLE TRIM
WITH WITH . . . BY
EXPLAIN
HELP
SHOW
The HELP Command is used to display information about database objects such
as (but not limited to):
HELP retrieves information about these objects from the Data Dictionary. Below
are the syntactical options for various forms of the HELP command:
HELP Command
HELP
DATABASE databasename;
HELP USER username;
HELP TABLE tablename;
HELP VIEW viewname;
HELP MACRO macroname;
HELP COLUMN table or viewname.*; (all columns)
HELP COLUMN table or viewname.colname . . ., colname;
Some of the other HELP commands which will be seen later in this course
include:
The HELP DATABASE command in the following example shows all objects in the
Customer_Service database. Objects may be recognized by their 'Kind'
designation as follows:
Table =T
View =V
Macro =M
Trigger =G
Join Index =J
Stored Procedure =P
HELP DATABASE Customer_Service;
All objects in this database are tables. The '?' is how BTEQ displays a null,
indicating that no user comments have been entered.
Note: To return HELP information on an object, a user must either own the
object or have at least one privilege on it.
The HELP TABLE command shows the name, data type, and comment (if
applicable), of all columns in the specified table:
manager_employee_number I ?
department_number I Department employee
works in
birthdate DA ?
salary_amount D Annual compensation
amount
Data types are as follows:
Use the HELP SESSION command to see specific information about your current
SQL session. The following output returns session information for user DBC
including the user name, log-on date and time, default database, and other
information related to his current session:
.SET FOLDLINE ON
.SET SIDETITLES ON
The SHOW command displays the current Data Definition Language (DDL) of a
database object (e.g., Table, View, Macro, Trigger, Join Index or Stored
Procedure). The SHOW command is used primarily to see how an object was
created.
Command Returns
CREATE TABLE
SHOW TABLE tablename;
statement
CREATE VIEW
SHOW VIEW viewname;
statement
SHOW MACRO CREATE MACRO
macroname; statement
BTEQ also has a SHOW command (.SHOW CONTROL) which is different from the SQL
SHOW command. It provides information on formatting and display settings for
the current BTEQ session.
The SHOW TABLE command seen here returns the CREATE TABLE statement that
was used to create the employee table. To perform a SHOW TABLE, the user must
have a privilege on either the table itself or the containing database.
Note: If a table is subsequently altered after it has been created, the SHOW
command will always reflect the most current alterations.
The SHOW VIEW command shows you the CREATE VIEW statement used to create
a view:
Result
The SHOW MACRO command shows you the statement used to create a macro:
Result
The EXPLAIN function looks at a SQL request and responds in English how the
optimizer plans to execute it. It does not actually execute the SQL statement
however it is a good way to see what database resources will be used in
processing your request.
For instance, if you see that your request will force a full-table scan on a very
large table or cause a Cartesian Product Join, you may decide to re-write a
request so that it executes more efficiently.
Explanation
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
Sometimes the BTEQ Instructions get hidden behind the Telnet
Window. You will need these instructions to log on to Teradata.
Objectives
To locate rows for which a numeric column is within a range of values, use the
BETWEEN <a> AND <b> operator. Specify the upper and lower range of values
that qualify the row.
The BETWEEN operator looks for values between the given lower limit <a> and
given upper limit <b> as well as any values that equal either <a> or <b>
(i.e., BETWEEN is inclusive.)
Example
Select the name and the employee's manager number for all employees
whose job codes are in the 430000 range.
SELECT first_name
,last_name
,manager_employee_number
FROM employee
WHERE job_code BETWEEN 430000 AND 439999;
first_name last_name manager_employee_number
----------- ----------- ------------------------------
Loretta Ryan 801
Armando Villegas 1005
SELECT first_name
,last_name
,manager_employee_number
FROM employee
WHERE job_code >= 430000
AND job_code <= 439999;
Note that using BETWEEN requires only one mention of the column name being
tested, whereas without BETWEEN the column name must be provided for each
condition tested.
Use the BETWEEN <a> AND <b> operator to locate rows for which a character
column is within a range of values. Specify the upper and lower range of values
that qualify the row. BETWEEN will select those values which are greater than or
equal to <a> and less or equal to <b>. (BETWEEN is inclusive.)
SELECT last_name
FROM employee
WHERE last_name BETWEEN 'r' AND 's';
last_name
------------
Ryan
Note: 'Stein' is not included because 'S_____' sorts lower than 'Stein'. (Teradata
is not case-sensitive by default.)
Set Operator IN
Use the IN operator as shorthand when multiple values are to be tested. Select the
name and department for all employees in either department 401 or 403. This
query may also be written using the OR operator which we shall see shortly.
SELECT first_name
,last_name
,department_number
FROM employee
WHERE department_number IN (401, 403);
Use the NOT IN operator to locate rows for which a column does not match any of
a set of values. Specify the set of values which disqualifies the row.
SELECT first_name
,last_name
,department_number
FROM employee
WHERE department_number NOT IN (401, 403) ;
NOT IN vs. OR
Select the name and the department for all employees who are NOT members of
departments 401 and 403.
SELECT first_name
,last_name
,department_number
FROM employee
WHERE NOT (department_number=401
OR
department_number=403);
NULL
Use NULL in a SELECT statement, to define that a range of values either IS NULL
or IS NOT NULL.
The following table lists two employees with unknown telephone extensions:
SELECT employee_number
FROM employee_phone
WHERE extension IS NULL;
employee_number
--------------------
1025
1005
SELECT employee_number
FROM employee_phone
WHERE extension IS NOT NULL;
employee_number
--------------------
1008
1013
LIKE Operator
The LIKE operator searches for patterns matching character data strings.
You must provide two parameters for the LIKE operator: a string expression to
be searched and a string pattern for which to search.
The string can contain specific characters, as well as the following "wildcards":
String pattern
Meaning:
example:
LIKE 'JO%' begins with 'JO'
LIKE '%JO%' contains 'JO' anywhere
LIKE '__HN' contains 'HN' in 3rd and 4th position
LIKE '%H_' contains 'H' in next to last position
Case-Blind Compare
The UPPER function converts a string to uppercase. Use the UPPER string
function on both strings being compared to ensure a case-blind comparison
regardless of the session transaction mode. The LOWER function may be
similarly used.
The NOT CASESPECIFIC data attribute can also be used to force case-blind
compares, however it is not an ANSI-compliant operator.
Problem
Display the full name of employees whose last name contains the letter "r"
followed by the letter "a".
SELECT first_name
,last_name
FROM employee
WHERE last_name LIKE '%ra%';
first_name last_name
------------------------------ --------------------
James Trader
Peter Rabbit
I.B. Trainer
Robert Crane
Larry Ratzlaff
Since we are in Teradata (BTET) mode, the default for comparisons is non-case-
specific. Thus, we could have also used LIKE '%Ra%' or LIKE '%RA%' and
produced the same result.
SELECT first_name
,last_name
FROM employee
WHERE last_name LIKE '%ra%';
first_name last_name
------------------------------ --------------------
James Trader
I.B. Trainer
Robert Crane
Since we are in ANSI mode, the default for comparisons is case-specific. Thus,
we will not pick up Ratzlaff or Rabbit, unless we use 'case blind' testing, seen in
the next example.
SELECT first_name
,last_name
FROM employee
WHERE LOWER(last_name) LIKE LOWER('%ra%');
first_name last_name
------------------------------ --------------------
James Trader
Peter Rabbit
I.B. Trainer
Robert Crane
Larry Ratzlaff
By applying the LOWER function to each side of the comparison, all testing is
done in a 'case blind' mode. UPPER could have also been used. We could also
have used LIKE '%ra%' (NOT CASESPECIFIC) to produce this result,
however this is not an ANSI standard approach.
Use the CASESPECIFIC data attribute on one or both of the string expressions
being compared to ensure a case-sensitive comparison, regardless of the session
transaction mode. CASESPECIFIC is a Teradata extension.
Problem
Display the full name of employees whose last name contains "Ra". This is a
case-sensitive test.
SELECT first_name
,last_name
FROM employee
WHERE last_name (CASESPECIFIC) LIKE '%Ra%';
first_name last_name
------------------------------ --------------------
Peter Rabbit
Larry Ratzlaff
Because we used the case-specific designator, we don't get James Trader in our
answer set but only get answers that contain an uppercase "R". The name
'LaRaye' would have also appeared in this answer set, if it existed in the table.
Using LIKE 'Ra%' will return only names that begin with "Ra".
SELECT first_name
,last_name
FROM employee
WHERE last_name LIKE '%Ra%';
first_name last_name
------------------------------ --------------------
Peter Rabbit
Larry Ratzlaff
Use of the Teradata extension (CASESPECIFIC) is not required. This could have
also been submitted with LIKE 'Ra%' and produced the same result, however it
would have missed a name like 'LaRaye'.
To extend the pattern matching functions of the LIKE operator, use quantifiers.
ANY and SOME are synonyms. Using LIKE ANY and LIKE SOME will give the same
result.
Problem
Display the full name of all employees with both "E" and "S" in their last name.
Solution
SELECT first_name
,last_name
FROM employee
WHERE last_name LIKE ALL ('%E%', '%S%');
first_name last_name
---------- ----------
John Stein
Carol Kanieski
Arnando Villegas
Problem
Display the full name of all employees with either an "E" or "S" in their last
name.
Solution
SELECT first_name
,last_name
FROM employee
WHERE last_name LIKE ANY ('%E%', '%S%');
first_name last_name
----------- -----------
John Stein
Carol Kanieski
Arnando Villegas
Darlene Johnson
James Trader
The "_" and "%" symbols are used as 'wildcards' in the string expression of the
LIKE construct. However, what if one of the wildcard symbols is in the expression
you are evaluating. For example, to search for the substring "95%", you must
define an ESCAPE character to instruct the Parser to treat the '%' as a non-
wildcard character.
To Summarize:
Problem
List all objects defined in the Teradata Database Data Dictionary whose names
contain "_" (underscore) as the second character.
Solution
SELECT tablename
FROM dbc.tables
WHERE tablename LIKE '_Z_%' ESCAPE 'Z';
TableName
------------------------------
M_7_B
t_wt_erd1
Things To Notice
Example
Logical operators AND, OR and NOT allow you to specify complex conditional
expressions by combining one or more logical expressions.
The logical operator AND combines two expressions, both of which must be true
in a given record for them to be included in the result set.
Problem
Display the name and employee number of employees in department 403 who
earn less than $35,000 per year.
Solution
SELECT first_name
,last_name
,employee_number
FROM employee
WHERE salary_amount < 35000.00
AND department_number = 403 ;
Logical Operator -- OR
The logical operator OR combines two expressions. At least one must be true in a
given record for it to be included in the result set.
Problem
Display the name and the employee number for employees who either earn less
than $35,000 annually or work in department 403.
Solution
SELECT first_name
,last_name
,employee_number
FROM employee
WHERE salary_amount < 35000.00
OR department_number = 403;
Result
Multiple AND . . . OR
You want to find all employees in either department 401 or department 403
whose salaries are either under $35,000 or over $85,000.
SELECT last_name
,salary_amount
,department_number
FROM employee
WHERE (salary_amount < 35000
OR salary_amount > 85000)
AND (department_number = 401
OR department_number = 403) ;
In the absence of parentheses, SQL uses the default precedence of the Boolean
operators. By default, NOT has higher precedence than AND which has higher
precedence than OR. Operators that have the same precedence level are
evaluated from left to right (e.g., multiple ORs or multiple ANDs).
Problem
Select the name, department number, and job code for employees in
departments 401 or 403 who have job codes 412101 or 432101.
Solution
SELECT last_name
,department_number
,job_code
FROM employee
WHERE (department_number = 401
OR department_number = 403)
AND (job_code = 412101
OR job_code = 432101) ;
Result
If we accidentally left the parentheses out of our statement, would we get the
same results?
SELECT last_name
,department_number
,job_code
FROM employee
WHERE department_number = 401
OR department_number = 403
AND job_code = 412101
OR job_code = 432101;
last_name department_number job_code
---------- --------------------- ---------
Villegas 403 432101
Johnson 401 412101
Trader 401 411100
Question
James Trader does not have either job code. Why was he selected?
Answer
Without parentheses, the default precedence causes the expression to be
evaluated as follows:
SELECT last_name
,department_number
,job_code
FROM employee
WHERE department_number = 401
OR (department-number = 403
AND job_code = 412101)
OR job_code = 432101;
Remember, if any of the expressions that OR combines are true, the result is
true. Since James Trader is in department_number 401, that expression
evaluates to true. Therefore the row qualifies for output.
Multiple AND
Use multiple ANDs if you need to locate rows which meet all of two or more sets
of specific criteria.
For example, what happens if we take the previous example, and replace all the
ORs with ANDs?
SELECT last_name
,department_number
,job_code
FROM employee
WHERE department_number = 401
AND department_number = 403
AND job_code = 412101
AND job_code = 432101;
No rows will be found. Why?
Answer
Problem
Find all employees in department number 403 with job_code 432101 and a
salary greater than $25,000.
Solution
SELECT last_name
,department_number
,job_code
,salary_amount
FROM employee
WHERE department_number = 403
AND job_code = 432101
AND salary_amount > 25000.00 ;
Result
Logical NOT
Problem
Select the name and employee number of employees NOT in department 301.
SELECT first_name
,last_name
,employee_number
FROM employee
WHERE department_number NOT = 301;
SELECT first_name
,last_name
,employee_number
FROM employee
WHERE NOT (department_number = 301);
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
Objectives
Data Types
Every column in a row is associated with a data type that defines the kind of
values it accepts. These data types are associated with the fields when the table
is created. Data types fall into one of the four categories shown below:
Note:
Time, Timestamps, Intervals are
covered in the Teradata SQL Advanced
course.
Note:
Geospatial data types are covered in
the SQL Application Development course.
Character Data
There are two data types for holding character data:
In both cases, the maximum string length is 64,000 characters. String size is
specified in parentheses after the keyword CHAR or VARCHAR as in CHAR(20) or
VARCHAR(20). There are subtle differences between the two.
In the case of CHAR, if you insert a character string less than the specified size
(20 in the above example), the system will append and store trailing blanks as
needed to expand the string to the specified length. The size specified on a
VARCHAR field represents a maximum length. The system will not pad the
contents with trailing blanks. (It will use two extra bytes to store the length
internally however.)
When you know that data will always be a specific length (e.g., a Mail Stop that
is a four-character length) use CHAR. You save the two bytes that VARCHAR would
use to store the length. When data does not have a fixed length (e.g., last
names), VARCHAR makes more efficient use of the space, because it does not pad
values with trailing blanks.
Character strings may be defined using any one of the following character sets:
KanjiSJIS 64,000
Unicode 32,000 (two bytes per character)
Examples
Byte Data
BYTE.
VARBYTE.
BYTE is for fixed length binary strings. VARBYTE is for variable length binary
strings. These data types are used for storing binary objects such as digital
images, executable objects, flat files, etc.
Numeric Data
Date Data
The following chart shows how the DATE data type is stored internally as an
integer by Teradata.
In the first example, the date being represented is September 21, 1999.
In the second example, the date being represented is February 15, 2000.
Note that the year is represented as an offset from the year 1900 in both cases.
Note, this is how the date is stored internally. How you see the date in a report
depends on how the date is formatted.
The recommended display format for DATE values is ‘YYYY-MM-DD’. This format
will avoid problems that may occur from formats specifying only two digits for
the year.
Example
Note: Formatting techniques for dates are discussed in the forthcoming module
'Output Attributes'.
Arithmetic Operators
Teradata provides six binary arithmetic operators and two unary arithmetic
operators. They are defined in the tables below:
Operator Definition
** exponentiation
* Multiply
/ Divide
MOD Modulos (remainders)
+ Add
- Subtract
Operator Definition
+ Positive value
- Negative value
** (exponentiation)
MOD (modulo arithmetic)
Example
4**3 = 4 * 4 * 4 = 64
Example
60 MOD 7 = 4
Sixty divided by 7 equals 8, with a remainder of 4
Arithmetic Functions
Teradata supports several arithmetic functions which provide additional
capabilities beyond those available in ANSI SQL. They are described in the table
below:
Function Result
ABS (arg) Absolute value
EXP (arg) Raises e to the power of arg (e ** arg)
LOG (arg) Base 10 logarithm
LN (arg) Base e (natural) logarithm
SQRT (arg) Square root
Examples
SQRT (16) = 4
Computed Values
Example
SELECT last_name
,first_name
,salary_amount/12
FROM employee
WHERE department_number = 401
ORDER BY last_name ;
salary_amount/
last_name first_name
12
---------- ----------
--------------------
Johnson Darlene 3025.00
Trader James 3154.17
Character Literals
SELECT 'Employee'
,last_name
,first_name
FROM employee ;
Employee last_name first_name
-------- --------- ----------
Employee Stein John
Employee Kanieski Carol
Employee Ryan Loretta
Numeric Constants:
SELECT 12345
,last_name
,first_name
FROM employee ;
12345 last_name first_name
----- --------- ----------
12345 Stein John
12345 Kanieski Carol
12345 Ryan Loretta
Calculator Mode:
MOD Operator
Example
7 ÷ 4 = 1 remainder of 3
Therefore 7 MOD 4 = 3
Problem
SELECT customer_number
,zip_code
FROM location
WHERE zip_code MOD 10000 = 0;
customer_number zip_code
------------------- -----------
10 940710000
14 1210000
You could do this search with string comparisons, but it is more efficient to use
MOD because no character conversion is required.
The Teradata DATE data type is used to store calendar dates representing year,
month and day. It is stored internally as a four-byte integer.
Month-to-month conversions.
Year-to-year conversions.
Leap-year conversions.
Dates prior to 1900.
Dates after 2000.
The database treats the DATE data type as an INTEGER value but does not permit
invalid calendar dates. The system encodes dates using the formula:
For dates before Jan 1, 1900, the integer calculation returns a negative number.
Remember, this is only how the date is stored, not how it is represented in a
query output.
DATE Arithmetic
Find Syntax
The date 30 days from today SELECT DATE + 30;
Since days is the basic unit, we need to express a year in terms of the number of
days it contains. DATE + 365 (or in a leap-year, DATE + 366), gives us the date
one year from today.
Find Syntax
The date 1 year from now SELECT DATE + 365;
SELECT (DATE-birthdate)/365
FROM employee
WHERE last_name = 'Stein';
((Date-birthdate)/365)
----------------------
45
This example takes today's date, (encoded as an integer) and "subtracts" John
Stein's birth date, (also encoded as an integer) and divides by the number of
days in a year (365).
Suppose today's date is 980826 and John's birth date is 531015. If you did this
as a straight integer subtraction, you would find that 980826 - 531015 = 449811
- not a legal date. Because these are DATE data types, not INTEGERS, the
subtraction operator actually returns the number of days between the two
operands instead of subtracting the INTEGERs.
(DATE - birthdate) thus provides us with the number of days elapsed between a
birth date and today. Dividing that amount by the number of days in a year
(365) gives us John's age in years. To account for the leap year, we should
divide by 365.25 to get a more precise number of years.
Problem
SELECT last_name
,hire_date
FROM employee
WHERE (DATE-hire_date)/365 > 30;
last_name hire_date
-------------------- ---------
Trainer 73/03/01
Crane 78/01/15
Rogers 77/03/01
Brown 76/07/31
Stein 76/10/15
Phillips 77/04/01
Hopkins 77/03/15
Lombardo 77/02/01
Kanieski 77/02/01
Trader 76/07/31
Wilson 78/03/01
Johnson 76/10/15
Rogers 78/03/01
Hoover 76/06/18
Villegas 77/01/02
Runyon 78/05/01
Ryan 76/10/15
Daly 77/03/15
The example shows that operations using DATE may be included in the WHERE
clause.
ADD_MONTHS (date, n)
Query Results
SELECT DATE; /* March 20, 2001 */ 01/03/20
SELECT ADD_MONTHS (DATE, 2) 2001-05-20
SELECT ADD_MONTHS (DATE, 12*14) 2015-03-20
SELECT ADD_MONTHS (DATE, -3) 2000-12-20
Note: The results of the ADD_MONTH function are always displayed in YYYY-MM-DD
format.
ADD_MONTHS may also be applied to a literal date, which must be specified in the
YYYY-MM-DD format.
Query Results
SELECT ADD_MONTHS ('2004-07-31', 2) 2004-09-30
SELECT ADD_MONTHS ('2003-12-31', 2) 2004-02-29
SELECT ADD_MONTHS ('2003-12-31', 14) 2005-02-28
ADD_MONTHS accounts for the Gregorian calendar and knows how many days are
in each month, including leap years such as 2004.
Show the date two month from today (March 20, 2001) using both ADD_MONTHS
and simple arithmetic methods.
SELECT DATE
,ADD_MONTHS(DATE, 2)
,DATE + 60;
Time is represented as a TIME data type which in reality carries three different
fields of information: hours, minutes and seconds. It also has 'clock intelligence'
in its implementation, just as DATE has calendar intelligence, thus making
possible complex calculations.
TIMESTAMP is a data type which combines both DATE and TIME into a single data
type.
Both TIME and TIMESTAMP have a WITH ZONE option, which allows for time-zone
dependent storage and processing of time related information.
Note: TIME and TIMESTAMP data types are covered in greater detail the
Advanced Teradata SQL Web-based training.
1. Year-Month Intervals
2. Day-Time Intervals
Year-Month Intervals
YEAR
YEAR TO MONTH
MONTH
Day-Time Intervals
DAY
DAY TO HOUR
DAY TO MINUTE
DAY TO SECOND
HOUR
HOUR TO MINUTE
HOUR TO SECOND
MINUTE
MINUTE TO SECOND
SECOND
Note: INTERVAL data types are covered in greater detail the Advanced Teradata
SQL Web-based training.
The CAST function allows you to convert a value or expression from one data
type to another. For example:
When you cast from a decimal to an integer, the system discards everything to
the right of the decimal point. In our example, even though the decimal portion
was .75, making the number closer to 50501 than to 50500, the result of the
cast is still 50500.
As you can see from the example above, CAST can take the CHAR(20) last_name
data field from the employee table and convert it to a CHAR(5) data type. The
values displayed as a result of this query are truncated to five characters or
padded with trailing blanks up to five characters (if there are less than five
characters to start with).
Alternative Syntax
In current and previous versions of Teradata, data type conversions can also be
done without the CAST function as seen here:
This type of conversion syntax is considered shorthand and is not ANSI standard.
Casting Attributes
Things To Notice:
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
7.) Subqueries
Subqueries
Please Note: This is a narrated presentation. You may control the pacing of this
module by using the Pause button as well as other buttons available on the
screen duing the presentation.
Start Presentation
After viewing the presentation, return to this window and continue to the next
lab activity.
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
Click on the buttons to the left to see the answers for each lab
question.
Note: This lab is used again for Lab B in the Inner Join
Module.
Objectives
Use TITLE to add a heading to your output that differs from the
column or expression name.
Use AS to specify a name for a column or expression in a SELECT
statement.
Use CHARACTERS to determine the number of characters in a string.
Use TRIM to Trim blank characters or binary zeroes from data.
Use FORMAT to alter the display of a column or expression.
The AS clause:
SELECT last_name
,first_name
,salary_amount / 12 AS monthly_salary
FROM employee
WHERE department_number = 401
ORDER BY monthly_salary;
last_name first_name monthly_salary
----------- ----------- --------------------
Johnson Darlene 3025.00
Trader James 3154.17
Note: Once you have renamed a column with the AS clause you may re-use that
name within your SQL statement. Renaming does not prevent you from using
any of the other methods we have discussed in your statement, for example:
order by salary_amount/12
order by salary_amount
order by 3 (using the positional reference)
Teradata Extensions
The Teradata RDBMS still supports the following form of specifying a name for a
column or expression:
SELECT last_name
,first_name
,salary_amount / 12 (NAMED monthly_salary)
FROM employee
WHERE department_number = 401
ORDER BY monthly_salary
Warning! NAMED is the old way of renaming a column for output display. AS is
the new method. Currently, both attributes work. In the future, support for the
NAMED attribute will disappear. Since the Teradata extension described above
provides equivalent functionality to the ANSI compliant version, all existing
Teradata SQL applications that use this form to name a column or expression
should be updated to use the ANSI compliant form (AS).
TITLE Attribute
The TITLE attribute allows you to rename columns for output with headings
that may contain multiple lines and/or blanks. Column headings may stack up
to three levels.
SELECT last_name
,first_name
,salary_amount / 12 (TITLE 'MONTHLY // SALARY')
FROM employee
WHERE department_number = 401
ORDER BY 3;
MONTHLY
last_name first_name SALARY
----------- ----------- ------------
Johnson Darlene 3025.00
Trader James 3154.17
The new title must appear between single quotes ( ' ) in the TITLE phrase.
The // is used to represent a line break, causing the stacking. You can do the
same thing with AS, which is the ANSI standard. Literal strings must be
enclosed with double quotes (") when using AS.
SELECT last_name
,first_name
,salary_amount / 12 AS "MONTHLY // SALARY"
FROM employee
WHERE department_number = 401
ORDER BY 3;
MONTHLY
last_name first_name SALARY
----------- ----------- ------------
Johnson Darlene 3025.00
Trader James 3154.17
CHARACTERS Function
Problem
To find all employees who have more than five characters in their first name.
Solution
SELECT first_name
FROM employee
WHERE CHARACTERS (first_name) > 5;
first_name
------------
Loretta
Darlene
Arnando
TRIM Function
Use the TRIM function to suppress leading and/or trailing blanks in a CHAR
column or leading and/or trailing binary zeroes in a BYTE or VARBYTE column.
TRIM is most useful when performing string concatenations.
Problem
List the employees who have exactly four characters in their last name. The
data type of last_name is CHAR(20).
Solution 1
SELECT first_name
,last_name (TITLE 'last')
FROM employee
WHERE CHAR (TRIM (TRAILING FROM last_name)) = 4;
Solution 2
SELECT first_name
,last_name(TITLE 'last')
FROM employee
WHERE CHAR(TRIM(last_name))=4;
first_name last_name
----------- -----------
Loretta Ryan
James Daly
Note: When TRIM is specified with no additional directives, both leading and
trailing blanks are trimmed.
The || (double pipe) symbol is the concatenation operator that creates a new
string from the combination of the first string followed by the second.
Example 1:
Concatenating of literals without the TRIM function:
Name
------------------------
Jones , Mary
Example 2:
Name
------------
Jones,Mary
TRIM may also be used to remove characters other than blanks and binary
zeroes. Using the TRIM function format in the following examples, any defined
character(s) may be trimmed from a character string.
Example 1:
Trim_String
----------------
PAUL
Example 2:
Trim_String
----------------
PAUL??????
Example 3:
Trim_String
----------------
??????PAUL
FORMAT Phrase
The FORMAT phrase can be used to format column output and override the
default format. For example:
salary_amount
---------------
$36,300.00
The five dollar signs ($) in front of the FORMAT operand indicate that we want
to float the leading dollar sign until we hit the first significant digit. For
example, if the amount had been 6300.00, it would have been displayed as
$6,300.00.
Things To Notice
FORMAT allows you to format column output for a calculation. The format
character 'Z' is used to represent leading zero suppression. It works similarly
to the '$' in that it floats to the first significant digit.
Problem
Management has decided to give employee 1004 a $1,000 raise and wants to
know what percent the increase will be:
Solution
Increase Percentage
----------------------
3%
FORMAT Characters
Examples
The FORMAT mask can contain specifications for up to 18 digits of data plus
other insertion characters. If you needed to have 18 zero-suppressed decimal
digits, you could note it as Z(18) rather than ZZZZZZZZZZZZZZZZZZ.
DATE Formats
09/04/28
It is unclear whether this format represents
Let's look at some unambiguous DATE formats which show all four digits of the
year:
SYNTAX RESULT
FORMAT 'YYYY/MM/DD’ 2009/04/28
FORMAT 'DDbMMMbYYYY' 28 Apr 2009
FORMAT 'mmmBdd,Byyyy' Apr 28, 2009
FORMAT 'DD.MM.YYYY' 28.04.2009
Example
Date
-------------
Mar-01
Note: Date formatting options may also use expanded syntax (i.e., MMM-DD).
Example
Date
------------------
2010:March:060
SELECT last_name
,first_name
,hire_date (FORMAT 'mmmBdd,Byyyy')
FROM employee
ORDER BY last_name;
Problem
SELECT last_name
,first_name
,birthdate (FORMAT 'mmdd') AS birthday
,birthdate AS whole_date
FROM employee
WHERE department_number = 401
ORDER BY 3;
This solution does not produce the desired sorted result. Note that Brown's
birthday sorts between Rogers and Johnson whereas ideally, it should follow
them. The ORDER BY 3 sorts on the full internal representation of the
birthdate column (i.e., year, month and day). The FORMAT specification
suppresses the year in the viewable output but not in the sort.
The MODULO function works similarly to long division. Just as when we divide
7 by 3, we get a quotient of 2 and a remainder of 1. MODULO does exactly
the opposite. 7 MOD 3 discards the quotient and keeps only the remainder.
Thus, 7 MOD 3 = 1.
SELECT last_name
,first_name
,birthdate MOD 10000 (FORMAT '9999')
AS birthday
,birthdate AS whole_date
FROM employee
WHERE department_number = 401
ORDER BY 3;
The EXTRACT function allows for easy extraction of year, month and day from
any DATE data type. The following examples demonstrate its usage.
Query Result
SELECT DATE; /* March 20,2001 */ 01/03/20 (Default format)
SELECT EXTRACT(YEAR FROM DATE); 2001
SELECT EXTRACT(MONTH FROM DATE); 03
SELECT EXTRACT(DAY FROM DATE); 20
Date arithmetic may be applied to the date prior to the extraction. Added
values always represent days.
Query Result
SELECT EXTRACT(YEAR FROM DATE +
2002
365);
SELECT EXTRACT(MONTH FROM DATE +
04
30);
SELECT EXTRACT(DAY FROM DATE + 12); 01
Note: CURRENT_DATE is the ANSI standard for today's date and may be used
in place of DATE, which is Teradata specific.
The EXTRACT function may also be applied against the current time. It permits
extraction of hours, minutes and seconds. The following examples
demonstrate its usage.
Query Result
SELECT TIME; /* 2:42 PM */ 14:42:32 (Default format)
SELECT EXTRACT(HOUR FROM TIME); 14
SELECT EXTRACT(MINUTE FROM TIME); 42
SELECT EXTRACT(SECOND FROM TIME); 32
Time arithmetic may be applied prior to the extraction. Added values always
represent seconds.
Query Result
SELECT EXTRACT(HOUR FROM TIME +
14
20);
SELECT EXTRACT(MINUTE FROM TIME +
42
20);
SELECT EXTRACT(SECOND FROM TIME +
52
20);
SELECT EXTRACT(SECOND FROM TIME +
Invalid Time
30);
Note: CURRENT_TIME is the ANSI standard for current time and may be used
in place of TIME, which is Teradata specific. While TIME is a Teradata function,
it does not have the intelligence of the DATE data type, thus adding 30
seconds to 32 seconds produces an invalid 62 for the seconds portion. For
true time and timestamp arithmetic, the TIME and TIMESTAMP data types are
needed. These capabilities are covered in the Advanced SQL course.
The FORMAT phrase controls the display of the result but does not change the
underlying data in any way.
Example
SELECT last_name
,first_name
,first_name (FORMAT 'X')
FROM employee
WHERE last_name = 'Brown'
ORDER BY 3;
last_name first_name first_name
----------- ----------- -----------
Brown Alan A
Brown Allen A
Question
Answer
Problem
Show the last name, first name, and first initial of all employees named
Brown. Sequence by the first initial.
SELECT last_name
,first_name
,CAST (first_name AS CHAR(1))
FROM employee
WHERE last_name = 'Brown'
ORDER BY 3;
last_name first_name first_name
----------- ----------- -----------
Brown Allen A
Brown Alan A
Question
Answer
Yes, because we have actually CAST the first_name to a new data type of
CHAR(1).
Note: Both of the above examples would return an error if run in an ANSI
mode session. This is because ANSI mode does not support character
truncation as part of a CAST operation.
Attribute Functions
TYPE
TITLE
FORMAT
NAMED
CHARACTERS
Query Results
SELECT DISTINCT TYPE (job_code) FROM
job; INTEGER
SELECT DISTINCT TITLE (job_code)FROM job_code
job;
SELECT DISTINCT FORMAT (job_code)FROM (10)9
job;
SELECT DISTINCT NAMED (job_code)FROM job_code
job;
SELECT DISTINCT CHARACTERS(last_name) 20
FROM employee;
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
Objectives
Join
A join is a technique for accessing data from more than one table in an
answer set. Tables are joined according to columns they have in common. A
join between the employee table and the department table could be done
according to department number. An example of an inner join will be seen on
the next screen:
Joins may access data from tables, views or a combination of the two.
Types of Joins
To get a report that includes employee number, last name, and department
name, join the employee table and the department table.
Department number is the common column that determines the way data in
these two tables will be joined. Most often joined columns are related to each
other as primary and foreign keys.
. . .
. . .
. . .
We fully qualified every column referenced in our SELECT statement to include the
table that the column is in ( e.g., employee.employer_number). It is only
necessary to qualify columns that have identical names in both tables (i.e.,
department_number).
The ON clause is used to define the join condition used to link the two tables.
An alias is a temporary name for a TABLE or VIEW defined in the FROM clause. It
can be useful for abbreviating long table names and is required to join a table to
itself.
Once the alias name is defined, it must be used throughout the SQL statement.
SELECT e.employee_number
,e.last_name
,d.department_name
FROM employee e INNER JOIN
department d
ON e.department_number = d.department_number;
employee_number last_name department_name
------------------- ---------- --------------------
1006 Stein research and development
1008 Kanieski research and development
1005 Ryan education
1004 Johnson customer support
1007 Villegas education
1003 Trader customer support
Note: The "e" in the FROM clause is what sets up "e" as the employee table alias.
Similarly, the "d" sets up the department table alias. Alias names can be up to
30 characters.
Aliases can be used in the SELECT part of the statement even though SELECT
appears before FROM in a statement. Once an alias has been established, it
should be used consistently thoughout the query. Using the original table name
as a qualifier instead of the alias may produce unexpected results as we shall see
in a later example.
Cross Joins
A Cross Join is a join that requires no join condition (Cross Join syntax does not
allow an ON clause). Each participating row of one table is joined with each
participating row of another table. The WHERE clause restricts which rows
participate from either table.
SELECT e.employee_number
,d.department_number
FROM employee e CROSS JOIN
department d
WHERE e.employee_number = 1008;
employee_number department_number
-------------------- ----------------------
1008 301
1008 501
1008 402
1008 201
1008 302
1008 600
1008 401
1008 100
1008 403
The employee table has 26 rows. The department table has 9 rows. Without the
WHERE clause, we would expect 26 x 9 = 234 rows in our result set. With the
constraint that the employee_number must equal 1008 (which only matches one
row in the employee table), we now get 1 x 9 = 9 rows in our result set.
Cross Joins by themselves often do not produce meaningful results. This result
shows employee 1008 associated with each department. This is not meaningful
output. Cross joins can however produce useful information as will be seen in the
following pages.
Cartesian Product
SELECT employee.employee_number
,employee.department_number
FROM employee CROSS JOIN
department;
Each employee row (26) matched with each department row (9) yields 234 rows
of output. An 8,000,000 row table and a 50,000 row table would yield a
400,000,000,000 row answer set. The output of a Cartesian product is often not
meaningful however they do have useful application as we shall see.
Cartesian products may also result accidentally from an INNER JOIN with
improper aliasing or missing join conditions.
There are some real-world uses for Cartesian product joins. One important use is
to benchmark system performance with large data throughputs. Cartesian
products make it easy to generate very large answer sets.
Example
Any query requiring all elements of one table to be compared to all elements of
another is a candidate for a Cartesian product using the CROSS JOIN syntax.
Example
SELECT e.last_name
,d.department_name
,j.description
FROM employee e INNER JOIN
department d
ON e.department_number = d.department_number
INNER JOIN
job j
ON e.job_code = j.job_code;
last_name department_name description
----------- -------------------- ------------
Daly software support Manager - Software Supp
Runyon marketing sales Manager - Marketing Sales
Trainer president Corporate President
Brown customer support Dispatcher
… … …
An accidental Cartesian product can result from an Inner Join with improper
aliasing:
SELECT employee.employee_number
,d.department_name
FROM employee e INNER JOIN
department d
ON e.department_number = d.department_number;
We forgot to use the "e" alias in the SELECT statement. The parser sees that "e"
needs to be joined with "d". That's two tables and one join. So far, so good. Then
the parser sees a reference to employee and views it as a third table. We now
have three tables and only one join, resulting in an unintended Cartesian
product.
A Cartesian product also may result from an Inner Join with an improper join
condition:
SELECT e.employee_number
,d.department_name
FROM employee e INNER JOIN
department d
ON 3 = 3;
In the above example, since 3 = 3 is always true, every row meets this
condition. Every row in the department table is joined with every row in the
employee table. We probably do not want this result.
Self Joins
A self join occurs when a table is joined to itself. Which employees share the
same surname Brown and to whom do they report?
Result
A Subquery qualifies which rows selected in the main query will be in the answer
set. Data selected in the subquery will not be included in the answer set.
A Join qualifies which rows from two or more tables will be matched to create
rows of an answer set. The answer set can include data from one or more of the
joined tables.
List the first name, last name and department number of all employees who
work in research departments.
Using a JOIN
SELECT employee.first_name
,employee.last_name
,employee.department_number
FROM employee INNER JOIN
department
ON employee.department_number = department.department_number
WHERE department.department_name LIKE '%Research%';
Using a SUBQUERY
SELECT first_name
,last_name
,department_number
FROM employee
WHERE department_number IN
(SELECT department_number FROM department
WHERE department_name LIKE '%Research%');
Result
Note that in some cases, either a join or a subquery may be used to produce the
same result.
Use EXPLAIN to Understand a Join
Use EXPLAIN to see how the database will execute your query. EXPLAIN is
especially useful for checking to see that you are not creating an unintended
Cartesian product.
EXPLAIN
SELECT employee.first_name
,employee.last_name
,employee.department_number
FROM employee INNER JOIN
department
ON employee.department_number = department.department_number
WHERE department.department_name LIKE '%Research%';
Explanation
EXPLAIN a Subquery
You can also use EXPLAIN against your subqueries.
EXPLAIN
SELECT first_name
,last_name
,department_number
FROM employee
WHERE department_number IN
(SELECT department_number
FROM department
WHERE department_name LIKE '%Research%');
Explanation
Question
The query on this page and the one from the Join on the previous page produce
equivalent results. How could you compare their expected performance on the
system?
One way is to compare some of the basic characteristics divulged by the EXPLAIN
.The Join query took six steps and estimated that it would return 24 rows and take
approximately 0.23 seconds to complete.
In comparison, the Subquery took five steps, estimated that it would return 28
rows and take approximately 0.24 seconds to complete.
The two implementations of this query are roughly equivalent from a performance
standpoint.
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
Answers: A. List the employee number, last name, salary, and job
Lab A description of all employees except the President. (The
Lab B President is the only employee assigned to Department
Lab C 100.) Format last name as 9 characters and description
Lab D as 20 characters. Sort by employee number within job
description.
Lab E
B. Modify Lab D from the Subquery module to use a join
instead of a subquery to select information about
California customers. EXPLAIN the request and note the
total estimated time. Compare with the total estimated
time from Lab D in the Subquery module. Which is more
efficient: the join or the subquery?
C. Produce a report showing employee number,
department number and manager’s last name for all
employees hired before Jan 1, 1978. Sort by
department number.
D. Do an EXPLAIN statement using CROSS JOIN on the
employee and job tables. Select job code, job
description and employee last name.
E. Management has a request. They need the last name,
department name, job description, and phone number
of all employees in the 213 area code with job code
412101. Use table name aliases for your joins. Order by
last name.
Outer Joins
Please Note: This is a narrated presentation. You may control the pacing of this
module by using the Pause button as well as other buttons available on the
screen duing the presentation.
Start Presentation
After viewing the presentation, return to this window and continue to the next
lab activity.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
Answers: A. List the last name, job code and job description of all
Lab A employees. Cast last name as 9 characters, and job
Lab B description as 20 characters. Sort by last name. Also,
Lab C include employees who have an invalid or null
Lab D job_codes. Use outer join syntax to accomplish this.
B. Show the last name, job description and department
name of all employees with valid departments and
valid job codes. Order by last name. Format last name
as 9 characters, job description as 20 characters and
the department name as 13 characters.
C. Modify exercise B to include employees with valid
departments but without valid job codes.
D. Modify exercise B to include unassigned job codes.
Objectives
Set Operators
The following are graphic representations of the three set operators, INTERSECT,
UNION and EXCEPT. All are ANSI-compliant operators.
INTERSECT
The INTERSECT operator returns rows from multiple sets which share some
criteria in common.
UNION
The UNION operator returns all rows from multiple sets, displaying duplicate rows
only once.
EXCEPT
The EXCEPT operator subtracts the contents of one set from the contents of
another.
Note: Using the Teradata keyword ALL in conjuction with the UNION operator
allows duplicate rows to remain in the result set.
UNION Operator
Before the introduction of the OUTER JOIN feature, The UNION operator provides
functionality similar to the OUTER JOIN feature. UNION is still a viable feature,
however. Here are some rules for its usage:
Problem
Show manager 1019 identifying him as the manager, show his employees and
identify each of them as an employee.
Solution
SELECT first_name
,last_name
,'employee' (TITLE 'employee//type')
FROM employee
WHERE manager_employee_number = 1019
UNION
SELECT first_name
,last_name
,' manager '
FROM employee
WHERE employee_number = 1019
ORDER BY 2
;
Result
employee
first_name last_name type
Carol Kanieski employee
Ron Kubic manager
John Stein employee
INTERSECT Operator
Problem
Solution
SELECT manager_employee_number
FROM employee
INTERSECT
SELECT manager_employee_number
FROM department
ORDER BY 1
;
Result
manager_employee_number
801
1003
1005
1011
1017
1019
1025
EXCEPT Operator
*** SQL warning 5811 MINUS of queries is not in entry level ANSI.
Problem
Solution
SELECT manager_employee_number
FROM department
EXCEPT
SELECT manager_employee_number
FROM employee
ORDER BY 1
;
Result
manager_employee_number
1016
1099
Set operators may be used in most SQL constructs but cannot contain a WITH or
WITH...BY clause. Set operators are evaluated in order of precedence, as
follows:
INTERSECT
UNION
EXCEPT from left to right
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
Objectives
Data Definition Language (DDL) is used by SQL to create, modify, and remove
object definitions:
When executed, the CREATE TABLE statement creates and stores the table
structure definition in the Data Dictionary (DD).
TABLE options.
COLUMN definitions.
TABLE level constraints.
INDEX definitions.
Note: We cover Table level constraints in the Advanced SQL course. Our
discussion here will focus on table options, column definitions, and index
definitions.
Element Definition
Fallback.
Journaling.
Teradata Extensions
FALLBACK specifies that the system builds a duplicate copy of each row of the
table and stores it on a different (FALLBACK) AMP within the cluster.
JOURNAL specifies that the system stores BEFORE and /or AFTER images for each
changed row of the table in the specified permanent journal, providing disaster
recovery capability.
Example
COLUMN Definitions
Aspect Meaning
Column Name Name of the column
Data Type
Column data type (INTEGER, CHAR, etc.)
Column Storage
Column compression for nulls or for specific values
Attributes
Example
Example
The WITH DEFAULT option is a special case of the DEFAULT option. The
WITH DEFAULT phrase is converted by the Teradata database to a
DEFAULT option in which the default system value for that data type
becomes the value to use in place of null. For example, character
strings will be defaulted to spaces and numeric columns will default
to zeroes.
A DEFAULT function exists which permits the retrieval of the default value
assigned to a particular column. For example, assume we define a table as
follows:
We may retrieve the default values for any column by using the DEFAULT
function as follows:
Default values are returned in place of the normal column values. Note that two
rows are returned. This is because there are currently two rows in the table. One
row of output is returned for each existing row in the table. You may return a
single row by using the following syntax:
Default(Col2) Default(Col3)
------------- -------------
10 20
If no default values are assigned to the specified column, then a null is returned.
Teradata Extensions
The COMPRESS phrase allows values in one or more columns of a permanent table
to be compressed to zero space, thus reducing the physical storage space
required for a table.
Examples
In the second example, both null and 'Savings' account types will be suppressed.
The value of 'Savings' is written in the table header for bank_account_data on
each AMP in the system.
Primary Key (PK) - is defined as one or more columns used to uniquely identify
each row in a table. PKs are used in conjunction with foreign keys to define the
important column relationships in a database. PKs are always unique and cannot
be null. PKs are not known to the Teradata RDBMS as such. The Teradata
RDBMS implements a primary key as a unique index.
Primary Secondary
The following table illustrates performance characteristics for SQL operations that
use indices.
Entries are listed in order from most desirable to least desirable in terms of
system resource use. They show the number of AMPs involved in each operation
and number of rows that could be returned.
#AMPS #ROWS
UPI 1 0 or 1
The following example shows everything we have learned up to this point about
the CREATE TABLE statement.
For INDEX definition, if UNIQUE is not explicit, then the default is a non-unique
index. Similarly, if PRIMARY is not explicit, then the default is a secondary index.
Examples:
DROP TABLE
To remove all data associated with a table, as well as the table structure
definition from the Data Dictionary, use the DROP TABLE statement.
Example
To remove all data associated with a table, without dropping the table definition
from the Data Dictionary, use the DELETE statement.
Example
Delete the employee data from the emp_date table in the previous example
without dropping the table.
(All three examples are valid syntax which perform the operation.)
DELETE emp_data;
ALTER TABLE
Once a table has been created, certain characteristics are not alterable, such as
the Primary Index choice. To change them you must CREATE a new table which
includes the new characteristics, then populate that table.
Other characteristics are alterable. You can use the ALTER TABLE statement to
modify these characteristics.
ALTER TABLE
Indexes may be defined when creating a table with the CREATE TABLE
statement, or alternatively, they may be created after the fact using the CREATE
INDEX statement.
Primary indexes are always created at table creation time. Secondary indexes
may be created at table creation or after the fact.
Example
USI (named)
Note: We know this is a USI because primary indexes can't be created (except
when the table is created). This index is named 'fullname'.
NUSI (unnamed)
CREATE INDEX
(job_code)
ON emp_data;
HELP INDEX
HELP INDEX <tablename> displays index definitions for the specified table. If the
index is unnamed, the "Index Name" will display as NULL. Naming indexes is
optional, however named indexes provide more ease of use in SQL commands
we shall see later.
Note: HELP INDEX may sometimes produce a very wide report. In this case, the
best way to view the output is to use the .SET FOLDLINE and .SET SIDETITLES
features of BTEQ.
Y USI on
Unique?:
last_name,first_name combined
Primary//or//Secondary?: S
Column Names: last_name,first_name
Index Id: 8
Approximate Count: 0
fullname The name we gave
Index Name:
to the index
The values in the Index Id column correlate to the index numbers referenced in
EXPLAIN text. The Primary index always has an Index ID of 1; Secondary
indices have Index IDs in multiples of 4.
DROP INDEX
If you are dropping a named index, you can specify the index by either the index
name or its column definition.
If you are dropping an unnamed index, you must specify the index by naming
the columns which are associated with it.
Example
Note: Only one secondary index may be dropped per DROP statement.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
Objectives
Data Manipulation
INSERT
-Add a row to a table.
INSERT SELECT
-Add rows to a table from another table.
UPDATE
-Change column values in existing rows of a table.
DELETE
-Remove rows from a table.
INSERT
Example
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Note: Columns for which no values are inserted, will receive either their
assigned default value (as provided by the DEFAULT or WITH DEFAULT
options in the CREATE TABLE statement), or a NULL value if no default is
specified but nulls are allowed.
INSERT SELECT
The SELECT portion of the statement may be used to define a subset of rows
and/or a subset of columns to be inserted to the target table.
Problem
Solution
First create the table 'emp_copy' with the same table definition as 'emp', then do
a simple INSERT SELECT.
Assumes:
Problem
Create a table for tracking birthdays and populate it with data from the employee
table.
Solution
First, create a table which will contain the required rows and columns.
Then, use INSERT SELECT to populate the new table. Only the required columns are
selected for insertion. All rows are included.
BIRTHDAYS
BIRTH
EMPNO LNAME FNAME
DATE
PK
1006 Stein John 531015
1008 Kanieski Carol 580517
1005 Ryan Loretta 550910
1004 Johnson Darlene 460423
1007 Villegas Arnando 370131
1003 Trader James 470619
EMPLOYEE
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Note: .
In the Advanced Teradata SQL course we will see a feature of SQL which
permits the CREATE TABLE statement and the INSERT SELECT statement to be
combined into a single statement called CREATE TABLE WITH DATA.
UPDATE
UPDATE allows you to modify one or many columns of one or many rows in a
single table.
The WHERE condition can include:
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Problem
Change employee 1010's department to 403, job code to 432101, and manager
to 1005 in the employee table:
UPDATE employee
SET department_number = 403
,job_code = 432101
,manager_employee_number = 1005
WHERE employee_number = 1010
;
EMPLOYEE (After)
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Problem
Update the employee table to give everyone in all support departments a 10%
raise. Department numbers for all of the support departments are not known.
DEPARTMENT
MANAGER
DEPARTMENT DEPARTMENT BUDGET
EMPLOYEE
NUMBER NAME AMOUNT
NUMBER
PK FK
EMPLOYEE
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Solution 1
Using Subquery:
UPDATE employee
SET salary_amount=salary_amount * 1.10
WHERE department_number IN
(SELECT department_number
FROM department
WHERE department_name LIKE '%Support%')
;
Solution 2
Using Join:
UPDATE employee
SET salary_amount=salary_amount * 1.10
WHERE employee.department_number =
department.department_number
AND department_name LIKE '%Support%'
;
Note: In an update, you can't use the ON clause, so the join condition is specified
in the WHERE clause.
DELETE
DELETE allows you to delete rows from a single table. If no WHERE clause is
specified, then all rows are deleted.
Problem
EMPLOYEE
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Solution
Problem
EMPLOYEE
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
Solution
Problem
EMPLOYEE
MANAGER
EMPLOYEE DEPARTMENT JOB LAST FIRST HIRE BIRTH SALARY
EMPLOYEE
NUMBER NUMBER CODE NAME NAME DATE DATE AMOUNT
NUMBER
PK FK FK FK
DEPARTMENT
MANAGER
DEPARTMENT DEPARTMENT BUDGET
EMPLOYEE
NUMBER NAME AMOUNT
NUMBER
PK FK
Solution 1
Using Subquery:
Using Join:
Note: In either case, no rows are removed from the 'employee' table because no
current employees are assigned to the 'Temp' department.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window. Sometimes
the BTEQ Instructions get hidden behind the Telnet Window.
You will need these instructions to log on to Teradata.
Objectives
Overview of Macros
A macro allows you to name a set of one or more statements. When you need
to execute those statements, simply execute the named macro. Macros provide a
convenient shortcut for executing groups of frequently-run SQL statements.
The following table is a fairly complete list of commands you may use to
manipulate macros.
CREATE MACRO
The CREATE MACRO command allows you to define a set of SQL statements and
put them into an executable statement.
Example
EXEC birthday_list;
last_name first_name birthdate
----------- ----------- ---------------
Morrissey Jim 43/04/29
Short Michael 47/07/07
Notice that there is a semicolon before the closing parenthesis. This is a required
element of macro syntax. The CREATE MACRO statement will fail if there is a
syntax error anywhere in the body of the SQL statement. If a macro exists, it is
guaranteed to have previously been syntax checked. This does not guarantee
that it will work without errors because objects referenced within the macro
(e.g., the employee table) may not exist any longer or may have changed.
EXECUTE Macro
To execute a macro, simply precede its name with the EXEC command.
EXEC birthday_list;
last_name first_name birthdate
----------- ----------- ---------------
Morrissey Jim 43/04/29
Short Michael 47/07/07
DROP Macro
This command removes the macro from the containing database and also
removes its entry from the Data Dictionary.
REPLACE Macro
You need to resubmit the entire contents of amacro to modify it. It is convenient
to display current macro contents so you can cut and paste them into a text
editor. You may then modify the macro without retyping the entire command. To
display the definition of a macro, use the SHOW command as seen here:
Result
In editing the macro, change the CREATE command to the REPLACE command,
and make the appropriate changes, such as adding a minor sort to the ORDER BY
clause. Comments may optionally be added.
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
or
2. CREATE MACRO tdxxx.Lab 7_A as...
Objectives
Macros Revisited
Macros contain one or more prewritten SQL statements that are stored in the
Teradata Data Dictionary. They can be executed from any viable SQL front-end,
including:
Note: If a user has EXEC privileges he may execute the macro. It doesn't matter
whether or not he has privileges for the underlying tables or views that the
macro uses.
Example
In parentheses following the macro name is the parameter list. It names each
parameter followed by its data type. When a parameter is used in the body of a
macro, it is always preceded by a colon.
Employee Table:
Manager
Employee Employee Department Job Last First
Number Number Number Code Name Name
1008 1019 301 312102 Kanieski Carol
1010 1003 401 412101 Rogers Frank
1014 1011 402 422101 Crane Robert
1006 1019 301 312101 Stein John
1017 801 501 511100 Runyon Irene
1003 801 401 411100 Trader James
1019 801 301 311100 Kubic Ron
1011 801 402 421100 Daly James
1013 1003 401 412102 Phillips Charles
1009 1005 403 432101 Lombardo Domingus
1025 801 201 211100 Short Michael
1024 1005 403 432101 Brown Allen
1007 1005 403 432101 Villegas Arnando
1005 801 403 431100 Ryan Loretta
1021 1025 201 222101 Morrissey Jim
1020 1005 403 432101 Charles John
1022 1003 401 412102 Machado Albert
801 801 100 111100 Trainer I.B.
1015 1017 501 512101 Wilson Edward
1002 1003 401 413201 Brown Alan
1004 1003 401 412101 Johnson Darlene
1016 801 302 321100 Rogers Nora
1018 1017 501 512101 Ratzlaff Larry
1001 1003 401 412101 Hoover William
1023 1017 501 512101 Rabbit Peter
1012 1005 403 432101 Hopkins Paulene
To Execute
Result
last_name
------------
Stein
Kanieski
Kubic
Macros may have more than one parameter. Each name and its associated type
are separated by a comma from the next name and its associated type. The
order is important. The first value in the EXEC of the macro will be associated
with the first value in the parameter list. The second value in the EXEC is
associated with the second value in the parameter list, and so on.
Example
Results:
employee_number
-------------------
1006
1008
In this example, 301 is associated with the department number and 50000 is
associated with the salary amount.
In the example above, the value after 102 has been omitted, hence the two
commas in a row. In such cases the parameter value will be set to the default
specified for in the CREATE MACRO statement. In this case it is 0.
Note, for this example to work, both the budget and manager columns must
allow nulls or have a default value in the department table definition.
Lab
Try It! For this set of lab questions you will need information from the
Database Info document.
To start the online labs, click on the Telnet button in the lower left
hand screen of the course. Two windows will pop-up: a BTEQ
Instruction Screen and your Telnet Window.
To do the following labs, you will first need to create and populate your
own copy of the department table. This may be accomplished by
executing the following commands:
DATABASE tdxxx;
Objectives
Aggregate Operators
Example
SELECT
COUNT ( salary_amount ) (TITLE 'COUNT')
,SUM ( salary_amount ) (TITLE 'SUM SALARY')
,AVG ( salary_amount ) (TITLE 'AVG SALARY')
,MAX ( salary_amount ) (TITLE 'MAX SALARY')
,MIN ( salary_amount ) (TITLE 'MIN SALARY')
FROM employee ;
Result
Note: If one salary amount value had been NULL, the COUNT would have
returned a count of 5. In that case, the average would have reflected an average
of only five salaries. To COUNT all table rows use COUNT (*), which will count rows
regardless of the presence of NULLs.
Suppose we would like to find the total amount of money spent by each
department on employee salaries. Without the GROUP BY clause, we could
attempt to get an answer by running a separate query against each department.
Solution
GROUP BY provides the answer with a single query, regardless of how many
departments there are.
SELECT department_number
,SUM (salary_amount)
FROM employee
GROUP BY department_number
;
department_number Sum(salary_amount)
--------------------- ---------------------
401 74150.00
403 80900.00
301 58700.00
The WHERE clause eliminates some rows before GROUP BY puts them into desired
groupings.
Problem
Solution
SELECT department_number
,SUM (salary_amount)
FROM employee
WHERE department_number IN (401, 403)
GROUP BY department_number
;
Sum
department_number
(salary_amount)
----------------------
-----------------------
403 80900.00
401 74150.00
GROUP BY does not imply any ordering of the output. An ORDER BY clause is
needed to control the order of the ouput.
Problem
For departments 301, 401 and 403, find the total number of employees in each
department as well as the highest, lowest and average salaries for each
department. Order results by department number.
It is possible and often desireable to GROUP BY more than one column. This
permits the query to compute aggregates of groups within groups. In the
following example, we are looking for salaries by job code (one group) within
department (second group).
By job code, what are the total salaries for departments 401 and 403?
SELECT department_number
,job_code
,SUM (salary_amount)
FROM employee
WHERE department_number IN (401, 403)
GROUP BY 1, 2
ORDER BY 1, 2
;
department_number job_code SUM (salary_amount)
--------------------- ---------- -----------------------
401 411100 37850.00
401 412101 107825.00
401 412102 56800.00
401 413201 43100.00
403 431100 31200.00
403 432101 201800.00
2 Rules of Aggregations:
Note: Repeating values such as '401' above may be suppressed using the BTEQ
command:
This will cause the value to be displayed only the first time.
HAVING is just like WHERE , except that it applies to groups rather than rows.
HAVING qualifies and selects only those groups that satisfy a conditional
expression.
Problem
Here is the order of evaluation within a SQL statement if all four clauses are
present:
WHERE
GROUP BY
HAVING
ORDER BY
Lab
Try It! For this set of lab questions, you will need information from
the Database Info document.
Objectives
The WITH...BY clause is a Teradata extension that creates subtotal lines for a
detailed list. It differs from GROUP BY in that detail lines are not eliminated.
The WITH...BY clause allows subtotal "breaks" on more than one column and
generates an automatic sort on all "BY" columns.
Solution
Note: The "BY DEPT" phrase causes an automatic sort on the Dept column.
Result
Multiple aggregates within the same WITH BY clause are possible if all aggregates
are based on the same 'break' column. In the following example, both
aggregates are generated by department. Note that while WITH BY produces
aggregates similar to the GROUP BY functionality, the difference is that GROUP BY
does not show the detail rows upon which the aggregates are based.
Problem
Show salary subtotals and averages by department and also show the individual
employee salaries.
Solution
Result
------------
------------
Dept Total 74150.00
Dept Avg 37075.00
------------
Dept Total 80900.00
Dept Avg 40450.00
Note: The BY DEPT clause causes results to be sorted by department.
You may use several "WITH BY" clauses together in an SQL statement.
The order in which you specify them determines the order of the sorted output.
Problem
Show total salaries by department and by job code within department for
departments 401 and 501. Also show the detail salary for each employee.
Solution
Result
------------
DEPT TOTAL 245575.00
------------
DEPT TOTAL 200125.00
Note: Because there are two BY clauses in this query, their sequence in the
query affects how the final report is sorted. Because 'BY Dept' is the last one
specified, it becomes the highest level sort column, thus the report is sorted as
job code within department.
Problem
Display employee numbers and salary amounts for department 301 and a final
total for the department.
Solution
SELECT employee_number
,salary_amount
FROM employee
WHERE department_number = 301
WITH SUM(salary_amount)(TITLE 'GRAND TOTAL')
ORDER BY employee_number
;
Result
employee_number salary_amount
------------------------ --------------------
1006 29450.00
1008 29250.00
1019 57700.00
------------
GRAND TOTAL 116400.00
DISTINCT Modifier
The DISTINCT modifier is used in conjuction with the COUNT aggregate to prevent
the same value from being counted more than once.
Problem
Count the number of managers for employees numbered between 1003 and
1008.
Solution
SELECT
employee_number
,department_number
,manager_employee_number (NAMED manager)
FROM employee
WHERE employee_number BETWEEN 1003 AND 1008
WITH COUNT (manager)(TITLE 'TOTAL MANAGERS')
;
Result
Note that even though the count is represented as six, there are really only four
managers.
Solution
SELECT
employee_number
,department_number
,manager_employee_number AS manager
FROM employee
WHERE employee_number BETWEEN 1003 AND 1008
WITH COUNT (DISTINCT manager)(TITLE 'TOTAL MANAGERS')
;
Result
Note: When using DISTINCT with an aggregate function only a single column or
expression may be used.
Legal Example:
SELECT COUNT(DISTINCT(job_code))
,COUNT(DISTINCT(employee_number)).....
Illegal Example:
Problem
Show the salary for each employee with subtotals by department, a final total,
and results sorted by employee name.
Solution
Results
Note that the WITH BY clause controls the high level sort by department. The
ORDER BY clause is applied as a minor level sort, producing a sorted list of names
within sorted departments.
WITH and GROUP BY clauses may both appear in the same query. The GROUP BY
clause is needed when both aggregates and non-aggregates are in the SELECTed
columns. The WITH clause is needed when a final aggregation of all the groups is
desired.
Problem
Show the total and average salaries for each department plus a final total and
average for the company.
Solution
Result
Will provide subtotals, subcounts, and subaverages, and also show detail
rows.
Summarylist can specify more than one column.
Breaklist can specify more than one column.
Implied ORDER BY on the breaklist columns.
WITH...BY determines the major sort key(s).
ORDER BY specifies any additional minor sorts.
An SQL statement can have several WITH...BY clauses.
The highest level of sort is the last specified WITH...BY clause.
Is not supported by ANSI standard or ODBC clients.
WITH summarylist:
The WITH clause produces grand total results for the entire answer set. This
clause is typically used when you need to produce final totals and also wish to
see the detail rows.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
Objectives
Simple Rankings
The RANK function permits a column to be ranked, either based on high or low
order, against other rows in the answer set. You may specify the order of
ranking by use of the ORDER BY clause within the RANK function. Descending
order will provide traditional ranking wherein the largest value is assigned a rank
of one. Ascending order will associate the largest value with the highest rank
value (i.e., typically a value > 1).
Most of the examples we will see are rankings of sales figures, where the larger
the amount, the better (i.e. lower number) the ranking.
where 'sales' ' represents the column to be ranked and the descending sort key
of the result.
Problem
Solution
SELECT storeid
,prodid
,sales
,RANK( ) OVER (ORDER BY sales DESC) AS "Rank"
FROM salestbl
WHERE storeid = 1001;
Result
Things To Notice:
Problem
SELECT storeid
,prodid
,sales
,RANK( ) OVER (ORDER BY sales ASC) AS "Rank"
FROM salestbl
WHERE storeid = 1001;
Result
Things To Notice:
When the order of sales is ASC, the lowest sales amount is rank #1
Rank #1 always appears at top of list unless overridden.
The QUALIFY clause allows restriction of which rankings will be output in the
final result. In the second query at hand, the QUALIFY clause restricts the result
to the top three sales amounts.
QUALIFY performs like the HAVING clause by requesting a specific range in the
output.
Problem
Solution
SELECT storeid
,prodid
,sales
,RANK( ) OVER (ORDER BY sales DESC)
AS "Ranking"
FROM salestbl
QUALIFY Ranking <= 3;
Result
Things To Notice:
QUALIFY shows the ranking for the top 3 sales amounts only.
When the order of sales is DESC, the highest sales amount is rank #1
Variations On a Ranking
Problem
Reverse the ranking sequence in the previous example.
Solution
SELECT storeid
prodid
,sales
,RANK( ) OVER (ORDER BY sales DESC) AS "Ranking"
FROM salestbl
QUALIFY Ranking <= 3
ORDER BY 4 DESC;
Result
Things To Notice:
SELECT storeid
,prodid
,sales
,RANK( ) OVER (ORDER BY sales DESC) AS "Ranking"
FROM salestbl
QUALIFY Ranking <= 3
ORDER BY 3 ASC;
Result
Things To Notice:
The ORDER BY clause in the SELECT statement uses the sales column for
final ordering.
In the example seen next, the PARTITION BY clause adds the store id to the
scope, thus rankings will be based upon sales within a store. Each store will have
its top three ranking sales output.
Whereas the RANK( ) ORDER BY clause controls the default sort key, the
PARTITION BY clause adds another level of sort to the output.
Problem
Solution
SELECT storeid
,prodid
,sales
,RANK( ) OVER (PARTITION BY storeid
ORDER BY sales DESC) AS "Ranking"
FROM salestbl
QUALIFY Ranking <= 3;
Result
Things To Notice:
In the following example, an additional level of scope has been added via the
PARTITION BY clause. Our scope is now sales by product within store. Because
these three elements make up a ranking group and because the distinct
elements store and product only yield a single row, there are no rankings below
rank one. This is a case where the scope matches the granularity of the table,
thus no reasonable ranking can be produced.
Whereas the RANK( ) ORDER BY clause controls the default sort key, the
PARTITION BY clause adds another level of sort to the output.
Problem
Solution
SELECT storeid
,prodid
,sales
,RANK( ) OVER (PARTITION BY storeid, prodid
ORDER BY sales DESC) AS "Ranking"
FROM salestbl
QUALIFY Ranking <= 3;
Result
Things To Notice:
Note, the RANK function is covered in more detail in the Advanced SQL course.
The SAMPLE function is used to generate samples of data from a table or view.
This may be done in one of two ways. SAMPLE n - where n is an integer will yield
a sample of n rows, or if the number n is greater than the number of rows in the
table, the sample will consist of the number of rows in the table. Rows are not
reused within the same sample.
SAMPLE n - where n is a decimal value less than 1.00 and greater than .00
A percentage of a table.
An actual number of rows.
Problem
SELECT department_number
FROM employee
SAMPLE 10;
Result
department_number
-----------------
501
501
402
402
301
403
100
301
501
401
Things To Notice:
In the following example, 25% of the rows of the employee table are to be
returned. The employee table has 26 rows.
Example 2
Solution
SELECT employee_number
FROM employee
SAMPLE .25
ORDER BY 1;
Result
employee_number
---------------
1005
1006
1011
1014
1018
1022
1024
Things To Notice:
Using SAMPLEID
Problem
Get three samples from the department table, one with 25% of the rows,
another with 25% and a third with 50%.
SELECT department_number
,sampleid
FROM department
SAMPLE .25, .25, .50
ORDER BY sampleid;
Result
department_number SampleId
----------------- -----------
301 1
403 1
402 2
201 2
100 3
501 3
302 3
401 3
600 3
Things To Notice:
Note that all 9 of the 9 rows of the department table are returned.
This is due to the individual calculations.
9 *.25 = 2.25 = 2
9 *.25 = 2.25 = 2
9 *.50 = 4.50 = 5
-----
9
Example 2
Get three samples from the department table, one with 27% of the rows,
another with 35% and a third with 2%.
Solution
SELECT department_number
,SAMPLEID
FROM department
SAMPLE .27, .35, .02
ORDER BY SAMPLEID;
Result
department_number SampleId
----------------- -----------
402 1
403 1
100 2
302 2
401 2
Things To Notice:
Get three samples from the department table, one with 3 rows, another with 5
and a third with 8.
Solution
SELECT department_number
,sampleid
FROM department
SAMPLE 3, 5, 8
ORDER BY sampleid;
Result
department_number SampleId
----------------- -----------
501 1
402 1
403 1
100 2
302 2
301 2
401 2
201 2
600 3
Things To Notice:
Because the rows are not repeated to different sample sets, the supply of
rows is exhausted before the third set can be completed.
This results in a warning that there were not enough rows to populate all
samples as requested.
This warning is seen in the BTEQ environment, but not in ODBC.
The third sample gets the only remaining row.
Stratified Sampling
Solution
Result
Empno Salamt
----------- ------------
1014 24500.00
1023 26500.00
1009 31000.00
1005 31200.00
1022 32300.00
1025 34700.00
1004 36300.00
1012 37900.00
1024 43700.00
1010 46000.00
Things To Notice:
System Variables
The following system variables are available for use in creating SQL queries.
SESSION - contains the session-id of the requesting session/user.
DATABASE- contains the current default database of the requesting session/user.
USER - contains the user-id associated with the current session.
ACCOUNT - contains the user account info of the requesting session/user.
Problem
Return the current session-id, default database, user-id and account string of the
current user.
Solution
Result
The default database setting may change during the course of a session. These
built-in functions allow you to keep track of your current database.
Problem
Show the current database both before and after it has been changed.
Solutions
Database
------------------------------
PED
SELECT DATABASE; /* Show the new current database for this user
session*/
Database
------------------------------
cs_views
TOP N Overview
This improvement has been achieved by implementing the following new syntax
to the SQL lexicon:
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 ?
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:
Things to notice:
The same result could have been retuned by specifying TOP 6 without the WITH
TIES option.
SELECT TOP 6
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:
Add the WITH TIES option and note any difference in the result set.
Things to notice:
There is no difference.
WITH TIES has no effect because the top six rows are requested.
If a seventh row existed with the same amount as the sixth, it would be
returned.
Note, that the same result is returned if we change the count to TOP 5.
Things to notice:
The same result set is returned whether TOP 5 WITH TIES or TOP 6 is
specified.
In this case, there is a tie for the 5th row, hence both rows are returned.
If a seventh row existed with the same amount as the fifth and sixth, it
would also be returned.
Now, consider getting the rows at the bottom of an ordered list. This is
accomplished by using the ORDER BY clause.
SELECT TOP 3
employee_number
, salary_amount
FROM employee ORDER BY salary_amount ASC;
employee_number salary_amount
--------------- -------------
1014 24500.00
1013 24500.00
1001 25525.00
Things to notice:
ORDER BY ASC reverses the ranking sequence and shows the bottom
rankings.
Two rows with the same salary are treated as two rows of output.
What would you expect to happen if you add the WITH TIES option?
Things to notice:
SELECT TOP 3
employee_number
, salary_amount
FROM employee ORDER BY salary_amount ASC;
employee_number salary_amount
--------------- -------------
1013 24500.00
1014 24500.00
1026 25525.00
Now add the WITH TIES option. Do you expect it to make a difference this time?
Things to notice:
The TOP N function may also be used to return unordered rows. This is
accomplished by using TOP N without including an ORDER BY clause. In the
example seen here, two rows are requested. Since an unordered set cannot
produce the top n rows, the first n rows returned from the database are
selected. A second execution of the same query will produce the same two rows,
assuming that both the table and the configuration of the system have not
changed. This is because the database will return the rows in a consistent
sequence and always take the first (or top) n specified.
SELECT TOP 2
employee_number
, salary_amount
FROM employee;
employee_number salary_amount
--------------- -------------
1008 29250.00
1012 37900.00
Things to notice:
Add the WITH TIES option to the preceding query. What effect is seen?
employee_number salary_amount
--------------- -------------
1008 29250.00
1012 37900.00
Things to notice:
Any two rows may be returned by this query, since no ranking criteria is
defined.
Without an ORDER BY clause, the WITH TIES clause is ignored.
This is similar to using the SAMPLE function, however the SAMPLE
function produces a more truly randomized result set.
Previously, to return less than a full answer set, a user could use the .SET
RETLIMIT N option in BTEQ to control the number of rows returned to the client
side. Since the DBS must still generate all the result rows, the performance of
this approach could vary dramatically depending on the number of rows returned
by the SQL request. With the TOP N option, only the rows needed to satisfy the
query requirement are fetched by the DBS, thus making it a better choice when
a number of random rows are to be selected. The SAMPLE function continues to
be the preferred approach for truly random samples.
Things to notice:
Things to notice:
TOP n processing also permits the value of n to be passed into the operation by
means of a parameterized macro.
EXEC Top_Budgets(5);
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
501 308000.00
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course.
Two windows will pop-up: a BTEQ Instruction Screen and your
Telnet Window.
Sometimes the BTEQ Instructions get hidden behind the
Telnet Window.
You will need these instructions to log on to Teradata.
Case Expressions
Please Note: This is a narrated presentation. You may control the pacing of this
module by using the Pause button as well as other buttons available on the
screen duing the presentation.
Start Presentation
After viewing the presentation, return to this window and continue to the next
lab activity
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course.
Two windows will pop-up: a BTEQ Instruction Screen and your
Telnet Window.
Sometimes the BTEQ Instructions get hidden behind the
Telnet Window.
You will need these instructions to log on to Teradata.
Also list the count of rows that are going into each
averaging function. Sequence the report by
department. Limit the department number and count
columns to four digits each with zero suppress.
20.) Views
Objectives
Create a view.
Use a view to provide secure access to data.
Drop or modify a view.
List several reasons for using views.
What is a View?
A view functions like a 'window' into a table. It allows users to customize their
access to base tables by:
One can restrict columns from the base table(s) by explicitly listing the desired
column names from the base table(s) in the view definition.
One can restrict which rows may be accessed by using the WHERE clause in the
view definition. This will limit the rows returned from a SELECT statement
referencing the view.
Problem
Create a view of the employees in department 403 to use for both read and
update. Limit the view to an employee’s number, last name, and salary.
Solution
Result
Example
Create a join view of the employee and call_employee tables for call dispatch
use.
Using this view permits SELECT of information from both tables. Note that
column names which appear in both tables (i.e., employee_number) must be
qualified.
Problem
Solution
SELECT last_name
,first_name
,call_number
FROM employee_call
WHERE employee_number = 1002;
Result
Brown Alan 9
Renaming Columns
You may create a view that renames columns. This is often used to shorten long
column names, making queries easier to create.
Example
SELECT last
,sal
FROM shortcut
ORDER BY last DESC;
Result
last sal
------------ ----------
Short 38750.00
Morrissey 34700.00
UPDATE shortcut
SET sal = 100000.00
WHERE emp = 1019;
Replacing a View
You may replace a view in order to include or drop specfic columns or values.
Problem
Solution
Copy and paste the view definition to a text editor. Change the CREATE keyword
to REPLACE and make the desired changes.
The FORMAT clause may be used to set formats and the AS clause may be used
to assign new column names. For example, let's create a view for department
201 with shortened titles and appropriate formatting. Also, show the salary
amount as a monthly figure.
SELECT *
FROM report
ORDER BY monthly_salary;
Emp Dept Name Monthly_Salary
---- ---- ------------------ ------------------ --------------
1025 201 Short Michael $2,891.67
1021 201 Morrissey Jim $3,229.17
Aggregate Views
You may create a view that summarizes information by using aggregates. For
instance, let's create a view that summarizes salary information by department.
Note: Aggregate and derived columns must be assigned names in the view
definition.
Select the average salary for all departments using the view we created:
SELECT department
,salary_average
FROM deptsals;
department salary_average
100 100000.00
201 36725.00
403 38833.33
... ...
401 35082.14
The HAVING clause may be used within a view definition to restrict which groups
will participate in the view.
We may modify the deptsals view to include only departments with average
salaries of less than $36,000.
SELECT department
,salary_average
FROM deptsals;
department salary_average
------------- -----------------
401 35082.14
Since views are really virtual tables, they may be joined with other tables and/or
views in a query.
Problem
First, create a view showing the the aggregate salaries by department number.
Solution
SELECT *
FROM dept_salaries;
Results
deptnum salaries
----------- ------------
100 100000.00
201 73450.00
301 116400.00
302 56500.00
401 245575.00
402 77000.00
403 233000.00
501 200125.00
Problem
Solution
SELECT deptnum
,department_name
,salaries
FROM dept_salaries ds INNER JOIN department de
ON ds.deptnum = de.department_number
WHERE salaries > 100000
ORDER BY 1;
Result
Nested Aggregates
An aggregate result may be obtained from a view which has aggregation defined
in its creation. In the following example, an aggregated view is created.
SELECT *
FROM dept_sals
ORDER BY 1;
Result
dept SumSal
----- ---------
100 100000.00
201 73450.00
301 116400.00
302 56500.00
401 245575.00
402 77000.00
403 233000.00
501 201125.00
You may apply aggregate operators in your SELECT statement against the
aggregate view you just created. This represents aggregate functions being
applied to an aggregated view or what is referred to as 'nested aggregates'.
Result
Problem
Solution
SELECT dept
,Sumsal as Topsal
FROM dept_sals
WHERE topsal = (SELECT MAX(Sumsal)
FROM dept_sals);
Result
dept Topsal
---- ---------
401 245575.00
Things to notice:
The aggregated view is used in both the main query and the subquery
The subquery determines the maximum salary from the aggregate view.
The main query uses the maximum salary to capture the department
number.
In a view, the WITH CHECK OPTION controls the update and insert capability of
users with those privileges on the view. A user may not insert or update a row if
the resulting row will fall outside of the view. Said differently, an insert or update
will be denied if the resulting row will violate the constraints of the WHERE clause
in the view definition.
For example, let's recreate a view and add a WHERE clause and a WITH CHECK
OPTION:
With the WITH CHECK OPTION in place in a view, the following inserts and
updates are affected:
Users will not be able to update or insert rows whose values violate the
constraints of the WHERE clause in the view definition.
Example 1
Example 2
UPDATE dept_budget
SET budget = 2000000
WHERE dept = 401;
**** Failure 3564 Range constraint:
Check error in field dept.budget_amount.
Both operations above were disallowed because the WITH CHECK OPTION
prevents these changes.
Create at least one view for each base table to restrict user's access to
data they don't need to see.
Create query views as you need them for ad hoc situations.
Use access locks when creating views to maximize data availability to
users.
Lab
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
String Expressions
Several functions are available for working with character strings in SQL. Also,
the concatenation operator is provided for combining strings. The string
functions and the concatenation operator are listed here.
|| (Two vertical
Concatenates (combines) character strings together.
bars)
String Functions
All functions listed are part of the ANSI standard except for:
POSITION, UPPER and LOWER are covered in more detail in the Teradata SQL For
Application Development course.
The TRIM function was covered in a previous module of this course.
SUBSTRING Function
Examples
Result
'log'
SELECT SUBSTR ('catalog',5,3);
Result
'log'
Let's look at several more examples showing the results of a SELECT using either
SUBSTRING or SUBSTR . Equivalent results are returned in all cases.
SUBSTRING SUBSTR
Result Result
Problem
Display the first initial and last name of all employees in department 403. Sort
the result by last name.
Solution
Result
FI last_name
-- --------------------
A Brown
J Charles
P Hopkins
D Lombardo
L Ryan
A Villegas
The following gives examples of how integers are converted when a SUBSTRING is
performed on them.
no_of_
BYTEINT - (3 digits) 4 characters including sign
dependents
When an integer field is converted to CHAR, a sign character is added and the
value is right-aligned as follows:
+ 1 2 7 (no_of_dependents BYTEINT)
+ 0 0 2 1 3 (area_code SMALLINT)
+ 0 0 0 5 5 5 4 1 3 4 (phone INTEGER)
Problem
Produce a list of all 3-digit area codes from the location_phone table.
Solution
Result
AREA CODE
202
212
213
312
313
415
609
617
718
804
919
Problem
Find those customers in the location table whose nine-digit zip code ends with
four zeros.
Solution
SELECT customer_number
,zip_code (FORMAT '9(9)')
FROM location
WHERE SUBSTRING (zip_code FROM 8 FOR 4) = '0000'
;
Result
String Concatenation
Multiple strings may be combined using the string concatenation operator '||'.
Only character and byte data types may be concatenated. (Byte data types must
be concatenated with another byte data type.)
Result
'abcxyz'
Example
Result
'ab'
Example
Result
'ax'
Teradata Alternative
The Teradata database also supports the use of exclamation marks (!!) as the
concatenation operator. Existing applications using exclamation marks should be
re-written using vertical bars to meet ANSI standards.
Problem
Display the first and last names for employees in department 403.
Solution
Result
EMPLOYEE
Arnando Villegas
Loretta Ryan
Things to notice:
The concatenation operators are used to place a space between first and
last name.
Even though two columns are concatenated together, it is treated as a
single column of output.
There are no commas in this statement, since there is only one column of
output.
Problem
Display the first initial and last name of employees in department 403.
Solution
Result
EMPLOYEE
-----------------------
D. Lombardo
A. Villegas
P. Hopkins
L. Ryan
A. Brown
J. Charles
Problem
Produce the same list, but concatenate last name followed by first name
separated by a comma.
Solution
Result
EMPLOYEE
-------------------------------------
Lombardo , Domingus
Villegas , Arnando
Hopkins , Paulene
Ryan , Loretta
Brown , Allen
Charles , John
Things to notice:
Use the TRIM function to remove the extra spaces in your query result.
Problem
Drop the spaces after the last names prior to concatenating with the first name.
Solution
Result
EMPLOYEE
-------------------
Lombardo, Domingus
Villegas, Arnando
Hopkins, Paulene
Ryan, Loretta
Brown, Allen
Charles, John
The POSITION function finds the position within a string where a specified
substring begins. The ANSI standard POSITION function has a Teradata
equivalent function called INDEX. The INDEX function will continue to be
supported in future releases of the product, however the use of POSITION is
encouraged for future applications.
Both the POSITION and INDEX functions return a character position in a string.
Problem
Solution
SELECT department_name
,POSITION('SUPPORT' IN department_name)
FROM department
WHERE POSITION('SUPPORT' IN department_name) > 0
ORDER BY department_number;
Result
DeptName PositionNum
------------------------------ -----------
customer support 10
software support 10
Note: POSITION is discussed in more detail in the Teradata SQL For Application
Development course.
Problem
Display the first name and last names separated by a single space for each person
in the contact table.
Solution
Result
Contact Names
--------------------------
Jack Hughes
Nancy Dibble
James Leblanc
Ginny Smith
Alison Torres
Connie Brayman
Things to notice:
Try It! For this set of lab questions you will need information from
the Database Info document.
To start the online labs, click on the Telnet button in the lower
left hand screen of the course. Two windows will pop-up: a
BTEQ Instruction Screen and your Telnet Window.
J. --------------------
K. (xxx) xxx-xxxx xxx
L. List all contacts from the contact table, breaking the
first name out into its own column and the last name
into its own column. Include phone number. Select
only contacts in area code 408. Sort by first name.
Custom column titles for the name columns are: First
Name and Last Name.