Your SlideShare is downloading. ×
  • Like
  • Save
Intro To TSQL - Unit 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Intro To TSQL - Unit 2

  • 979 views
Published

Intro To TSQL - Unit 2

Intro To TSQL - Unit 2

Published in Business , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
979
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • 1

Transcript

  • 1. Introduction To SQL Unit 2 Modern Business Technology Introduction To TSQL Unit 2 Developed by Michael Hotek
  • 2. Unit 2
    • Goals
    • Limit result set with where
    • Use compound criteria
    • Grouping conditions
    • Comparison operators
    • Ranges
    • Wildcards
    • Escape characters
    • Pattern Matching
    • Negation
  • 3. 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.
  • 4. 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)
  • 5. 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)
  • 6. 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
  • 7. 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)
  • 8. 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
  • 9. 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'
  • 10.
    • 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
  • 11. 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'
  • 12. 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.00
    • title_id price
    • -------- --------------------------
    • BU1032 19.99
    • BU1111 11.95
    • BU7832 19.99
    • MC2222 19.99
    • PC1035 22.95
    • PC8888 20.00
    • ...
    • (12 row(s) affected)
  • 13. 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)
  • 14. 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
  • 15. 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 list
    • au_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)
  • 16. 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
  • 17. 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
  • 18. 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)
  • 19. 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)
  • 20. 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)
  • 21. 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)
  • 22. 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
  • 23. 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)
  • 24. 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)
  • 25. 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)
  • 26. 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)
  • 27. 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)
  • 28. 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
  • 29. 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
  • 30. 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]
  • 31. 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] '
  • 32. Negation
    • We briefly touched on negation a few slides before.
    • The negation operator is NOT or in patterns a caret (^)
  • 33. 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)
  • 34. 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)
  • 35. 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 !=
  • 36. Unit 2 Exercises
    • Time allotted is 30 minutes