Introduction To SQLUnit 2
Modern Business Technology
Introduction To TSQLUnit 2
Developed by
Michael Hotek
Unit 2
Goals• Limit result set with where• Use compound criteria• Grouping conditions• Comparison operators• Ranges• Wildcards• Escape characters• Pattern Matching• Negation
Where
• So far we have returned the entire contents of a table.
• This is usually not very practical
• Suppose we wanted to see the authors that live in California.
• We could do a select * from authors and scroll through the result set looking for those where state = CA
• While feasible for a small table, this is not practical.
Where
• So to limit the result set to just the data you need, we will use the third major SQL clause: where
• The where clause tells the database which rows to retrieve.
• To just retrieve those authors that live in CA, we would use the following:
• select au_lname, aufname, state from authors where state = 'CA'
au_lname au_fname state
---------------------------------------- -------------------- -----
White Johnson CA
Green Marjorie CA
Carson Cheryl CA
O'Leary Michael CA
...
(15 row(s) affected)
Compound Criteria
• This limited our result set to just those authors in CA
• But our list of authors could begin to get very large and we’re only looking for those authors with a last name of Green.
• We would do this with the following:
• select au_lname, au_fname, state from authors where state = 'CA' and lname = 'Green'
au_lname au_fname state
---------------------------------------- -------------------- -----
Green Marjorie CA
(1 row(s) affected)
Compound Criteria
• Make sure you are careful with the spelling. Our database is case insensitive, but this does not apply to the data values.
• Green does not equal GREEN
Compound Criteria
• Now that we know how to get just those authors who live in CA, how do we get the authors that live in KS also?
• We accomplish this through the use of an OR instead of an AND
• select * from authors where state = 'CA' or state = 'KS'
au_lname au_fname state
---------------------------------------- -------------------- -----
White Johnson CA
Green Marjorie CA
Carson Cheryl CA
O'Leary Michael CA
Straight Dean CA
Smith Meander KS
Bennet Abraham CA
Dull Ann CA
Gringlesby Burt CA
Locksley Charlene CA
...
(16 row(s) affected)
Compound Criteria
• So, what is the difference between using an AND and an OR?
• The AND is exclusive– This means that the row must meet all of the
conditions in order to be selected
• The OR is inclusive– This means that for a row to be selected, it
has to meet just one of the criteria
Compound Criteria
• Now we are going to get a little more complicated.
• We want to select all authors who live in KS with a last name of Smith and also every author from CA.
• We know how to do the first part
• select au_lname, au_fname, state from authors where state = 'KS' and au_lname = 'Smith'
• We also know how to do the second part
• select au_lname, au_fname, state from authors where state = 'CA'
• We just need to put them together
• select au_lname, au_fname, state from authors where state = 'KS' and au_lname = 'Smith' or state = 'CA'
au_lname au_fname state
---------------------------------------- -------------------- -----
Smith George CA
White Johnson CA
Green Marjorie CA
Carson Cheryl CA
O'Leary Michael CA
Straight Dean CA
Smith Meander KS
Bennet Abraham CA
...
(17 row(s) affected)
Compound Criteria
Grouping Criteria
• While this SQL statement returns the data we want, it isn’t very clear and is sloppy
• When using compound criteria in a where clause, you should always group the criteria to make it plain exactly what you want.
• You group by using parenthesis
• The proper SQL statement is as follows:
• select * from authors where (state = 'KS' and lname = 'Smith') or state = 'CA'
Comparisons
• Besides using an =, you can also use any of the other comparison operators: >, <, <=, >=.
• Suppose we want to return all of the books with a price greater than $10.00
select title_id, price from titles where price > 10
• We could also write: select title_id, price from titles where price >
$10.00title_id price
-------- --------------------------
BU1032 19.99
BU1111 11.95
BU7832 19.99
MC2222 19.99
PC1035 22.95
PC8888 20.00
...
(12 row(s) affected)
Comparisons
• One other thing you can take advantage of with comparison operators is that they don’t simply apply to numeric types of data.
• They can also be used on character data.
• To select all authors who live in states that come after MA we could use the following
select au_lname, state from authors where state > 'MA'
au_lname state
---------------------------------------- -----
Greene TN
Blotchet-Halls OR
del Castillo MI
Panteley MD
Ringer UT
Ringer UT
(6 row(s) affected)
Range Output
• Suppose we want to select all authors who live in CA, MI, KS, and UT
• We could write the following:select au_lname, state from authors where
state = 'CA' or state = 'MI' or state = 'KS' or state = 'UT'
• With long lists, this get get very tedious and take a lot of typing.
• Fortunately, SQL gives us something much better
IN
• Instead of using multiple ORs, we can use an IN operator
select au_lname, state from authors where state in ('CA','KS','MI','UT')
• This will return the same listau_lname state
---------------------------------------- -----
Smith CA
White CA
...
Straight CA
Smith KS
Bennet CA
...
Yokomoto CA
del Castillo MI
Stringer CA
...
McBadden CA
Ringer UT
Ringer UT
(20 row(s) affected)
Range Output
• We now want to select all books that have a price greater than or equal to $10, but also less than or equal to $20.
• We could write the following:
select title_id, price from titles where price >= 10 and price <= 20
• But, there is a much simpler way
• SQL has given us a between operator
select title_id, price from titles where price between 10 and 20
Wildcards
• Sometimes we do not know exactly what we are looking for
• Or we are looking for the group of data that match a certain pattern
• In these cases we would use wildcards within our where clause
• SQL has two wildcard characters– The percent (%) symbol designates any
string of zero or more characters– The underscore (_) designates a single
character
Wildcards
• Suppose we wanted to select all authors whose first names start with M
select au_fname, au_lname from authors where au_fname like 'M%'
au_fname au_lname
-------------------- ----------------------------------------
Marjorie Green
Michael O'Leary
Meander Smith
Morningstar Greene
Michel DeFrance
(5 row(s) affected)
Wildcards
• Maybe we want to select all of the authors whose first name is Carl.
• We have to be careful here, because it could be spelled Carl or Karl
select au_fname, au_lname from authors where au_fname like '_arl'
au_fname au_lname
-------------------- ----------------------------------------
Carl Burns
Karl Johnson
(2 row(s) affected)
Wildcards
• You can combine wildcards to retrieve exactly what you need
• Suppose we needed to retrieve all of the Smiths in the database
• The last name could be spelled Smith, Smithe, or Smythe. We want to retrieve all of the spellings
select au_fname, au_lname from authors where au_lname like 'Sm_th%'
au_fname au_lname
-------------------- ----------------------------------------
Meander Smith
Jim Smithe
Patti Smythe
(3 row(s) affected)
Escape characters
• But what happens when we really want to find a % inside of the data
• To find this data we will employ an escape character
select notes from titles where notes like '%@%%' escape '@'
• This tells the DBMS to treat the next character after the escape character (@) as a literal string
notes
-------------------------------------------------------------------
What happens when the data runs dry?%
(1 row(s) affected)
Pattern Matching
• But what do we do when we know what we are looking for, but know it could have many variations.
• We can employ a technique called pattern matching
• This technique can mix wildcards with sets of characters that required to be present
• These are designated within brackets inside of the string we are matching
Pattern Matching
• Suppose we wanted to retrieve all of the authors whose last names started with either an L, M, or S
select au_lname, au_fname from authors where au_lname like '[LMS]%'
au_lname au_fname
---------------------------------------- --------------------
Locksley Charlene
MacFeather Stearns
McBadden Heather
Smith George
Smith Meander
Smithe Jim
Smythe Patti
Straight Dean
Stringer Dirk
(9 row(s) affected)
Pattern Matching
• Suppose we want to retrieve all five letter first names where only the first character is uppercase
• We do not want to retrieve name names like McDay
• We also don't want names with special characters like apostrophes
select au_lname, au_fname from authors where au_lname like '[A-Z][a-z][a-z][a-z]'
au_lname au_fname
---------------------------------------- --------------------
Dull Ann
(1 row(s) affected)
Pattern Matching
• We want to retrieve all books with a title of Life Without Fear, but don't know how the word without was stored (uppercase, lowercase, or mixed case)
select title_id, titles from titles where title
like '%[Ww][Ii][Tt][Hh][Oo][Uu][Tt]%'title_id title
-------- --------------------------------------------------------
PS2106 Life Without Fear
(1 row(s) affected)
Pattern Matching
• We now want to retrieve just those authors whose first name is four characters long
select au_lname, au_fname from authors where au_fname like '____' (That's four underscore characters)
au_lname au_fname
---------------------------------------- --------------------
Burns Carl
Gringlesby Burt
Johnson Karl
Ringer Ann
Straight Dean
Stringer Dirk
(6 row(s) affected)
Pattern Matching
• But you ask, why do we see the entry for Ann in this list. It only has three characters.
• This is for two reasons– There was a space added to the end– Depending on how the database was set
up, it could pad spaces on to the end (Beyond scope)
Pattern Matching
• To get around this we exclude the space
select * from authors where au_fname
like '[^ ] [^ ] [^ ] [^ ]'au_id au_lname au_fname
----------- ---------------------------------------- --------------------
111-11-1112 Burns Carl
111-11-1113 Johnson Karl
274-80-9391 Straight Dean
472-27-2349 Gringlesby Burt
724-08-9931 Stringer Dirk
(5 row(s) affected)
• The caret is a negation operator. The query above says to retrieve any first names that do not have a space as one of the four characters
Pattern Matching
• Granted, in most real world situations you will not go to these lengths when retrieving data.
• But constructs like this are used extensively to ensure only valid data is entered into tables
• Rules and constraints are beyond the scope of this unit, but the examples below are for demonstrative purposes to give an idea of further applications to pattern matching
Pattern Matching
• Suppose you have a column that will accept 6 characters
• You have to be careful, because numbers and special characters like #,@,& will also go in this column
• To restrict this to just characters, use the following
[A-z] [A-z] [A-z] [A-z] [A-z] [A-z]
Real World Example
• A table we are working with stores social security numbers (complete with dashes)
'[0-9] [0-9] [0-9]- [0-9] [0-9]- [0-9] [0-9]
[0-9] [0-9]'
• Vehicle VIN numbers have a very specific format that conforms to the following VIN: 1G2JB14KOL7569785
' [0-9][A-Z] [0-9][A-Z][A-Z] [0-9] [0-9][A-Z][A-Z][A-Z] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] '
Negation
• We briefly touched on negation a few slides before.
• The negation operator is NOT or in patterns a caret (^)
Negation
• In a query above we extracted all four letter first names that did not have a space in them.
• Now we want to extract everything but these names
select au_fname, au_lname from authors where au_fname not like '[^ ] [^ ] [^ ] [^ ]'
au_fname au_lname
-------------------- ----------------------------------------
Abraham Bennet
Reginald Blotchet-Halls
Carl Burns
Cheryl Carson
Michel DeFrance
Ann Dull
Marjorie Green
Morningstar Greene
Burt Gringlesby
Sheryl Hunter
Karl Johnson
Livia Karsen
...
(28 row(s) affected)
Negation
• Select all authors who do not live in CA
select * from authors where state <> 'CA'
• Depending on DBMS, this can also be written as
select au_fname, au_lname from authors
where state != 'CA'au_fname au_lname state
-------------------- ---------------------------------------- -----
Carl Burns MA
Karl Johnson MA
Patti Smythe MA
Jim Smithe MA
Meander Smith KS
Morningsta Greene TN
Reginald Blotchet-Halls OR
Innes del Castillo MI
Michel DeFrance IN
Sylvia Panteley MD
Anne Ringer UT
Albert Ringer UT
(12 row(s) affected)
Review
• A where clause allows us to restrict the result set
• We can combine multiple criteria in a single where clause using and/or
• We can use comparison operators to specify ranges of data
• IN allows us to easily specify a list of values to find
• Between simplifies some range searches and is inclusive (The value specified as the upper and lower bound is also retrieved)
• We can use % and _ as wildcards to do sophisticated searching
• We can use an escape character to cause SQL to ignore a wildcard and treat as a literal
• These can be combined with pattern matching to specify very specific patterns of data
• We can negate our searching by using not, ^, <>, or !=
Unit 2 Exercises
• Time allotted is 30 minutes