Introduction To SQL Unit 2 Modern Business Technology Introduction To TSQL Unit 2 Developed by Michael Hotek
Unit 2 <ul><li>Goals </li></ul><ul><li>Limit result set with where </li></ul><ul><li>Use compound criteria </li></ul><ul><...
Where <ul><li>So far we have returned the entire contents of a table. </li></ul><ul><li>This is usually not very practical...
Where <ul><li>So to limit the result set to just the data you need, we will use the third major SQL clause:  where </li></...
Compound Criteria <ul><li>This limited our result set to just those authors in CA </li></ul><ul><li>But our list of author...
Compound Criteria <ul><li>Make sure you are careful with the spelling.  Our database is case insensitive, but this does no...
Compound Criteria <ul><li>Now that we know how to get just those authors who live in CA, how do we get the authors that li...
Compound   Criteria <ul><li>So, what is the difference between using an AND and an OR? </li></ul><ul><li>The AND is exclus...
Compound Criteria <ul><li>Now we are going to get a little more complicated. </li></ul><ul><li>We want to select all autho...
<ul><li>We also know how to do the second part </li></ul><ul><li>select au_lname, au_fname, state from authors where state...
Grouping Criteria <ul><li>While this SQL statement returns the data we want, it isn’t very clear and is sloppy </li></ul><...
Comparisons <ul><li>Besides using an =, you can also use any of the other comparison operators: >, <, <=, >=. </li></ul><u...
Comparisons <ul><li>One other thing you can take advantage of with comparison operators is that they don’t simply apply to...
Range Output <ul><li>Suppose we want to select all authors who live in CA, MI, KS, and UT </li></ul><ul><li>We could write...
IN <ul><li>Instead of using multiple ORs, we can use an IN operator </li></ul><ul><li>select au_lname, state from authors ...
Range Output <ul><li>We now want to select all books that have a price greater than or equal to $10, but also less than or...
Wildcards <ul><li>Sometimes we do not know exactly what we are looking for </li></ul><ul><li>Or we are looking for the gro...
Wildcards <ul><li>Suppose we wanted to select all authors whose first names start with M </li></ul><ul><li>select au_fname...
Wildcards <ul><li>Maybe we want to select all of the authors whose first name is Carl. </li></ul><ul><li>We have to be car...
Wildcards <ul><li>You can combine wildcards to retrieve exactly what you need </li></ul><ul><li>Suppose we needed to retri...
Escape characters <ul><li>But what happens when we really want to find a % inside of the data </li></ul><ul><li>To find th...
Pattern Matching <ul><li>But what do we do when we know what we are looking for, but know it could have many variations. <...
Pattern Matching <ul><li>Suppose we wanted to retrieve all of the authors whose last names started with either an L, M, or...
Pattern Matching <ul><li>Suppose we want to retrieve all five letter first names where only the first character is upperca...
Pattern Matching <ul><li>We want to retrieve all books with a title of Life Without Fear, but don't know how the word with...
Pattern Matching <ul><li>We now want to retrieve just those authors whose first name is four characters long </li></ul><ul...
Pattern Matching <ul><li>But you ask, why do we see the entry for Ann in this list.  It only has three characters. </li></...
Pattern Matching <ul><li>To get around this we exclude the space </li></ul><ul><li>select * from authors where au_fname  <...
Pattern Matching <ul><li>Granted, in most real world situations you will not go to these lengths when retrieving data. </l...
Pattern Matching <ul><li>Suppose you have a column that will accept 6 characters </li></ul><ul><li>You have to be careful,...
Real World Example <ul><li>A table we are working with stores social security numbers (complete with dashes) </li></ul><ul...
Negation <ul><li>We briefly touched on negation a few slides before. </li></ul><ul><li>The negation operator is NOT or in ...
Negation <ul><li>In a query above we extracted all four letter first names that did not have a space in them. </li></ul><u...
Negation <ul><li>Select all authors who do not live in CA </li></ul><ul><li>select * from authors where state <> 'CA' </li...
Review <ul><li>A where clause allows us to restrict the result set </li></ul><ul><li>We can combine multiple criteria in a...
Unit 2   Exercises <ul><li>Time allotted is 30 minutes </li></ul>
Upcoming SlideShare
Loading in …5
×

Intro To TSQL - Unit 2

1,354 views

Published on

Intro To TSQL - Unit 2

Published in: Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,354
On SlideShare
0
From Embeds
0
Number of Embeds
59
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 1
  • Intro To TSQL - Unit 2

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

    ×