Documente Academic
Documente Profesional
Documente Cultură
Introduction
After completing this module, you will be able to: Define the role of SQL in accessing a Relational Database. Retrieve data from a relational table using the basic SELECT statement.
A complete data access and maintenance language Designed for Relational Database Management System (RDBMS) An industry standard for relational databases A non-procedural language Three levels of ANSI compliant SQL:
Entry
Intermediate Full
Teradata SQL is compliant with entry-level ANSI SQL. Teradata extensions to ANSI compliant SQL add value. Some capabilities are available using either ANSI compliant syntax or Teradata syntax. Most intermediate and full ANSI standards are not implemented in Teradata V2R2.0.
ANSI SQL Teradata SQL SQL AS
intermed. full entry
Rename a column
NAMED CM
Create a macro
Select the set of all employees in the employee table who are in department 401.
Select the set of all employees in the employee table who earn more than $35,000.
Select the set of all employees in the employee table who are in department 401and who earn more than $35,000.
SQL Commands
Table A Table with A Rows and Columns With Rows and Columns
EMPLOYEE ( PARTIAL ) LAST NAME FIRST NAME HIRE DATE BIRTH SALARY DATE AMOUNT
MANAGER EMPLOYEE EMPLOYEE DEPARTMENT JOB CODE NUMBER NUMBER NUMBER PK 1006 1008 1005 1004 1007 1003 FK 1019 1019 0801 1003 1005 0801 FK 301 301 403 401 403 401 FK 312101 312102 431100 412101 432101 411100
Column names have been folded and capitalized for visibility. EMPLOYEE NUMBER HIRE DATE Exception: BIRTH DATE is birthdate is is employee_number. hire_date
NOTE: Syntax and result examples shown throughout this text relate to the (partial) sample table shown with them. On pages where no sample table is shown, results relate to the entire table as shown in Appendix B.
Table Relationships
What is the department name for employee 1004? Who is the manager of that employee? What happens if manager 1003 quits?
EMPLOYEE MANAGER EMPLOYEE EMPLOYEE DEPARTMENT JOB CODE NUMBER NUMBER NUMBER PK 1006 1008 1005 1004 1007 1003 FK 1019 1019 0801 1003 1005 0801 FK 301 301 403 401 403 401 FK 312101 312102 431100 412101 432101 411100 Stein Kanieski Ryan Johnson Villegas Trader John Carol Loretta Darlene Arnando James 761015 770201 761015 761015 770102 760731 531015 580517 550910 460423 370131 470619 2945000 2925000 3120000 3630000 4970000 3785000 LAST NAME FIRST NAME HIRE DATE BIRTH SALARY DATE AMOUNT
Table Relationships
DEPARTMENT DEPARTMENT DEPARTMENT NAME NUMBER PK 501 301 302 403 402 401 201 marketing sales research and development product planning education software support customer support technical operations 80050000 46560000 22600000 93200000 30800000 98230000 29380000 MANAGER BUDGET EMPLOYEE AMOUNT NUMBER FK 1017 1019 1016 1005 1011 1003 1025
Obtain the employee numbers for all the employees in the Employee table of the Customer Service Database.
SQL Query SELECT FROM ; employee_number employee
EMPLOYEE
EMPLOYEE NUMBER PK 1006 1008 1005 1004 1007 1003 MANAGER EMPLOYEE NUMBER FK 1019 1019 0801 1003 1005 0801 DEPARTMENT NUMBER FK 301 301 403 401 403 401 JOB CODE FK 312101 312102 431100 412101 432101 411100 LAST NAME FIRST NAME HIRE DATE BIRTH DATE SALARY AMOUNT
Obtain all columns of information for all the employees in Employee table of the Customer Service Database.
SELECT FROM ; * employee
employee_number manager_employee_number 1008 1019 1006 1019 1005 801 1004 1003 1007 1005 1003 801
birthdate salary_amount 58/05/17 29250.00 53/10/15 29450.00 55/09/10 31200.00 46/04/23 36300.00 37/01/31 49700.00 47/06/19 37850.00
EMPLOYEE
EMPLOYEE NUMBER PK 1006 1008 1005 1004 1007 1003 MANAGER EMPLOYEE NUMBER FK 1019 1019 0801 1003 1005 0801 DEPARTMENT NUMBER FK 301 301 403 401 403 401 JOB CODE FK 312101 312102 431100 412101 432101 411100 LAST NAME FIRST NAME HIRE DATE BIRTH DATE SALARY AMOUNT
WHERE Clause
The WHERE clause is used to qualify the rows returned by a SELECT statement. Example:
SELECT employee_number ,hire_date ,last_name ,first_name employee department_number = 401
FROM WHERE ;
ORDER BY Clause
The ORDER BY clause specifies the column(s) to be used for sorting the result.
SELECT employee_number ,last_name ,first_name ,hire_date FROM employee WHERE department_number = 401 ORDER BY hire_date ; employee_number last_name first_name hire_date 1003 Trader James 76/07/31 1004 Johnson Darlene 76/10/15
First column specified is major sort column. Second and subsequent columns are minor sort columns.
SELECT employee_number ,department_number ,job_code FROM employee WHERE department_number < 302 ORDER BY department_number ,job_code ; employee_number 801 1025 1021 1019 1006 1008 department_number 100 201 201 301 301 301 job_code 111100 211100 222100 311100 312100 312100
DISTINCT Option
DISTINCT department_number
,job_code employee department_number = 501
Naming rules: Database names and user names must be unique within the Teradata RDBMS. TABLE, VIEW, and MACRO names must be unique within a database/user. Column names must be unique within a table. The syntax for qualifying a name is: [ [ databasename. ] tablename. ] columnname Example:
NAME EMPLOYEE.NAME PAYROLL.EMPLOYEE.NAME (unqualified) (partially qualified) (fully qualified)
PAYROLL EMPLOYEE
NUM NAME SALARY
PERSONNEL DEPARTMENT
NUM NAME BUDGET
MFG EMPLOYEE
NUM NAME SALARY
DEPARTMENT
NUM NAME BUDGET
Use Teradata extensions to improve readability beyond the ANSI rules: Names are composed of:
ANSI A-Z 0-9 _ (underscore) Teradata extensions a-z # $
Do this:
SELECT last_name ,first_name ,hire_date ,salary_amount FROM employee WHERE department_number = 401 ORDER BY last_name ;
Not this:
select last_name, first_name, hire_date salary_amount from employee where department_number = 401 order by last_name;
FIND THE ERROR IN THIS REQUEST! TRY TO ADD FORMATTING TO THE HIRE DATE!
Course materials use Teradata Extensions for object names in order to improve readability: Lower and mixed case Up to 30 characters
Default Database
A database may contain: Tablescontain data. Viewsare previously defined windows to the data. Macrosare stored SQL statements. The SQL Parser looks in your default database for the tables, views, or macros you refer to in your SQL statement. Example: .logon johnc; Password: xyz The normal default database isjohnc. Your user profile may be modified to choose a different default database.
Once logged on, you change the default database by using the DATABASE command. Example:
DATABASE payroll;
The default database is payroll until: User logs off A new Database command is issued
You can override a default DATABASE in a single SQL statement by specifying the database you wish to query: Example:
SELECT FROM * payroll.employee
LOCATION
I I DA
N Y N
I I CF CV CF I CF CF
Y N N N N N Y Y
manager_employee_number I
EMPLOYEE employee_number I Y Y Y Y N N N N N -9(10) -9(10) -9(10) -9(10) x(20) X(30) YY/MM/DD YY/MM/DD -9(6).99
employee_number
LOCATION_PHONE location_number area_code phone extension description comment_line I I2 I I CV CV Y N N Y N Y -9(10) -9(5) -9(10) -9(10) X(40) X(32000)
KEY
EMPLOYEE_PHONE employee_number area_code phone extension comment_line I I2 I I CF N N N Y Y -9(10) -9(5) -9(10) -9(10) X(72)
I I2 D
Lab 1
The exercises in Lab 1 are to be done as a pencil and paper workshop. Write your SQL statements as if you were going to enter them on-line. Lab1_1 Select all columns for all departments from the department table.
Lab 1_2 Request a report of employee last and first names and salary for all of manager 1019's employees. Order the report in last name ascending sequence.
Lab 1_3 Modify the request in Lab1_2 to show department number instead of first name and make it for manager 801's employees instead.
Note: Save Lab 1, as you enter actually enter and run these exercises, using BTEQ scripts, for Lab 2.
Refer to Appendix B for information on the tables and columns used in all of the lab exercises in this book.
Lab 1Optional
Lab 1_4 (Optional) Prepare a report of the department numbers and the manager's employee numbers for everyone in the employee table. Now add the DISTINCT option to the same report.