SQL 101
SQL 101
Datenaufbereitung
1
Dieter
© 2016 Schwarenthorer, Felix Pichler-Rossbacher
Machine Setup
• Host: gpsdb02.eastus.cloudapp.azure.com
• Username: student_Matrikelnummer
• Password: pass_Matrikelnummer
2 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
3 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
4 © 2016
RDBMS vs Database
5 © 2016
Tables
6 © 2016
Column
7 © 2016
Row
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
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
11 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
12 © 2016
Data Definition Language (DDL)
13 © 2016
CREATE TABLE
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
Example
ALTER TABLE employee
ADD BirthDate DATE FORMAT 'YYYY-MM-DD',
DROP DOB;
16 © 2016
DROP TABLE
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
19 © 2016
Data Manipulation Language (DML)
20 © 2016
INSERT
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
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
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
26 © 2016
SELECT contd.
Example
SELECT EmployeeNo,FirstName,LastName
FROM Employee;
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
Example
SELECT * FROM employee
WHERE EmployeeNo = 101;
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
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;
Example
FirstName Count(*)
Alex 1
Mike 1
Peter 1
Robert 2
32 © 2016
AGGREGATE FUNCTIONS
33 © 2016
AGGREGATE FUNCTIONS
• COUNT
SELECT count(*)
FROM Salary
34 © 2016
Agenda
1 Database
Fundamentals
2 DDL
3 DML
35 © 2016
JOINS
36 © 2016
37 © 2016
JOINS
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);
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);
• 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;
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.
42 © 2016
SET Operators
43 © 2016
UNION
44 © 2016
INTERSECT / MINUS
45 © 2016
46
46 © 2014 Teradata