Teradata Vantage SQL Basics
Teradata Vantage SQL Basics
Work with Teradata Studio Express for submitting queries to the Teradata Database
Write basic SELECT statements using the FROM, WHERE, and ORDER BY query clauses
Apply SQL predicates such as IN, BETWEEN, and LIKE for data retrieval
Add multiple search conditions using the logical operators NOT, AND and OR
Understand Teradata character, numeric, and datetime data types
Convert data from one type to another, both explicitly and implicitly
Apply basic character, numeric, and datetime functions such as SUBSTRING, STRPOS, ROUND,
FLOOR, EXTRACT and ADD_MONTHS
Describe what NULLs are and how to incorporate NULL syntax into queries correctly.
Extract important information from an Explain plan and understand how it’s influenced by statistics
Write SELECT statements using subqueries, inner and outer joins
Write SELECT statements using the EXISTS predicate
Write queries to produce totals and subtotals in reports using the GROUP BY and HAVING query
clauses and aggregation operators such as COUNT, SUM, and MAX
Write nested SELECT statements using Derived Tables and Common Table Expressions
Combine multiple SELECTs using the set operations UNION, EXCEPT, and INTERSECT
Write SELECT statements using the EXISTS predicate
Display values based upon conditions using the CASE expression and the NULLIF and COALESCE
functions
Return a sample as a number or a percentage of rows using the SAMPLE clause
Return a number or a percentage of rows using the TOP clause
Audiences
Power Users
Application Developers
Data Analysts
Database Administrators
Architects/Designers
One client tool to manage Teradata Vantage
Create, modify, and format SQL statements with visual builder, pre-built templates, and autocomplete
Teradata Studio is a client-based graphical interface used to perform Teradata database administration, external
data access, and query tasks.
Teradata Studio is built on the Eclipse Rich Client Platform (RCP) which takes advantage of the RCP framework for
building and deploying native GUI applications. It extends the Eclipse Data Tools Platform (DTP) to provide
enhancements for accessing objects and data stored in Teradata-supported databases. Teradata Plug-in for Eclipse,
in addition to the features provided in Teradata Studio, plugs into your existing Eclipse IDE and provides wizards to
help users create Java database objects such as Java Stored Procedures and Java User Defined Functions. Teradata
Plug-in for Eclipse is a tool for developers creating Java applications and database objects.
Windows
Mac OSX
Data Management
–
Table Editor to easily add, delete, or update table rows
Data Wizard to extract or load to and from existing tables
Smart Loader for flat files to Teradata Database (including Data Lab) and
Hadoop
Ad-hoc Reporting
–
SQL Editor to create and modify SQL statements with syntax checking and
formatting
SQL Query Builder to visually build and edit SQL queries
Execute individual or multiple SQL Statements
Run Stored Procedures, Macros, and User-defined Functions
Result Set Viewer with the ability to save, export, or print the results
History Viewer to track the submitted SQL queries and preview results status
Teradata Studio
Teradata Studio, in addition to the features provided in Teradata Studio Express,
includes administration dialogs and wizards. It is mainly used by administrators to
create and administer database objects. Teradata Studio is a tool for DBAs, Data
Scientists, and Power Users. Teradata Studio also provides a client-based graphical
interface for performing SQL, and database administrative/management tasks on
Vantage Advanced SQL Engines, Teradata Aster Databases, and Hadoop systems.
Equivalent to Teradata Assistant and Teradata Administrator with added
features and new UI.
Additional Features in Teradata Studio
Teradata Administration
–
Teradata database objects management
Security commands to grant or revoke access and system rights
Administer Roles and Profiles
Manage Privileges on database objects
Objects dependency browser
Compare database objects
Move Space between Teradata databases
Teradata statistics collections management
Secure Zones administration
Data Management
–
Smart Loader for Teradata Database and Hadoop
Bi-directional data movement from Teradata/Teradata and Teradata/Hadoop
Modular Display
The window helps you to visualize the Teradata Studio homepage and understand
each component, such as Menu and Toolbar, Data Source Explorer, SQL Editor, etc.
Creating Connections
1. Defining a Connection.
2. To begin using Teradata Studio, create a Connection Profile for your
database.
3. Connection to any database must be made through a JDBC (Java-
based data access technology) connection. The Connection Profile
details the JDBC information required to connect to an instance of a
Vantage Advanced SQL Engine.
4. When connecting to the Vantage Advanced SQL Engine, determine
which mode you will be using. Studio defaults to ANSI mode rather
than Teradata mode. Therefore, you may need to change it in the data
connection profile.
5. Use the Test Connection option to verify the connection with the
username and password. If you choose to save the password, do this
after testing the connection.
6. Connecting to a data source is the equivalent of “logging on” with
Teradata Studio. You may choose from any previously defined data
connection profile. Just right mouse click on the defined data
connection and select “Connect”.
objects and to determine what folders are shown in the hierarchical display.
SQL Editor
The SQL Editor provides an integrated environment to develop SQL statements for execution. You can validate SQL syntax, save
files to a project or export to system files, execute all SQL in a file, or execute the SQL as individual statements. You must specify
a Connection Profile before you can execute an SQL query statement.
The SQL Editor displays when you open an SQL/DDL file or when you select the Query Development perspective. Double-click an
SQL/DDL file in Project Explorer to open that file.
The session connected by the Data Source Explorer Tree is not reused in the editor. Each SQL editor window connects to a new session.
Connection Monitor
Information about the number of sessions connected by Studio can be shown in the Connection Monitor:
Window > Show View > Other > Teradata > Connection Monitor
You can click and drag object(s) from the Database Explorer tree to the Query
window.
You can drag objects from the Data Source Explorer and drop them into the SQL Editor to build
your SQL statement. The object name appears in the SQL Editor at the location where you drop it.
You can drag object names from the Database Explorer tree to the Query pane. Click and drag the
object from the Explorer tree to the Query pane. The name of the object appears in the Query
window. Use the Ctrl key to add a comma after the object name when it is dragged to the Query
Window. Dragging Multiple Objects
You can use the Shift and Ctrl keys on the keyboard to select more than one
object from the Database Explorer Tree that can be dragged to the Query
window.
Rectangular block selection or column mode selection allows you to select the text vertically within
the text. To do so, you can click the SQL Editor and toggle between column mode selection and
Execute
Execute all Execute
the highlighted st Execute the statement
statements the highlighted st
atement(s) in the Execute all statements between the previous
in the editor atement(s) in the
editor as in the editor as a single and the following
as a single editor as a single
individual request (sequentially) semicolon based on the
request (in request (in
requests cursor position
parallel) parallel)
(sequentially)
Execute current text and submits the SQL at the current cursor position, similar to
"highlight all text between the previous and the next semicolon and submit it". To set the
preferences, go to:
Preferences > Data Management > SQL Development > SQL Editor and select
your choices.
Project Explorer
The Project Explorer provides a hierarchical view of the Projects and their resource
files. You can select SQL files for editing or create new SQL files.
SQL History
The SQL History form provides an enhanced display of the history information. As you execute
SQL statements from the SQL Editor or run functions from the Data Source Explorer, an entry is
added to the SQL History list. The history displays extensive information about each SQL
execution, including execution elapsed time, DBMS time, fetch time, database server, row count,
parameter display for macros and stored procedures, as well as displaying the SQL statement and
SQL statement type. The SQL History form displays the data in a grid format that lets you select
rows or cells, and you can copy cells or rows, sort columns, delete rows, edit the note or SQL for a
history entry, filter columns, search for result history data, format the display, and re-execute SQL
statements. You can add notes manually by clicking on the Note column value. You can also
choose to be prompted to add notes when you execute the SQL. You can sort columns in
ascending and descending order with the exception of the Parameters column. Rows can be
selected by clicking in the numbered row header. Column order, collapsed columns, filter, sort,
and formatting are preserved from session to session. If a filter is in effect, the result of a newly
executed SQL statement is inserted as the top row, regardless of whether it meets the filter
criterion. If a sort is in effect the new entry is inserted in the sorted order. The Result column
contains a summary of the executed SQL operation. To read the entire contents in a column cell,
hover the mouse pointer over the cell. Error results show as red text in the resulting tool tip
display.
Comment
You can embed Standard SQL compliant comments within an SQL request.
The SQL Parser recognizes the following types of ANSI/ISO SQL:2016-compliant embedded
comments:
The SHOW statements return the source code (DDL) of the named objects.
These statements are particularly useful for application developers who need to
develop exact replicas of existing tables, views, triggers, hash and join indexes, stored
procedures, user-defined functions, or macros for purposes of testing new software.
Some SHOW statements optionally return database object definition SQL text in XML
format.
SHOW SELECT
Displays the DDL for all database objects referenced by a specified DML request.
You must have one of the following privileges to execute a SHOW or SHOW IN XML
request:
The privileges necessary to perform the specified DML request and any right on
each database object
The SELECT privilege on the dbc.tvm system table
The SHOW report lists the DML_statement DDL definitions in order of dependencies, first
listing the definitions of independent objects and then listing dependent objects.
1. Base tables
2. Referenced (parent) tables of base tables
3. Error tables
4. Views
5. Macros
6. Triggers
7. Join and hash indexes
8. UDTs
DELETE
INSERT
INSERT … SELECT
MERGE
SELECT
UPDATE
You can access SQL Editor with Outline View by navigating through the following path:
Window > Show View > Other > General > Outline
Click and drag the Outline view up to the right-hand side of the SQL Editor.
As you select SELECT Statements from the Outline view, the SQL statement is
highlighted in the SQL Editor window.
Additional Studio Preferences
Summary
LAB 1
Create a database connection for the training server based on the credentials
you received
o Don't forget to remove TMODE = ANSI
Modify Studio preferences:
o Show a flat list instead of a hierarchy of databases in the Explorer Tree
o Enable Execute Current Text
o Any additional option you consider appropriate
Locate the finance_payroll.hr_payroll table and check the info returned in the
Explorer Tree
Run HELP TABLE and SHOW TABLE for this table
Open a SQL Editor window and execute a
SELECT * FROM finance_payroll.hr_payroll;
Click the Export Results to File icon and rerun the Select, saving the result to a
CSV file.
Lesson 4 of 22
Procedural languages contain looping constructs and IF/ELSE logic that interpretive
languages don’t contain. Such constructs do, however, exist in Stored Procedures. Stored
Procedures are SQL based.
SQL Classifications: SQL statements are commonly divided into three categories:
Data Definition Language (DDL): Used to define and create database objects such as
tables, views, macros, databases, and users.
CREATE Define a table, view, macro, index, trigger, or stored procedure.
DROP Remove a table, view, macro, index, trigger, or stored procedure.
ALTER Change table structure or protection definition.
Data Manipulation Language (DML): Used to work with the data, including such tasks as
inserting data rows into a table, updating an existing row, or performing queries on the
data. The focal point of this course will be on SQL statements in this category.
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.
Data Control Language (DCL): Used for administrative tasks such as granting and
revoking privileges to database objects or controlling ownership of those objects.
GRANT Give user privileges on database objects.
REVOKE Remove user privileges on database objects.
GIVE Transfer database ownership.
Naming rules
The following rules apply to SQL:
1.
3.
4.
Enclosing an object name in double quotes (") enables the use of additional
characters and reserved keywords: CREATE TABLE "my
table?"("column" int); which should be avoided
5.
NOT=
< LT Tests for less than
<= LE Tests for less than or equal
> GT Tests for greater than
Tests for greater than or
>= GE
equal
Character String Comparison
Comparing two character strings of unequal length:
The shorter of the two is padded on the right with spaces before the comparison
occurs ????
Effectively ignoring trailing spaces: 'a' = 'a '
Case Sensitivity
Is 'a' = 'A'?
To answer this question, you should understand that:
Each character column is defined as either CASESPECIFIC or NOT
CASESPECIFIC
If either operand in a comparison is case specific then the comparison is case
sensitive
Teradata SQL allows two different modes of session operation. Let's understand this with
an example.
1. In ANSI mode WHERE last_name = 'brown' will return no row for 'Brown'.
2.
CASESPECIFIC attribute can be added to an operand to override the default, e.g.
WHERE last_name = 'brown' (NOT CASESPECIFIC) (Overwrite case
sensitive literal in ANSI mode)
WHERE last_name (CASESPECIFIC) = 'brown' (Overwrite case sensitive
column in Teradata mode)
ORDER BY Clause
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.
The ORDER BY clause specifies the column(s) for sorting the result set.
Requires more spool & CPU usage ⇒ don't order by too many columns.
o ORDER BY quantity * price;
o ORDER BY columns are duplicated in spool
o Varchar columns are expanded to the defined maximum size
The same logic is applied when two strings are compared using less/greater.
SET SESSION COLLATION changes the collation sequence for the current session.
This affects how the collation sequences collate and compare data.
All users share the same collation to get the same results.
Aliasing a Column
Naming Columns and Expressions
Each column in a SELECT result has a name that is derived from the list of specified
column names (expressions) that generated the data. The name is typically the
column name from which the data came.
An alias can be used for any column when a pseudonym is more descriptive or easier
to use.
Alias names must meet the same requirements as names of other database objects.
You can rename, or alias, a projected column for the current select.
Teradata SQL allows for naming derived columns and renaming existing columns. For
every expression that is given a name, that name is entered in a NAMED list that
associates the expression with the name. This is true for both forms of naming
columns, NAMED and AS.
This temporary name can be referenced elsewhere in the request. But it should not be
the same as another column name of any table used in a query. If the parser finds an
unqualified column name it searches the column lists of all tables named in the SQL
statement.
Only one row is to be returned from any set of duplicates that might result from a
given select list. Rows are duplicates only if each value in one is equal to the
Depending on the optimizer plan DISTINCT might return a sorted result set, ordered
over all columns. As the plan might change it is recommended to add an explicit
To Summarize:
1. 1
2. 2
3. 3
The asterisk (*) indicates that we wish to see all of the columns in the table.
The default database is not the only database used to find an unqualified name in an
SQL statement. The parser also looks for the name in:
If the unqualified object name exists in more than one of these databases,
the SQL statement produces a 3806 Table/view/trigger name xxx' is an
ambiguous error.
If the unqualified object is a function, the parser also looks for the name in:
A session always has a default database, initially set during login using
o Connection properties (JDBC, ODBC, etc.)
o Default database option of the user's profile
o Default database option of the user
o If none is defined the user name is used
Can be changed for the current session by using
DATABASE new_default_database;
Used by the parser to look for unqualified names, such
as table, view, or macro names, in SQL statements
SELECT *
FROM employee_sales.employee;
Example 1
DATABASE employee_sales;
SELECT *
FROM employee;
Example 2
Summary