Datenquellen &
Datenaufbereitung
01: SQL 101
1
Dieter
© 2016 Schwarenthorer, Felix Pichler-Rossbacher
Machine Setup
• Host: gpsdb02.eastus.cloudapp.azure.com
• Username: student_Matrikelnummer
• Password: pass_Matrikelnummer
• Make sure you are in Development Mode
• You should be able to open under Databases
– Sys_DBA -> FHWN
• Each student will have a Database
– With a Staging Area and a Core Area (DB)
2 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
4 JOINS & Set
Operators
Next Steps (open
Discussion)
3 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
4 JOINS & Set
Operators
Next Steps (open
Discussion)
4 © 2016
RDBMS vs Database
A Relational Database A Database is a collection of
Management System (RDBMS) logically related data.
is software that allows users to • Databases are accessed by many
interact with databases. users for different purposes.
• Structured Query Language (SQL) is • For example, a sales database
used to interact with the data contains information about sales
stored in tables. which is stored in many tables.
5 © 2016
Tables
• Tables are the basic unit of data storage in a RDBMS
• A table is a collection of rows and columns
• The following example is a table storing data about employees:
EmployeeNo FirstName LastName BirthDate
101 Mike James 1/5/1980
104 Alex Stuart 11/6/1984
102 Robert Williams 3/5/1983
105 Robert James 12/1/1984
103 Peter Paul 4/1/1983
6 © 2016
Column
• A column contains data of the same type (character, numeric, date)
• For example, the column BirthDate in the Employee table contains
birth_date information for all employees.
EmployeeNo FirstName LastName BirthDate
101 Mike James 1/5/1980
104 Alex Stuart 11/6/1984
102 Robert Williams 3/5/1983
105 Robert James 12/1/1984
103 Peter Paul 4/1/1983
7 © 2016
Row
• A Row is one instance of all the columns
• For example, in the employee table one row contains information
about a single employee.
EmployeeNo FirstName LastName BirthDate
101 Mike James 1/5/1980
104 Alex Stuart 11/6/1984
102 Robert Williams 3/5/1983
105 Robert James 12/1/1984
103 Peter Paul 4/1/1983
8 © 2016
Primary Key (PK)
• The Primary Key (PK) is used to uniquely identify each row in a table
• No duplicate values are allowed in a primary key column
• The PK column cannot accept NULL values. It is a mandatory field
EmployeeNo FirstName LastName BirthDate DepartNo
101 Mike James 1/5/1980 1
104 Alex Stuart 11/6/1984 2
102 Robert Williams 3/5/1983 2
105 Robert James 12/1/1984 3
103 Peter Paul 4/1/1983 3
9 © 2016
Foreign Key (FK)
• Foreign keys (FK) are used to build relationships between the tables
• A foreign key in a child table is defined as the primary key in the parent
table
• A table can have more than one foreign key. It can accept duplicate
values and also NULL values. Foreign keys are optional
EmployeeNo FirstName LastName BirthDate DepartNo
101 Mike James 1/5/1980 1
104 Alex Stuart 11/6/1984 1
102 Robert Williams 3/5/1983 2
105 Robert James 12/1/1984 2
103 Peter Paul 4/1/1983 2
DepartNo Name
1 Sales
2 Education
10 © 2016
Data Types
• Each column in a table is associated with a data type. Data types
specify what kind of values will be stored in the column. Teradata
supports several data types. Here are some of the frequently used data
types.
Data Types Length (Bytes) Range of values
BYTEINT 1 -128 to +127
SMALLINT 2 -32768 to +32767
INTEGER 4 -2,147,483,648 to +2,147,483,647
-9,233,372,036,854,775,80 8 to
BIGINT 8
+9,233,372,036,854,775,8 07
DECIMAL 1-16
NUMERIC 1-16
FLOAT 8 IEEE format
CHAR Fixed Format 1-64,000
VARCHAR Variable 1-64,000
DATE 4 YYYYYMMDD
HHMMSS.nnnnnn or
TIME 6 or 8
HHMMSS.nnnnnn+HHMM
YYMMDDHHMMSS.nnnnnn or
TIMESTAMP 10 or 12
YYMMDDHHMMSS.nnnnnn +HHMM
11 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
4 JOINS & Set
Operators
Next Steps (open
Discussion)
12 © 2016
Data Definition Language (DDL)
Table Commands & Description
The CREATE TABLE command is used to create tables
The ALTER TABLE command is used to add or drop columns from an
existing table
The DROP TABLE command is used to drop a table
13 © 2016
CREATE TABLE
The CREATE TABLE command is used to create tables
Syntax
CREATE TABLE EMPLOYEE (
EmployeeNo INTEGER,
FirstName VARCHAR(30),
LastName VARCHAR(30),
DOB DATE FORMAT 'YYYY-MM-DD',
JoinedDate DATE FORMAT 'YYYY-MM-DD',
DepartmentNo BYTEINT
)
UNIQUE PRIMARY INDEX ( EmployeeNo );
Column Definition − Specifies the list of columns, data types and their
attributes.
Index Definition − Additional indexing options such as Primary Index,
Secondary Index and Partitioned Primary Index.
14 © 2016
SHOW TABLE
• Once the table is created, use the SHOW TABLE command to view the
table definition:
SHOW TABLE Employee;
*** Text of DDL statement returned.
*** Total elapsed time was 1 second.
------------------------------------------------------------------------
CREATE SET TABLE EMPLOYEE ,FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO (
EmployeeNo INTEGER,
FirstName VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
LastName VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
DOB DATE FORMAT 'YYYY-MM-DD',
JoinedDate DATE FORMAT 'YYYY-MM-DD',
DepartmentNo BYTEINT
)
UNIQUE PRIMARY INDEX ( EmployeeNo );
15 © 2016
ALTER TABLE
The ALTER TABLE command is used to add or remove columns from an
existing table
You can also use ALTER TABLE command to modify the attributes of the
existing columns
Syntax
ALTER TABLE <tablename>
ADD <columnname> <column attributes>
DROP <columnname>;
Example
ALTER TABLE employee
ADD BirthDate DATE FORMAT 'YYYY-MM-DD',
DROP DOB;
16 © 2016
DROP TABLE
The DROP TABLE command is used to drop a table
When a DROP TABLE command is issued, data in the table is deleted and
the table is dropped
Syntax
DROP TABLE <tablename> ;
Example
DROP TABLE employee;
17 © 2016
OTHER DB Objects
• Database
• View
• Stored Procedure
• Macro
• Triggers
• Users
• ...
18 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
4 JOINS & Set
Operators
Next Steps (open
Discussion)
19 © 2016
Data Manipulation Language (DML)
• This chapter introduces the SQL commands used to manipulate the
data stored in Teradata tables
• INSERT
• SELECT
• UPDATE
• DELETE
20 © 2016
INSERT
The INSERT INTO statement is used to insert records into a table
Syntax
INSERT INTO <tablename> (column1, column2, column3,…)
VALUES (value1, value2, value3 …);
Example
INSERT INTO Employee (
EmployeeNo
,FirstName
,LastName
,BirthDate
,JoinedDate
,DepartmentNo
) VALUES (
101
,'Mike’
,'James’
,'1980-01-05’
,'2005-03-27’
,01
21
);
© 2016
INSERT SELECT
The INSERT SELECT statement is used to insert records from another table
Syntax
INSERT INTO <tablename> (column1, column2, column3,…)
SELECT column1, column2, column3… FROM <source table>;
Example
INSERT INTO Employee_Bkup (
EmployeeNo
,FirstName
,LastName
,BirthDate
,JoinedDate
,DepartmentNo
)
SELECT
EmployeeNo
,FirstName
,LastName
,BirthDate
,JoinedDate
,DepartmentNo
22 ©FROM
2016 Employee;
INSERT SELECT
• Rules
– The number of columns specified in the VALUES list should match with the
columns specified in the INSERT INTO clause.
– Values are mandatory for NOT NULL columns.
– If no values are specified, then NULL is inserted for nullable fields.
– The data types of columns specified in the VALUES clause should be
compatible with the data types of columns in the INSERT clause.
23 © 2016
UPDATE
The UPDATE statement is used to update records in a table
Syntax
UPDATE <tablename>
SET <columnnamme> = <new value>
[WHERE condition];
Example
UPDATE Employee
SET DepartmentNo = 03
WHERE EmployeeNo = 101;
• Rules
– You can update one or more values of the table.
– If a WHERE condition is not specified then all rows of the table are affected
– You can update a table with the values from another table.
24 © 2016
DELETE
The DELETE FROM statement is used to remove records from a table
Syntax
DELETE FROM <tablename>
[WHERE condition];
Example
DELETE FROM Employee
WHERE EmployeeNo = 101;
• Rules
– You can delete one or more records of the table
– If a WHERE condition is not specified then all rows of the table are deleted
25 © 2016
SELECT
The SELECT statement is used to retrieve records from a table
Syntax
SELECT column 1, column 2
FROM <tablename>
EmployeeNo FirstName LastName BirthDate
101 Mike James 1/5/1980
104 Alex Stuart 11/6/1984
102 Robert Williams 3/5/1983
105 Robert James 12/1/1984
103 Peter Paul 4/1/1983
26 © 2016
SELECT contd.
Example
SELECT EmployeeNo,FirstName,LastName
FROM Employee;
EmployeeNo FirstName LastName
101 Mike James
104 Alex Stuart
102 Robert Williams
105 Robert James
103 Peter Paul
If you want to fetch all the columns from a table, you can use the
following syntax instead of explicitly listing all columns.
SELECT *
FROM Employee;
27 © 2016
WHERE Clause
The WHERE clause is used to filter records returned by the SELECT
statement. A condition is associated with the WHERE clause. Only the
records that satisfy the condition in the WHERE clause are returned.
Syntax
SELECT * FROM <tablename>
WHERE [condition]
Example
SELECT * FROM employee
WHERE EmployeeNo = 101;
EmployeeNo FirstName LastName BirthDate
101 Mike James 1/5/1980
28 © 2016
Logical Operators
SYNTAX Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
BETWEEN If values within range
IN If values in <expression>
NOT IN If values not in <expression>
IS NULL If value is NULL
IS NOT NULL If value is NOT NULL
Combine multiple conditions. Evaluates to true only if all
AND
conditions are met
Combine multiple conditions. Evaluates to true only if either of the
OR
conditions is met.
NOT Reverses the meaning of the condition
29 © 2016
BETWEEN & IN
• The BETWEEN command is used to check if a value is within a range of
values
SELECT EmployeeNo, FirstName
FROM Employee
WHERE EmployeeNo BETWEEN 101 AND 103;
• The IN command is used to check the value against a given list of
values
SELECT EmployeeNo, FirstName
FROM Employee
WHERE EmployeeNo IN (101, 102, 103);
30 © 2016
ORDER BY
When the SELECT statement is executed, the rows are not returned in any
specific order. The ORDER BY clause is used to sort records in
ascending/descending order, based on any columns
Syntax
SELECT * FROM tablename
ORDER BY column 1, column 2;
Example
SELECT * FROM employee
ORDER BY Firstname;
EmployeeNo FirstName LastName BirthDate
104 Alex Stuart 11/6/1984
101 Mike James 1/5/1980
103 Peter Paul 4/1/1983
102 Robert Williams 3/5/1983
31
105
© 2016 Robert James 12/1/1984
GROUP BY
The GROUP BY clause is used with a SELECT statement to arrange similar
records into groups.
Syntax
SELECT column 1, column 2 FROM tablename
GROUP BY column 1, column 2;
Example
SELECT FirstName, Count(*) FROM employee
GROUP BY Firstname;
FirstName Count(*)
Alex 1
Mike 1
Peter 1
Robert 2
32 © 2016
AGGREGATE FUNCTIONS
• Every RDBMS supports common aggregate functions. These can be
used with the SELECT statement.
– COUNT − Counts the rows
– SUM − Sums up the values of the specified column(s)
– MAX − Returns the large value of the specified column
– MIN − Returns the minimum value of the specified column
– AVG − Returns the average value of the specified column
EmployeeNo Gross Deduction NetPay
101 40,000 4,000 36,000
104 75,000 5,000 70,000
102 80,000 6,000 74,000
105 70,000 4,000 66,000
103 90,000 7,000 83,000
33 © 2016
AGGREGATE FUNCTIONS
• COUNT
SELECT count(*)
FROM Salary
• MAX / MIN / AVG/ SUM
SELECT max(netpay), min(netpay), avg(netpay), sum(netpay)
FROM Salary
34 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
4 JOINS & Set
Operators
Next Steps (open
Discussion)
35 © 2016
JOINS
• A Join is used to combine records from more than one table
• Tables are joined based on the common columns/values in each table
• Different types of Join are available
– Inner Join
– Left Outer Join
– Right Outer Join
– Full Outer Join
– Self Join
– Cross Join
– Cartesian Product Join
36 © 2016
37 © 2016
JOINS
Employee JoinedDat Departme
FirstName LastName BirthDate
No e ntNo
101 Mike James 3/27/2005 1 1/5/1980
102 Robert Williams 4/25/2007 2 3/5/1983
103 Peter Paul 3/21/2007 2 4/1/1983
104 Alex Stuart 2/1/2008 2 11/6/1984
105 Robert James 1/4/2008 3 12/1/1984
EmployeeNo Gross Deduction NetPay
101 40,000 4,000 36,000
102 80,000 6,000 74,000
103 90,000 7,000 83,000
104 75,000 5,000 70,000
38 © 2016
INNER JOIN
• Inner Join combines records from multiple tables and returns the rows in
both tables that share some common value(s)
• The following query joins the Employee table and Salary table on the
common column EmployeeNo. Each table is assigned an alias A & B
and the columns are referenced with the correct alias.
SELECT A.EmployeeNo, A.DepartmentNo, B.NetPay
FROM Employee A
INNER JOIN Salary B
ON (A.EmployeeNo = B. EmployeeNo);
EmployeeNo DepartmentNo NetPay
101 1 36,000
102 2 74,000
103 2 83,000
104 2 70,000
39 © 2016
OUTER JOIN
• LEFT OUTER JOIN and RIGHT OUTER JOIN also combine the results from
multiple tables
– LEFT OUTER JOIN returns all records from the left table and returns only the
matching records from the right table.
– RIGHT OUTER JOIN returns all records from the right table and returns only
matching rows from the left table.
– FULL OUTER JOIN combines the results from both LEFT OUTER and RIGHT OUTER
JOINS. It returns both matching and non-matching rows from the joined tables.
SELECT A.EmployeeNo, A.DepartmentNo, B.NetPay
FROM Employee A
LEFT OUTER JOIN Salary B
ON (A.EmployeeNo = B. EmployeeNo);
EmployeeNo DepartmentNo NetPay
101 1 36,000
102 2 74,000
103 2 83,000
104 2 70,000
40
105
© 2016
3 ?
CROSS JOIN
• Cross Join joins every row from the left table to every row from the right
table.
SELECT A.EmployeeNo, A.DepartmentNo, B.NetPay
FROM Employee A
CROSS JOIN Salary B
WHERE A.EmployeeNo = 101;
EmployeeNo DepartmentNo EmployeeNo NetPay
101 1 101 36,000
101 1 104 70,000
101 1 102 74,000
101 1 103 83,000
41 © 2016
SUBQUERIES
• A subquery returns records from one table based on the values from
another table. It is a SELECT query within another query.
• The SELECT query called as inner query is executed first and the result is
used by the outer query.
• Some of its salient features are −
– A query can have multiple subqueries and subqueries may contain another
subquery.
– Subqueries doesn't return duplicate records.
– If a subquery returns only one value, you can use = operator to join it with the
outer query. If it returns multiple values you can use IN or NOT IN.
SELECT EmployeeNo, NetPay
FROM Salary
WHERE Netpay =
(SELECT MAX(netpay) from Salary);
42 © 2016
SET Operators
• SET operators combine results from multiple SELECT statement.
• This may look similar to Joins, but joins combines columns from multiple
tables whereas SET operators combines rows from multiple tables
• Rules
– The number of columns from each SELECT statement should be same.
– The data types from each SELECT must be compatible.
– ORDER BY should be included only in the final SELECT statement.
43 © 2016
UNION
• The UNION statement is used to combine results from multiple SELECT
statements. It ignores duplicates.
SELECT EmployeeNo FROM employee
UNION
SELECT EmployeeNo FROM salary
;
• The UNION ALL statement is similar to UNION, it combines results from
multiple tables including duplicate rows.
44 © 2016
INTERSECT / MINUS
• The INTERSECT command is also used to combine results from multiple
SELECT statements. It returns the rows from the first SELECT statement
that have a corresponding match in the second SELECT statement. In
other words, it returns the rows that exist in both SELECT statements.
SELECT EmployeeNo FROM employee
INTERSECT
SELECT EmployeeNo FROM salary;
• The MINUS/EXCEPT commands combine rows from multiple tables and
return the rows which are in the first SELECT but not in the second
SELECT. They both return the same results.
SELECT EmployeeNo FROM employee
MINUS
SELECT EmployeeNo FROM salary;
45 © 2016
46
46 © 2014 Teradata