Chapter 10 – Interrogating the Data

"The difference between genius and stupidity is that genius has its limits"

- Albert Einstein

Using the LOWER Command

image

The LOWER function converts all letters in a specified string to lowercase letters. If there are characters in the string that are not letters, they are not affected by the LOWER command.

A LOWER Command Example

image

The LOWER function converts all letters in a specified string to lowercase letters. If there are characters in the string that are not letters, they are not affected by the LOWER command. Above, we compare a LOWER 'ABCDE' = 'abcde' and they are now equivalent because we have lowercased the 'ABCDE'.

Using the UPPER Command

image

The UPPER function converts all letters in a specified string to uppercase letters. If there are characters in the string that are not letters, they are not affected by the UPPER command.

An UPPER Command Example

image

The UPPER function converts all letters in a specified string to uppercase letters. If there are characters in the string that are not letters, they are not affected by the UPPER command. Above, we compare a string of 'ABCDE' = UPPER 'abcde' and they are now equivalent because we have uppercased the 'abcde'.

Non-Letters are Unaffected by UPPER and LOWER

image

The UPPER and LOWER functions convert all letters in a specified string to either upper or lower case letters. If there are characters in the string that are not letters, they are not affected by the UPPER or LOWER commands. Notice in our example that the numbers 1 and 2 were unaffected by the LOWER and UPPER commands.

Quiz – Fill in the Answers for the NULLIF Command

image

What would the above Answer Set produce from your analysis?

Quiz – Fill in the Answers for the NULLIF Command

image

Look at the answers above. If it doesn’t make sense, go over it again until it does.

The COALESCE Command

image

SELECT Last_Name

,COALESCE (Home_Phone, Work_Phone, Cell_Phone)

as Phone

FROM  Sample_Table ;

Last_Name 

Phone 

Fill in the Answer Set above after looking at the table and the query

Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null.

The COALESCE Answer Set

image

SELECT Last_Name

,COALESCE (Home_Phone, Work_Phone, Cell_Phone)

as Phone

FROM  Sample_Table ;

Last_Name

Phone

Jones

555-1234

Patel

456-7890

Gonzales

354-0987

Nguyen

?

Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null.

The COALESCE Command – Fill In the Answers

image

Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null.

The COALESCE Answer Set

image

SELECT

Last_Name

,Grade_Pt

,Student_ID

,COALESCE (Grade_Pt, Student_ID) as ValidStudents

FROM Student_Table

WHERE Last_Name IN ('Johnson', 'Larkins', 'Thomas')

ORDER BY 1 ;

image

Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null.

COALESCE is Equivalent to This CASE Statement

SELECT  Last_Name,  Grade_Pt,  Class_Code

,COALESCE  (Grade_Pt,  Student_ID) as ValidStudents

FROM  Student_Table ;

SELECT

Last_Name

,Grade_Pt

,Class_Code

, CASE

WHEN Grade_Pt      IS NOT NULL THEN Grade_Pt

WHEN Student_ID IS NOT NULL THEN Student_ID

ELSE NULL

END as ValidStudents

FROMStudent_Table ;

Coalesce returns the first non-Null value in a list, and if all values are Null, returns Null. Above are two queries that return the exact same answer set. These examples are designed to give you a better idea of how Coalesce works.

The Basics of CAST (Convert And STore)

CAST will convert a column or value’s data type
temporarily into another data type. Below is the syntax:

image

Data can be converted from one type to another by using the CAST function. As long as the data involved does not break any data rules (i.e. placing alphabetic or special characters into a numeric data type), the conversion works. The name of the CAST function comes from the Convert And STore operation that it performs.

Some Great CAST (Convert And Store) Examples

image

The first CAST truncates the five characters (left to right) to form the single character ‘A’. In the second CAST, the integer 128 is converted to three characters and left justified in the output. The 127 was initially stored in a SMALLINT (5 digits - up to 32767) and then converted to an INTEGER.

A Rounding Example

image

Rounding isn't always intuitive as you can see from the examples above.

Quiz - The Basics of the CASE Statements

image

SELECT Course_Name

,CASE Credits

WHEN 1 THEN 'One Credit'

WHEN 2 THEN 'Two Credits'

WHEN 3 THEN 'Three Credits'

END  AS CreditAlias

FROM  Course_Table
WHERE Course_ID IN (220, 300) ;

Course_Name                       

CreditAlias    

Physical Database Design
SQL Features

                     

This is a CASE STATEMENT which allows you to evaluate a column in your table, and from that, come up with a new answer for your report. Every CASE begins with a CASE, and they all must end with a corresponding END. What would the answer be?

Answer to Quiz - The Basics of the CASE Statements

image

SELECT Course_Name

,CASE Credits

WHEN 1 THEN 'One Credit'

WHEN 2 THEN 'Two Credits'

WHEN 3 THEN 'Three Credits'

END  AS CreditAlias

FROM  Course_Table
WHERE Course_ID IN (220, 300) ;

Course_Name                      

CreditAlias     

Physical Database Design
SQL Features

?
Two Credits

The answer for the Physical Database Design class is null. This is because it fell through the case statement. The answer for the SQL Features course is Two Credits. Once a case statement gets a match, it leaves the statement and gets the next row.

Using an ELSE in the Case Statement

image

SELECT Course_Name

,CASE Credits

WHEN 1 THEN 'One Credit'

WHEN 2 THEN 'Two Credits'

WHEN 3 THEN 'Three Credits'

ELSE 'Four Credits'

END  AS CreditAlias

FROM  Course_Table WHERE Course_ID IN (220, 300) ;

Course_Name                      

CreditAlias      

Physical Database Design
SQL Features

Four Credits
Two Credits

Now that we have an ELSE in our case statement we are guaranteed that nothing will fall through.

Using an ELSE as a Safety Net

image

SELECT Course_Name

,CASE Credits

WHEN 1 THEN 'One Credit'

WHEN 2 THEN 'Two Credits'

WHEN 3 THEN 'Three Credits'

WHEN 4 THEN 'Four Credits'

ELSE 'Do not know'

END  AS CreditAlias

FROM  Course_Table ;

Now that we have an ELSE in our case statement we are guaranteed that nothing will fall through. An ELSE should be used in case you forgot a possibility and there was no match.

Rules For a Valued Case Statement

image

Rules for a Valued CASE:

1.You can only check for equality.

2.You can only check the value of the
column Credits.

There are two types of CASE statements. There is the Valued CASE and the Searched CASE. Above are the rules for the Valued CASE statement.

Rules For a Searched Case Statement

image

Rules for a Searched CASE:

1.You can check any way you want. You
don't have to just check for equality.

2.You can check other column values also.
You can mix and match different columns.

There are two types of CASE statements. There is the Valued CASE and the Searched CASE. Above are the rules for the Searched CASE statement.

Valued Case Vs. A Searched Case

image

The second example is better unless you have a simple query like the first example.

Quiz - Valued Case Statement

image

Look at the CASE Statement and the Course_Table, and fill in the Answer Set.

Answer - Valued Case Statement

image

Above is the full answer set.

Quiz - Searched Case Statement

image

Look at the CASE Statement and look at the Course_Table, and fill in the Answer Set.

Answer - Searched Case Statement

image

Above is the full answer set.

The CASE Challenge

Bring back all columns from the Employee_Table, but then use a
CASE statement to specifically follow these parameters exactly:

WHEN Salary < 900000 THEN 'CEO'
If the Salary is BETWEEN 20000 and 40000 THEN 'Worker
If a person is in Dept_No 200 THEN 'Winner'
If a person is making < 50000 Then 'Manager'
If a person is making < 60000 Then 'VP'

Make sure to sort the data by Dept_No.

The above challenge is actually trickier than you might think. Good luck!

The CASE Challenge Answer

SELECT E.*
,CASE
WHEN Dept_No = 200 THEN 'Winner'
WHEN Salary BETWEEN 20000 and 40000 THEN 'Worker'
WHEN Salary < 50000 THEN 'Manager'
WHEN Salary < 60000 THEN 'VP'
WHEN Salary < 900000 THEN 'CEO'
Else 'Don"t know'
END
FROM Employee_Table E
ORDER BY Dept_No ;

The above challenge was tricky, but if you got the right order you nailed it.

Combining Searched Case and Valued Case

image

This Query above uses both a Valued Case and Searched Case. That’s ALLOWED!

A Trick for getting a Horizontal Case

SELECT
AVG(CASE Class_Code
WHEN 'FR' THEN Grade_pt
ELSE NULLEND)ASFreshman_GPA
,AVG(CASE Class_Code
WHEN 'SO' THEN Grade_pt
ELSE NULLEND)ASSophomore_GPA
,AVG(CASE Class_Code
WHEN 'JR' THEN Grade_pt
ELSE NULLEND)ASJunior_GPA
,AVG(CASE Class_Code
WHEN 'SR' THEN Grade_pt
ELSE NULLEND)ASSenior_GPA
FROM Student_Table
WHERE Class_Code IS NOT NULL ;

image

Aggregates ignore Nulls so knowing this trick has allowed for Horizontal Reporting.

Nested Case

SELECT Last_Name
,CASE Class_Code
WHEN 'JR'THEN 'Jr '
||(CASE WHEN Grade_pt < 2THEN 'Failing'
WHEN Grade_pt < 3.5THEN 'Passing'
ELSE 'Exceeding'
END)
ELSE'Sr '
||(CASE WHEN Grade_pt < 2THEN 'Failing'
WHEN Grade_pt < 3.5THEN 'Passing'
ELSE 'Exceeding'
END)
ENDASStatus
FROM Student_Table WHERE Class_Code IN ('JR','SR')
ORDER BY Class_Code, Last_Name;

Last_Name

Status           

Bond
McRoberts
Delaney
Phillips

Jr Exceeding
Jr Failing
Sr Passing
Sr Passing

A NESTED Case occurs when you have a Case Statement within another CASE Statement. Notice the Double Pipe symbols (||) that provide Concatenation.

Put a CASE in the ORDER BY

image

I will bet you didn't know you could put a CASE statement in the Order By? You do now!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset