2. Agenda Select the DBMS we will use Select Database to work on PUBS Database Databases and tables Connecting to your database Change databases Some rules for SQL Overview of PUBS database First Select Command
3. Agenda cont. Concatenate two columns Alias Where & compound criteria Comparison operators Range Wild cards Escape characters Pattern matching Negation
9. etc.In this class we will use the SQL serveras our DBMS, Because it’s the most interactive one in out case.
10. SQL server 2005 or 2008 We need a SQLserver to interact with Database through queries as DBMS. Download from : http://www.microsoft.com/sqlserver/2005/en/us/express.aspx We need any version of SQL server(2005 or 2008).
11. Select Database to work on I selected the Northwindsimple database to make our queries on. You can download it from: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034
14. Databases come in all shapes and sizes. Some are large and some are small. But each database generally serves a particular purpose.
15. Examples: Tracking employee payroll, sales data on a particular sales line, stock data for a particular industry
16.
17.
18. The same procedure gives the same result with the same database in any other DBMS.
19.
20. Verify your database To check the database you are accessing use: select db_name() Throughout this course some of the things we discuss will be MS SQL Server specific. The DBMS you are using should have a command or function similar to this, but not necessarily the same.
21.
22. select is the same as SELECT is the same as SeLeCt, etc.
23. However depending on the DBMS (Database Management System), the columns might be case sensitive.
33. In order to get information from the database, you must tell the database what you are looking for. The first step along this journey is to get some simple information from the database.
34. select 'Mary had a little lamb.'-------------- Mary had a little lamb. (1 row affected)
35.
36. We can limit the columns returned by specifying them instead of using *.select author_id , author_name from authors
37.
38. We really want to display the first name and the last name separated by a space and then the rest of the data. The plus symbol (+) is the most widely used symbol for concatenation.
46. 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.
47. 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' Important Note : our DBMS might be case insensitive but the actual values of the data is case sensitive. So, Greennotequal to GREEN
48. 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'
49. Compound criteria So, what is the difference between using an ANDand 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
50. Compound criteria You group by using parenthesis The proper SQL statement is as follows: select * from authors where (state = 'KS' and lname = 'Smith') or state = 'CA' Note that: this is differ from the group by clause that will be discussed later in this class
51. Comparison Besides using an =, you can also use any of the other comparison operators: >, <, <=, >=,<>. If you want to get employees whose salary greater than 2000. It will be: select * from employees where salary > 2000
52. Range Suppose we want to select all authors who live in CA, MI, KS, and UT.. What will you do ?? Instead of using multiple ORs, we can use an INoperator select au_lname, state from authors where state in ('CA','KS','MI','UT')
53. Range 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.. What will you do ? SQL has given us a betweenoperator select title_id, price from titles where price between 10 and 20
54. Wild Cards SQL has two wildcard characters The percent (%) symbol designates any string of zero or more characters The underscore (_) designates a single character 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%'
55. 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
56. 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]%‘ We do not want to retrieve name names like McDay select au_lname, au_fname from authors where au_lname like '[A-Z][a-z][a-z][a-z]'
57. 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) --------------------------------------------------------------------- select * from authors where au_fname like '[^ ] [^ ] [^ ] [^ ]‘ 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
58. Negation We briefly touched on negation a few slides before. The negation operator is NOT or in patterns a caret (^) select au_fname, au_lname from authors where au_fname not like '[^ ] [^ ] [^ ] [^ ]' Select all authors who do not live in CA select * from authors where state <> 'CA'