Академический Документы
Профессиональный Документы
Культура Документы
Landsberg am Lech
Town-Wall
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 2
1
Agenda
SELECT Statement
Joining Tables and Views
Inner Join
Left/Right Outer Join
Full Join
Left/Right Exception Join
Cross Join
Self Join
NULL Values
Union / Intersect / Except
Common Table Expression
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 3
SELECT Statement
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 4
2
SELECT Statement
SELECT Statement
WITH – Common Table Expressions (CTE) SELECT Statement
Full Select
SELECT Columns/Fields (incl.scalar/UD functions)
From Files/Tables/Views/UDTFs incl. Joins
Where Conditions
Group By Clause Sub-Select
Having Clause
Start With Connect By (Release 7.1 PTF)
Order By Clause (Release 6.1)
Fetch First x Rows Only (Release 6.1)
ORDER BY Clauses
Fetch First x Rows, Limit/Offset, For Update Of, Optimize For x Rows
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 6
3
Join Tables and Views
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 7
Join Tables/Views
4
Join Tables/Views – Inner Join
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 9
Joining multiple Files Specify the JOIN type and the next table/View
Specify a single or multiple key Columns after ON
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 10
5
Join Tables/Views - Self Join
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 11
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 12
6
NULL Values
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 13
NULL Predicate
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 14
7
Select identical Values
Identical values
Identical NULL values
are not returned
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 15
Distinct Predicate
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 16
8
Distinct Predicate
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 17
Scalar Functions
Convert NULL Values
COALESCE(Column, Default1, Default2, … DefaultN) or
IFNULL(Column, Default)
Convert NULL Value into Default Value
Available for all Data Types
Convert NULL
value into a
Default value
NULLIF(Column, ToReplace)
Convert specified Value into NULL-Value
Available for all data types
Convert a Default
value into a NULL
value
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 18
9
NULL Values and
Aggregat Function
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 20
10
Join Tables/Views -
Left / Right Outer Join
Left Outer Join All rows from the 1st table/view
All rows from the second table where a match is found
Order headers without
Order positions
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 21
Join Tables/Views -
Left/Right (Outer) Join
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 22
11
Join Tables/Views -
Left/Right (Outer) Join
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 23
12
Join Tables/Views -
Exception Join
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 25
Join Tables/Views -
Cross Join
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 26
13
Union, Intersect and Except
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 27
SELECT Statement
SELECT Statement
Full Select
SELECT Columns/Fields (incl.scalar/UD functions)
From Files/Tables/Views/UDTFs incl. Joins
Where Conditions
Group By Clause Sub-Select
Having Clause
ORDER BY Clauses
Fetch First x Rows, Limit/Offset, For Update Of, Optimize For x Rows
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 28
14
UNION – Clause
UNION - Clause
Duplicates
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 30
15
UNION – Clause
Example
Order By for
the complete
result
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 31
INTERSECT Clause
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 32
16
EXCEPT Clause
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 33
INTERSECT
EXCEPT
Check whether 2 tables are a complete match (all rows/ all columns)
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 34
17
Common Table Expressions
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 35
SELECT Statement
WITH – Common Table Expressions (CTE) SELECT Statement
Full Select
SELECT Columns/Fields (incl.scalar/UD functions)
From Files/Tables/Views/UDTFs incl. Joins
Where Conditions
Group By Clause Sub-Select
Having Clause
ORDER BY Clauses
Fetch First x Rows, Limit/Offset, For Update Of, Optimize For x Rows
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 36
18
Common Table Expressions (CTE)
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 37
19
Common Table Expressions
Example
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 39
Calculate the
difference between the
accumulated sales of
the customer of the
current year with the
accumulated sales of
the customer of the
previous year
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 40
20
Any Questions
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 41
21
Landsberg am Lech
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 43
Special Thanks to
Holger Scherer
For providing an AS/400 (IBM i-System) enabling the
creation of the samples/code used in these presentations
http://www.rzkh.de/start_en.html
Your data is save! … in the bunker
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 44
22
References
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 45
Speaker‘s Biography
Birgitta Hauser has been a Software and Database Engineer since 2008,
focusing on RPG, SQL and Web development on IBM i at Toolmaker Advanced
Efficiency GmbH in Germany.
Since 2002 she has frequently spoken at the COMMON User Groups and
other IBM i and Power Conferences in Germany, other European Countries,
USA and Canada.
In addition, she is co-author of two IBM Redbooks and also the author of
several articles and papers focusing on RPG and SQL for a German publisher,
iPro Developer and IBM DeveloperWorks.
23
SQL - Joins? Yes i can!
Thank you!
24