Top Banner
Advanced SQL SMSU Computer Services Short Course
72

Advanced SQL SMSU Computer Services Short Course.

Dec 22, 2015

Download

Documents

Oscar Hamilton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced SQL SMSU Computer Services Short Course.

Advanced SQL

SMSU

Computer Services

Short Course

Page 2: Advanced SQL SMSU Computer Services Short Course.

2

Contact Information

• Greg Snider – MIS Database Analyst

• Ext. 6-4397

• Email – SGS345B

Don’t hesitate to email or call if you have any questions after you start using QM and SQL.

Page 3: Advanced SQL SMSU Computer Services Short Course.

3

Agenda

• SQL Limits

• FETCH FIRST n ROWS ONLY

• Quantified predicates

• Named expressions

• Scalar Subselect

Page 4: Advanced SQL SMSU Computer Services Short Course.

4

Agenda

• Case

• Multiple Join Types

• Common Table Expressions

• In-line Views/Table Expressions/Nested Table Expressions / Derived Tables

Page 5: Advanced SQL SMSU Computer Services Short Course.

5

SQL Limits

• SQL statements can now be 65, 535 characters long

• 256 tables can be referenced in a single SQL statement

• Approximately 8000 elements in a SELECT list

Page 6: Advanced SQL SMSU Computer Services Short Course.

6

FETCH FIRST n ROWS ONLY

• Sets the maximum number of rows to be returned

• Follows the ORDER BY clause if it is present

• If the ORDER BY clause is present, then the FETCH FIRST operation is performed on the sorted data

Page 7: Advanced SQL SMSU Computer Services Short Course.

7

FETCH FIRST n ROWS ONLY Example

Page 8: Advanced SQL SMSU Computer Services Short Course.

8

FETCH FIRST n ROWS ONLY Example Results

Page 9: Advanced SQL SMSU Computer Services Short Course.

9

Quantified Predicates

• Compares a value with a set of values

• Expression [=, <>, <, >, <=, >=] [Some, Any, All] (subselect)

Page 10: Advanced SQL SMSU Computer Services Short Course.

10

Quantified Predicates

• Expression > ALL (subselect)– The predicate is true if the expression is

greater than each individual value returned by the subselect. If the subselect returns no values, the predicate is true. The result is false if the specified relationship is false for at least one value.

– The <> All predicate is equivalent to the NOT IN predicate.

Page 11: Advanced SQL SMSU Computer Services Short Course.

11

Quantified Predicates

• Expression > ANY (subselect– The predicate is true if the expression is

greater than at least on of the values returned by the subselect. If the subselect returns no values, the predicate is false.

• The = ANY quantified operator is equivalent to the IN predicate

• SOME is synonymous with ANY

Page 12: Advanced SQL SMSU Computer Services Short Course.

12

Named Expressions

• The AS clause lets you assign a meaningful name to an expression, which makes it easier to refer back to that expression.

• SELECT YEAR(CURRENT DATE) – BIRTH_DATE) AS AGE

• The named column cannot be used in a WHERE clause

• The named column can be used in a ORDER BY clause or GROUP BY clause

Page 13: Advanced SQL SMSU Computer Services Short Course.

13

Named Expressions

Page 14: Advanced SQL SMSU Computer Services Short Course.

14

Named Expressions

Page 15: Advanced SQL SMSU Computer Services Short Course.

15

Scalar Subselect

• A scalar subselect is a subselect, enclosed in parentheses, placed in the SELECT list that returns a single result row and a single result column. If the result of the subselect is no rows, then the null value is returned. An error is returned if there is more than one row in the result.

Page 16: Advanced SQL SMSU Computer Services Short Course.

16

Scalar Subselect Example

Page 17: Advanced SQL SMSU Computer Services Short Course.

17

Scalar Subselect Example – Results

Page 18: Advanced SQL SMSU Computer Services Short Course.

18

CASE

• The CASE expression can be used to easily manipulate the data in a column– It can be used to change codes in to

meaningful words– It can be used to protect against exceptions

such as division by zero

Page 19: Advanced SQL SMSU Computer Services Short Course.

19

CASE Syntax

Page 20: Advanced SQL SMSU Computer Services Short Course.

20

CASE Example 1

Page 21: Advanced SQL SMSU Computer Services Short Course.

21

CASE Example 2

Page 22: Advanced SQL SMSU Computer Services Short Course.

22

CASE Example 3

Page 23: Advanced SQL SMSU Computer Services Short Course.

23

Can I use more than one column in a CASE expression?

• Yes, any number of WHEN conditions can be used in the CASE expression, and each WHEN can reference a different column or expression.

CASE (WHEN COL7 > 15 THEN COL7 * .0133

WHEN COL3 = 34 THEN COL3 / .0133

WHEN COL8 < 98 THEN 999

ELSE NULL END)

AS MULTICALC

Page 24: Advanced SQL SMSU Computer Services Short Course.

24

Where can I use CASE expressions?

• Anywhere an expression is allowed in a SQL statement

• SELECT clause• WHERE clause• HAVING clause• Built-in Function• IN (list)

Page 25: Advanced SQL SMSU Computer Services Short Course.

25

Can I use a subselect inside a CASE expression?

Page 26: Advanced SQL SMSU Computer Services Short Course.

26

CASE Subselect Results

Page 27: Advanced SQL SMSU Computer Services Short Course.

27

Can I use an IN list in a CASE expression?

Page 28: Advanced SQL SMSU Computer Services Short Course.

28

CASE Example 4

Page 29: Advanced SQL SMSU Computer Services Short Course.

29

CASE Example 4 Results

Page 30: Advanced SQL SMSU Computer Services Short Course.

30

CASE Example 5 (join ORing)

SELECT columns

FROM T1, T2

WHERE (CASE WHEN T1.ID1 IS NULL THEN T1.ID2 END) = T2.COLX

Page 31: Advanced SQL SMSU Computer Services Short Course.

31

CASE Example 6 (table pivoting)

• The table looks like this:

SALES_DATE SALES_PERSON REGION SALES

01/10/2004 Williams South 5

01/11/2004 Smith East 6

02/24/2004 Jones West 4

03/31/2004 Williams South 3

Page 32: Advanced SQL SMSU Computer Services Short Course.

32

CASE Example 6

Page 33: Advanced SQL SMSU Computer Services Short Course.

33

CASE Example 6 – Results

Page 34: Advanced SQL SMSU Computer Services Short Course.

34

Multiple Join types

• Inner Join

• Left Outer Join

• Right Outer Join

• Left Exception Join

• Right Exception Join

• Full Outer Join

Page 35: Advanced SQL SMSU Computer Services Short Course.

35

Inner Join

• An Inner Join returns only the rows from each table that have matching values in the join columns. Any rows that do not have a match between the tables will not appear in the result table.

• This is the join you’re used to using.

FROM T1 INNER JOIN T2 ON T1.COLA = T2.COL1

Page 36: Advanced SQL SMSU Computer Services Short Course.

36

Left Outer Join

• A Left Outer Join returns values for all of the rows from the first table (the table on the left) and the values from the second table for the rows that match. Any rows that do not have a match in the second table will return the null value for all columns from the second table.

FROM T1 LEFT OUTER JOIN T2

ON T1.COLA = T2.COL1

Page 37: Advanced SQL SMSU Computer Services Short Course.

37

Left Outer Join – Example

• List students and the classes they are taking, if any.

Page 38: Advanced SQL SMSU Computer Services Short Course.

38

Left Outer Join – Example

Page 39: Advanced SQL SMSU Computer Services Short Course.

39

Left Outer Join – Results

Page 40: Advanced SQL SMSU Computer Services Short Course.

40

Right Outer Join

• A Right Outer Join return values for all of the rows from the second table (the table on the right) and the values from the first table for the rows that match. Any rows that do not have a match in the first table will return the null value for all columns from the first table.

FROM T1 RIGHT OUTER JOIN T2

ON T1.COLA = T2.COL1

Page 41: Advanced SQL SMSU Computer Services Short Course.

41

Left Exception Join

• A Left Exception Join returns only the rows from the left table that do not have a match in the right table. Columns in the result table that come from the right table have the null value.

FROM T1 LEFT EXCEPTION JOIN T2

ON T1.COLA = T2.COL1

Page 42: Advanced SQL SMSU Computer Services Short Course.

42

Left Exception Join – Example

• List students who are not taking classes

Page 43: Advanced SQL SMSU Computer Services Short Course.

43

Left Exception Join – Example

Page 44: Advanced SQL SMSU Computer Services Short Course.

44

Left Exception Join – Results

Page 45: Advanced SQL SMSU Computer Services Short Course.

45

Right Exception Join

• A Right Exception Join returns only the rows from the right table that do not have a match in the left table. Columns in the result table that come from the left table have the null value.

FROM T1 RIGHT EXCEPTION JOIN T2

ON T1.COLA = T2.COL1

Page 46: Advanced SQL SMSU Computer Services Short Course.

46

Full Outer Join

• The Full Outer Join includes the inner join (the rows that match) and the rows from both the left and right tables that do not match.

• The Full Outer Join is not included in this version of SQL, but it can be simulated.

• You simulate a Full Outer Join by using a Left Outer Join union’ed with a Right Exception Join

Page 47: Advanced SQL SMSU Computer Services Short Course.

47

Full Outer Join – Example

Page 48: Advanced SQL SMSU Computer Services Short Course.

48

Full Outer Join – Results

Page 49: Advanced SQL SMSU Computer Services Short Course.

49

Full Outer Join - Results

Page 50: Advanced SQL SMSU Computer Services Short Course.

50

How to know which join to use?

• If the request contains one of the key condition phrases: “whether or not”, “regardless of”, “no matter what”, “if any”.

• If any of these phrases fit in the generic information request of “get all information from this table—condition phrase—other information is present in the other table”, then a left outer join will be necessary.

Page 51: Advanced SQL SMSU Computer Services Short Course.

51

Where to code the predicates

• The predicates applying to the left table in LEFT JOINs should be coded in a WHERE clause just like normal.

• Predicates on the right table in a LEFT JOIN must be in a table expression.

• Predicates for a FULL OUTER JOIN, since it is simulated with a UNION, can be placed in the proper place using the above rules

Page 52: Advanced SQL SMSU Computer Services Short Course.

52

Joins and Predicates - Example

• List all students in a specific department whether or not they are taking classes this semester. If they are, list the course code and number.

Page 53: Advanced SQL SMSU Computer Services Short Course.

53

Joins and Predicates – Example

Page 54: Advanced SQL SMSU Computer Services Short Course.

54

Joins and Predicates – Example Results

Page 55: Advanced SQL SMSU Computer Services Short Course.

55

Joins and Predicates – Example

Page 56: Advanced SQL SMSU Computer Services Short Course.

56

Joins and Predicates – Example Results

Page 57: Advanced SQL SMSU Computer Services Short Course.

57

Joins and Predicates – Example

Page 58: Advanced SQL SMSU Computer Services Short Course.

58

Joins and Predicates – Example Results

Page 59: Advanced SQL SMSU Computer Services Short Course.

59

Combining Joins

• Joins can be combined almost any way you want.

• FROM T1 LEFT OUTER JOIN T2 RIGHT OUTER JOIN T3 LEFT EXCEPTION JOIN T4

• FROM T1 LEFT OUTER JOIN ((T2 RIGHT OUTER JOIN T3) LEFT EXCEPTION JOIN T4))

Page 60: Advanced SQL SMSU Computer Services Short Course.

60

Common Table Expressions

• A table you create just for use in a complex query

• Can be referenced in a SQL statement just like any other table

• It only exists for the time it takes to run the SQL statement

WITH table name AS select-stmt

Page 61: Advanced SQL SMSU Computer Services Short Course.

61

Common Table Expression – Example

Page 62: Advanced SQL SMSU Computer Services Short Course.

62

Common Table Expression – Example Results

Page 63: Advanced SQL SMSU Computer Services Short Course.

63

In-line Views/Nested Table Expressions/Derived Tables

• All the terms refer to the same thing

• A nested table expression is a temporary view where the definition is nested (defined directly) in the FROM clause of a query.

• The SELECT statement must be enclosed in parenthesis.

Page 64: Advanced SQL SMSU Computer Services Short Course.

64

Nested Table Expression – Example

Page 65: Advanced SQL SMSU Computer Services Short Course.

65

Nested Table Expression – Example Results

Page 66: Advanced SQL SMSU Computer Services Short Course.

66

Differences between Common and Nested Table Expressions

• A common table expression can only be defined at the beginning of a query, but once defined, can be used in any FROM clause in the query.

• A nested table expression can be placed in any FROM clause in the query, but must be defined each time it is used.

Page 67: Advanced SQL SMSU Computer Services Short Course.

67

Table expressions – Example 1

Page 68: Advanced SQL SMSU Computer Services Short Course.

68

Table expressions – Example 1 – Results

Page 69: Advanced SQL SMSU Computer Services Short Course.

69

Table Expressions – Example 2 - Table Pivoting

Page 70: Advanced SQL SMSU Computer Services Short Course.

70

Table Expressions – Example 2 - Results

Page 71: Advanced SQL SMSU Computer Services Short Course.

71

Retrieving detail and summary in same result row

SELECT SALES.*, SUMMARY.*

FROM SALES,

(SELECT REGION,

SUM(SALES) AS TOT_SALES,

MAX(SALES) AS MAX_SALES,

AVG(SALES AS AVG_SALES

FROM SALES

GROUP BY REGION) AS SUMMARY

WHERE SALES.REGION = SUMMARY.REGION

Page 72: Advanced SQL SMSU Computer Services Short Course.

72

What can be in table expressions

• Anything that you can put in a regular SELECT statement can be used.