Sei sulla pagina 1di 24

Schaumburg

SQL – Let‘s Join Together

Birgitta Hauser bha@toolmaker.de / Hauser@SSS-Software.de


TOOLMAKER Advanced Efficiency GmbH Tel. (+49) 08191 968-0 www.toolmaker.de

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

Basic SELECT Statement


 * = Select all Columns

 List Columns explicitly

Basic SELECT Statement – Filter Data


 Select Rows by adding
WHERE Conditions

Basic SELECT Statement – Sort Data


 Sort Rows by using an
ORDER BY clause
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 5

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)

Merge several Sub-Selects using:


 UNION / EXCEPT / INTERSECT
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

Type Of Join Description


(Inner) Join: Only rows that are found in both tables/views are returned
All rows from the first (left) table/view are returned even if there is
no match in the second table/view
Left (Outer) Join:
If there is no equivalent in the second table, NULL values will be
returned
All rows from the second (right) table/view are returned even if
there is no match in the first table/view
Right (Outer) Join:
If there is no equivalent in the second table, NULL values will be
returned
All rows of both tables are returned independent if there is a
Full (Outer) Join:
match or not
All rows from the first (left) table/view are returned where in the
(Left) Exception Join:
second (right) table/view no match is found
All rows from the second (right) table/view are returned where in the
Right Exception Join:
first (left) table/view no match is found
All rows of the first table/view are connected to all rows of the
Cross Join:
second table (cartesian product)
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 8

4
Join Tables/Views – Inner Join

Inner Join: all rows that are available in both tables

 Join within the WHERE conditions


 Join within the FROM clause

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 9

Join Tables/Views – Inner Join


Joining Multiple Tables/Views

 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

A table/view can be joined with itself


Key fields can be joined by any of the following operators
 = (Equal) > (Greater) >= (Greater Equal)
 <> (Not Equal) < (Lower) <= (Lower Equal)
 Join year with the previous year

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 11

Join Tables/Views - Self Join


Running Totals

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

What are NULL Values?


 Values out of the Valid Range
 Neither Blank nor Zero nor x‘00‘
 Selecting everything between high and low value
 will NOT return NULL values

NULL Value in the Database


 Separate flag set to *On/*Off  Must be checked separately
 SQL: using the NULL predicate
IS NULL / IS NOT NULL (= not a NULL value)
 RPG: Built-In-Function: %NullInd()
 Embedded SQL: Indicator Variable

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

 Identical NULL values


are returned by testing
for NULL values in
both columns

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 15

Distinct Predicate

Compares a Value to another Value


 NULL values are considered as equal
 Contrary to comparing by using the Equal Sign (=)

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 16

8
Distinct Predicate

 Identical values incl. NULL values returned


 Distinct Predicate in a Case Clause

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

NULL values are ignored by aggregate functions


 Important for COUNT(), AVG(), MIN(), MAX()
 Example: 6 rows with 2 NULL values  Count(Column) = 4
 COUNT(*) Counts ALL rows even if there is a row with only NULLs

 Positions without delivery quantity are considered


 Positions without delivery quantity are ignored
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 19

Different Join Methods

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

Right Outer Join  All rows from the 2nd table/view


All rows from the first table where a match is found
 Order positions without
Order headers

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 21

Join Tables/Views -
Left/Right (Outer) Join

Compare sales of the current year with the sales of


the previous year (left 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

Compare sales of the current year with the sales of


the next year (right outer join)

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 23

Join Tables / Views


Full Outer Join

 Coalesce: Convert NULL value into a Default Value


20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 24

12
Join Tables/Views -
Exception Join

Exception Join  All rows from the left table without


match in the second Table

Predicate EXISTS  Returns true if rows exist

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 25

Join Tables/Views -
Cross Join

Cross Join  cartesian product


 Join Tables/Views with a single or a few rows
 No columns for to join available

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

Merge several Sub-Selects using:


 UNION / EXCEPT / INTERSECT
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

A Union-Clause allows to combine several SELECT Statements


and execute them as if it was a single one.
Example: As soon as an order is completed it is copied to a history table. If a
specific order is searched, both tables (OrderHeader and History
Table) must be searched.
Prerequisites for UNION Clauses
 Identical Number of Columns in the SELECT List
 Compatible data types in the selected columns
Add ALL or DISTINCT  controls the output of Duplicates
 UNION (DISTINCT): Returns No Duplicats
DISTINCT is Default and optional
Order is required  Find duplicates  Performance!
 UNION ALL: Returns Duplicates
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 29

UNION - Clause

Merge rows from multiple tables without duplicates

Merge rows from multiple tables including duplicates

 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

Composition of several Sub-Selects


 Display all rows that are identical in both tables/views
 Prerequisites analog UNION
 Identical Number of Columns in the SELECT List
 Compatible data types in the selected columns

Current Table Saved Table Select * From Current


Intersect
CustNo Sales CustNo Sales Select * From Saved
10001 10.000,00 10001 10.000,00
10002 20.000,00 10002 20.000,00 Result
10003 5.000,00 10003 4.500,00 CustNo Sales
10004 7.000,00 10004 6.000,00 10001 10.000,00
10005 12.000,00 10006 15.000,00 10002 20.000,00
10006 15.000,00 10006 15.000,00

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 32

16
EXCEPT Clause

Composition of several Sub-Selects


 Display all rows that are in the 1st table/view but not in the 2nd one
 Prerequisites analog UNION
 Identical number of columns in the SELECT List
 Compatible data types in the selected columns

Current Table Saved Table Select * From Current


Except
CustNo Sales CustNo Sales Select * From Saved
10001 10.000,00 10001 10.000,00
10002 20.000,00 10002 20.000,00 Result
10003 5.000,00 10003 4.500,00 CustNo Sales
10004 7.000,00 10004 6.000,00 10003 5.000,00
10005 12.000,00 10006 15.000,00 10004 7.000,00
10006 15.000,00 10005 12.000,00

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 33

INTERSECT

Returns all rows that completely match in both tables


 Determine duplicates

EXCEPT
Check whether 2 tables are a complete match (all rows/ all columns)

 Check whether all rows and all columns


in both tables are identical:
No row returned  Both tables are identical

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

Merge several Sub-Selects using:


 UNION / EXCEPT / INTERSECT
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)

Part of Select Statements


 Introduced with release V4R4M0
 Must be specified before the Full-Select  With CTE as (Select …

Pseudo-Views only valid for the current SELECT Statement


 Alternate for (nested) Sub-Selects
 Complex SQL-Queries can be split into smaller parts
 CTE can be used in other CTEs
 Order By and Fetch First X Rows only are allowed within CTEs

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 37

Common Table Expressions


Example

 CTE x: Accumulates the daily amounts on customer and year


Selects only the amounts for the years 2008, 2009, 2010
 CTE y: Accumulates the amount / year for the same period
Based on the x CTE
 Final Select: Joins both CTEs on the SalesYear column
Displays all columns from the x CTE and the TotalYear column
and computes the percentage of the customer sales
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 38

19
Common Table Expressions
Example

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 39

Common Table Expressions


Example

 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

Don’t Forget Your Session Surveys

Sign in to the Online Session Guide (www.common.org/sessions)


Go to your personal schedule
Click on the session that you attended
Click on the Feedback Survey button located above the abstract

Completing session surveys helps us plan future programming


and provides feedback used in speaker awards.
Thank you for your participation
20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 42

21
Landsberg am Lech

20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 43

Special Thanks to

Deon (Kit) von Blerk


 For proof-reading/correcting the English translation of these
presentations
 www.changefit.com

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

IBM i information center


 SQL Reference
http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_73/Db2/rbafzpdf.pdf?lang=en

IBM developer works IBM i


http://www.ibm.com/developerworks/ibmi/
 Hierarchical Queries with Db2 Connect By
http://www.ibm.com/developerworks/ibmi/library/i-b2connectby/index.html

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.

She graduated with a business economics diploma, and started programming


on the AS/400 in 1992. She also works in consulting and education as a trainer
for RPG and SQL developers.

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.

In 2015 she received the John Earl Speaker Scholarship Award.


20.09.2017 Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser Seite 46

23
SQL - Joins? Yes i can!
Thank you!

Birgitta Hauser Toolmaker Advanced Efficiency GmbH


Birgitta Hauser
bha@toolmaker.de Westendstrasse 34
BHA@Toolmaker.de
Hauser@SSS-Software.de 86916 Kaufering
Hauser@SSS-Software.de
www.toolmaker.de
Tel. (+49) 08191 968-0
20.09.2017 Seite 47
Common Fall Conference 2017 - SQL - Let's Join Together – Birgitta Hauser

24

Potrebbero piacerti anche