Chapter 5 – The WHERE Clause

“When I was 14 I thought my parents were the stupidest people in the world. When I was 21 I was amazed at how much they learned in seven years.”

- Mark Twain

The WHERE Clause limits Returning Rows

image

The WHERE Clause here filters how many ROWS are coming back. In this example, I am asking for the report to only rows WHERE the first name is Henry.

Double Quoted Aliases are for Reserved Words and Spaces

image

“Write a wise saying and your name will live forever.”

- Anonymous

When you ALIAS a column, you give it a new name for the report header. Yet, a good rule of thumb is to refer to the column by the alias throughout the query. Whoever wrote the above quote was way off. "Write a wise alias and it will live until the query ends – bummer".

Character Data needs Single Quotes in the WHERE Clause

image

In the WHERE clause, if you search for Character data such as first name, you need single quotes around it. You don’t single-quote integers.

Character Data needs Single Quotes, but Numbers Don’t

image

Character data (letters) need single quotes, but you need NO Single Quotes for Integers (numbers). Remember, you never use double quotes except for aliasing.

Comparisons against a Null Value

image

Null values are an absence of a data value or indicate a column has no data.
NULL is a keyword that represents an unknown or non-existent value.
Null is NOT a data type or a characteristic of data
If Null values are compressed they occupy zero space in the row

The above chart should help on any advanced NULL questions.

NULL means Unknown Data so Equal (=) won’t return rows

image

First thing you need to know about a NULL is its unknown data. It is NOT a zero. It is missing data. Since we don’t know what is in a NULL, you can’t use an = sign. You must use IS NULL or IS NOT NULL.

Use IS NULL or IS NOT NULL when dealing with NULLs

image

If you are looking for a row that holds NULL value, you need to put ‘IS NULL’. This will only bring back the rows with a NULL value in it.

NULL is UNKNOWN DATA so NOT Equal won’t Work

image

The same goes with = NOT NULL. We can’t compare a NULL with any equal sign. We can only deal with NULL values with IS NULL and IS NOT NULL.

Use IS NULL or IS NOT NULL when dealing with NULLs

SELECT    *

FROM    Student_Table

WHEREClass_Code IS NOT NULL ;

image

Much like before, when you want to bring back the rows that do not have NULLs in them, you put an ‘IS NOT NULL’ in the WHERE Clause.

Using Greater Than or Equal To (>=)

image

The WHERE Clause doesn’t just deal with ‘Equals’. You can look for things that are GREATER or LESSER THAN along with asking for things that are GREATER/LESSER THAN or EQUAL to.

AND in the WHERE Clause

image

Notice the WHERE statement and the word AND. In this example, qualifying rows must have a Class_Code = ‘FR’ and also must have a First_Name of ‘Henry’. Notice how the WHERE and the AND clause are on their own line. Good practice!

Troubleshooting AND

image

What is going wrong here? You are using an AND to check the same column. What you are basically asking with this syntax is to see the rows that have BOTH a Grade_Pt of 3.0 and a 4.0. That is impossible, so no rows will be returned.

OR in the WHERE Clause

image

Notice above in the WHERE Clause we use OR. Or allows for either of the parameters to be TRUE in order for the data to qualify and return.

Troubleshooting Or

image

Notice above in the WHERE Clause we use OR. OR allows for either of the parameters to be TRUE in order for the data to qualify and return. The first example errors and is a common mistake. The second example is perfect.

Troubleshooting Character Data

image

This query errors! What is WRONG with this syntax? No Single quotes around SR.

Using Different Columns in an AND Statement

image

Notice that AND separates two different columns, and the data will come back if both are TRUE.

Quiz – How many rows will return?

image

SELECT *  FROM Student_Table

WHERE  Grade_Pt = 4.0 OR Grade_Pt = 3.0

AND             Class_Code = 'SR' ;

Which Seniors have a 3.0 or a 4.0 Grade_Pt average. How many rows will return?

A) 2

C) Error

B) 1

D) 3

Answer to Quiz – How many rows will return?

image

SELECT *  FROM Student_Table

WHERE  Grade_Pt = 4.0 OR Grade_Pt = 3.0

AND             Class_Code = 'SR' ;

image

We had two rows return! Isn’t that a mystery? Why?

What is the Order of Precedence?

image  

( )

image  

NOT

image  

AND

image  

OR

SELECT     *

FROM       Student_Table

WHERE  Grade_Pt = 4.0 OR Grade_Pt = 3.0

AND          Class_Code = 'SR' ;

Syntax has an ORDER OF PRECEDENCE. It will read anything with parentheses around it first. Then, it will read all the NOT statements. Then, the AND statements. FINALLY, the OR Statements. This is why the last query came out odd. Let’s fix it and bring back the right answer set.

Using Parentheses to change the Order of Precedence

image

This is the proper way of looking for rows that have both a Grade_Pt of 3.0 or 4.0 AND also having a Class_Code of ‘SR’. Only ONE row comes back. Parentheses are evaluated first, so this allows you to direct exactly what you want to work first.

Using an IN List in place of OR

image

Using an IN List is a great way of looking for rows that have both a Grade_Pt of 3.0 or 4.0 AND also have a Class_Code of ‘SR’. Only ONE row comes back.

The IN List is an Excellent Technique

image

The IN Statement avoids retyping the same column name separated by an OR. The IN allows you to search the same column for a list of values. Both queries above are equal, but the IN list is a nice way to keep things easy and organized.

IN List vs. OR brings the same Results

image

The IN Statement avoids retyping the same column name separated by an OR. The IN allows you to search the same column for a list of values. Both queries above are equal, but the IN list is a nice way to keep things easy and organized.

The IN List Can Use Character Data

image

The IN Statement avoids retyping the same column name separated by an OR. The IN allows you to search the same column for a list of values. This works with character data as long as you use single quotes.

Using a NOT IN List

image

You can also ask to see the results that ARE NOT IN your parameter list. That requires the column name and a NOT IN. Neither the IN nor NOT IN can search for NULLs!

Null Values in a NOT IN List Bring Back No Rows

image

Few people know that when a NOT IN is used, and a null value is encountered, that no data returns. This is because a null value equals nothing, so it can't compare and eliminate values.

A Technique for Handling Nulls with a NOT IN List

image

This is a great technique to look for a NULL when using a NOT IN List.

BETWEEN is Inclusive

image

This is a BETWEEN. What this allows you to do is see if a column falls in a range. It is inclusive meaning that in our example we will be getting the rows that also have a 2.0 and 4.0 in their column!

NOT BETWEEN is Also Inclusive

image

This is a NOT BETWEEN example. What this allows you to do is see if a column does not fall in a range. It is inclusive meaning that in our example we will be getting no rows where the grade_pt is between a 2.0 and 4.0 in their column! The 2.0 and the 4.0 will not return.

LIKE uses Wildcards Percent ‘%’ and Underscore ‘_’

image

The wildcard percentage sign (%) is a wildcard for any number of characters. We are looking for anyone whose name starts with SM! In this example, the only row that would come back is ‘Smith’. The next page will show an example of underscore.

LIKE command Underscore is Wildcard for one Character

image

The _ underscore sign is a wildcard for any a single character. We are looking for anyone who has an 'a' as the second letter of their last name.

The ilike Command

Customer_Table

Customer_Number

Customer_Name    

11111111

Billy’s Best Choice

31313131

Acme Products

31323134

ACE Consulting

57896883

XYZ Plumbing

87323456

Databases N-U

ilike is NOT case
sensitive

SELECT *

FROM Customer_Table

WHERE  Customer_Name ilike '%BILL%' ;

The LIKE command is case sensitive, but if you don't want case sensitivity then use the ILIKE command. It is not case sensitive.

LIKE Command Works Differently on Char Vs Varchar

image

It is important that you know the data type of the column you are using with your LIKE command. VARCHAR and CHAR data differ slightly.

Troubleshooting LIKE Command on Character Data

image

This is a CHAR (20) data type. This means that any words under 20 characters will pad spaces behind them until they reach 20 characters. You will not get any rows back from this example because technically, no row ends in an ‘N’, but instead ends in a space.

Introducing the TRIM Command

image

This is a CHAR (20) data type. This means that every Last_Name is going to be 20 characters long. Most names are not really 20 characters long, so spaces are padded at the end to ensure filling up all 20 characters. We need to do the TRIM command to remove the leading and trailing spaces. Once the spaces are trimmed, we can find out whose name ends in 'n'.

Introducing the RTRIM Command

image

This is a CHAR (20) data type. This means that every Last_Name is going to be 20 characters long. Most names are not really 20 characters long, so spaces are padded at the end to ensure filling up all 20 characters. We need to do the RTRIM command to remove the trailing spaces. Once the spaces are trimmed, we can find out whose name ends in 'n'.

Quiz – What Data is Left Justified and what is Right?

SELECT     *

FROMSsegmentle_Table

WHEREColumn1 IS NULL

ANDColumn2 IS NULL ;

image

Which Column from the Answer Set could have a DATA TYPE of INTEGER, and which could have Character Data?

Numbers are Right Justified and Character Data is Left

SELECT     *

FROMSsegmentle_Table

WHEREColumn1 IS NULL

ANDColumn2 IS NULL ;

image

All Integers will start from the right and move left. Thus, Col1 was defined during the table create statement to hold an INTEGER. The next page shows a clear example.

Answer – What Data is Left Justified and what is Right?

SELECTEmployee_No, First_Name

FROMEmployee_Table

WHEREEmployee_No = 2000000;

image

All Integers will start from the right and move left. All Character data will start from the left and move to the right.

An example of Data with Left and Right Justification

SELECT Student_ID, Last_Name

FROMStudent_Table ;

image

This is how a standard result set will look. Notice that the integer type in Student_ID starts from the right and goes left. Character data type in Last_Name moves left to right like we are used to seeing while reading English.

A Visual of CHARACTER Data vs. VARCHAR Data

image

Character data pads spaces to the right and Varchar uses a 2-byte VLI instead.

Escape Character in the LIKE Command changes Wildcards

image

/*  We just pretended to add a new row to the Student_Table  */

/*    Can you use the LIKE command to find S% above?    */

Here you will have to utilize a Wildcard Escape Character. Turn the page for more.

Escape Characters Turn off Wildcards in the LIKE Command

image

Can you use the LIKE command to find S% above?

SELECT*

FROMStudent_Table

WHERE First_Name LIKE 'S@%' Escape '@';

We can pick our Escape character and we have chosen an @ sign. This turns the wildcard off for 1 character so we find ‘S%’, without bringing back Stanley or Susie.

Quiz – Turn off that Wildcard

image

Can you use the LIKE command to find the Last_Name of T_?   (pronounced Tunderscore!)

This is a little trickier than you might think so be on your toes. . . . And get a haircut!

ANSWER – To Find that Wildcard

image

Can you use the LIKE command to find the Last_Name of T_?   (pronounced
Tunderscore!)

SELECT * FROM Student_Table

WHERE TRIM(Last_Name) LIKE 'T@_' Escape '@' ;

You didn’t really need to get a full haircut, but just a TRIM Command and the Escape!

Using ILIKE with an AND Clause to Find Multiple Letters

image

The above uses an additional AND clause to find anyone with both a 'M' and an 'S' in their last name. Notice that Kognitio needs the ILIKE command because it is case sensitive.

Using ILIKE with an OR Clause to Find Either Letters

image

The above uses an additional OR clause to find anyone with either a 'M' or a 'B' in their last name. The ILIKE command handles case sensitivity.

Keywords

image

Kognitio has keywords that you can SELECT. You don't need a FROM clause in every query.

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

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