Presentation is loading. Please wait.

Presentation is loading. Please wait.

# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.

Similar presentations


Presentation on theme: "# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105."— Presentation transcript:

1 # 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105

2 # 2# 2 What Is a Query? A query is a question you ask of your database. You can: –display data from multiple tables –control which fields display –perform calculations on field values –save a query automatically

3 # 3# 3 Spring 2010 CS105 Sample query Note we have to capitalize the table names

4 # 4# 4 Spring 2010 CS105 Select Query also called a Result Set The results of a query are displayed as a result set (NOT SAVED AUTOMATICALLY, COULD CHANGE THE NEXT TIME IT IS RUN)—called recordset in some programming situations.

5 # 5# 5 Spring 2010 CS105 Select query-- most common query Retrieves and displays specific data requested from one or more tables; can specify display order—let’s try some: Action queries do mass record updates in one operation. Types: Update Query: alter the data in a Table Append Query: adds records from one table to another table Delete Query: deletes certain records, for example <1980 Make Table Query: creates a new Table from a query’s results. Query Types & Descriptions

6 # 6# 6 Spring 2010 CS105  Select * from Customers Select, *, and from are reserved (key) words and symbols that have special meaning in SQL SQLyog displays key words and symbols in blue. After the word from, if SQL sees the word Customers, it assumes it's a name of a table (see appendix E for a complete list of reserved or key words). Reserved or Key Words

7 # 7# 7 Spring 2010 CS105 Strings where cust_name = “Village Toys”  After the key word where, SQL assumes that the word cust_name is the name of a field (column) “Village Toys” is a string  A string is a sequence of ASCII symbols surrounded by single or double quotes that denotes a value (piece of information)

8 # 8# 8 Spring 2010 CS105 Wildcards The percent sign "%" can be used as a wild card to match any possible character that might appear before or after the characters specified. In another database, the wildcard might be different (such as *) select FirstName, Lastname, City from Employee where FirstName LIKE “ Mi% “; This SQL statement will match any first names that start with ‘Mi'..

9 # 9# 9 Spring 2010 CS105 More Wildcard Characters Lesson 6 in Sams Underscore: _ means that a character must be present Like ‘_oat’ returns boat but not oat Note: From or from both work fine

10 # 10 Spring 2010 CS105 Logical operators AND condition (happy only with both) OR condition (happy with either one) NOT condition (happy with anything but this one) For NOT, must be NOT (name = ‘smith’) select * from Pets where not( name = "Hortence“ )

11 # 11 Spring 2010 CS105 Relational Criteria, as in Where LastName = “Smith” See Lesson 4 in SAMS book = > < (equal to, greater than, less than) = Not equal to LIKE Pattern matching operator BETWEEN IS NULL IN

12 # 12 Spring 2010 CS105 Examples of Relational Query Criteria LIKE ‘Smith’ Age between 21 and 65 (Age >= 21 And Age <= 65 ) <= 98000 returns values of less than or equal to 98000 Not (firstname= ‘Smith’) Select records with values other than Smith

13 # 13 Spring 2010 CS105 Example: Do not do this!!!! Note: Repeat field firstname each time because—this is the worst part—the Query will RUN but the results will be WRONG! Select * from Customers where firstname = "Harry“ or “Fay”

14 # 14 Spring 2010 CS105 Order of precedence: When more than one logical operator is used in a query, NOT is evaluated first, then AND, and finally OR To make sure the statement is read the way you want it to be, use parentheses, because any statement within parentheses is evaluated first!

15 # 15 Spring 2010 CS105 select * from Movies where Rating 8.5 and year > 2000 select * from Movies where (Rating 8.5) and year > 2000 Example:

16 # 16 IN takes a comma delimited list of values, select * from Movies where Language IN ('Hindi','Mandarin') which is equivalent to select * from Movies where Language ='Hindi‘ OR Language = Mandarin' Spring 2010 CS105 IN Operator

17 # 17 select title select title from Movies from Movies where ID IN ( select MovieID where ID IN ( select MovieID from Casting from Casting where ActorID = 3) where ActorID = 3) Spring 2010 CS105 IN Operator to perform subqueries IN Operator to perform subqueries List all movies that Julia Roberts (ActorID = 3) performed. Or select title select title from Movies from Movies where ID IN ( select MovieID where ID IN ( select MovieID from Casting from Casting where ActorID = (select ID where ActorID = (select ID from Actors from Actors where LastName = 'Roberts')) where LastName = 'Roberts'))

18 # 18 Spring 2010 CS105 Multiple Tables Two tables can be linked by a common field Why would we use several tables rather than one big one? How can you link one table to another? Customers Billing

19 # 19 Spring 2010 CS105 Tables relating to other tables via fields

20 # 20 Spring 2010 CS105 Join using Where -- Lesson 13, in SAMs When you are looking for data from two tables, you want to limit your “hits” to records that match You relate one table to another by a common field You want a single set of output is returned, and the join associates the correct rows in each table on the fly Oops—what does “on the fly” mean? (not a permanent relationship)

21 # 21 Spring 2010 CS105 A simple WHERE join You do not need to specify Products.prod_name because Vendors does not have a field named prod_name

22 # 22 Spring 2010 CS105 What happens if you don’t use the Where join? Every vendor is listed with every product, so total: 9 * 6 = 54 records (!!!) Case Sensitive For Table Names

23 # 23 Spring 2010 CS105 You get too many hits!!

24 # 24 Spring 2010 CS105 Order of statements in a Select Query See Lesson 5 in Sams The Where clause comes after the From clause The Order By clause must be last, or you will get an error message

25 # 25 Spring 2010 CS105 Fields that you show aren’t necessarily the ones that you use for the join or filter…

26 # 26 Spring 2010 CS105 Note: if field names are duplicated in various tables, refer to fields specifically (note the table that they are from)

27 # 27 Spring 2010 CS105 In ascending order, NULL fields are at the top

28 # 28 Spring 2010 CS105 Case sensitivity MySQL is running on UNIX, therefore it is case sensitive However, inside a search string, so far we are finding that case doesn’t matter—both these work:

29 # 29 Spring 2010 CS105 Calculated field – Lesson 7 in Sams AS sets up an alias for a calculated value

30 # 30 Spring 2010 CS105 Another calculated field:

31 # 31 Spring 2010 CS105 Another Alias field …before:

32 # 32 Spring 2010 CS105 How do we make it look attractive? Instead of Detroit MI 44444 Why not Detroit, MI 44444

33 # 33 Spring 2010 CS105 Joining Words Together: Concatenation Concatenation is putting two words together Concatenation can be done with a function that takes two or more arguments separated by commas In SQL it works like this: Concat(field one,field two)

34 # 34 Spring 2010 CS105 However, it could look ugly concat(cust_city,cust_state,cust_zip) DetroitMI44444 How do we add a space? How do we insert a comma?

35 # 35 Spring 2010 CS105 After: SQLyog uses the CONCAT function – creates an Alias field by putting 3 columns together use Concat function rather than material shown in SAMs

36 # 36 Spring 2010 CS105 Commenting your SQL code You start comment lines with /* and end with */

37 # 37 Spring 2010 CS105 Remark or Comment Statement in SQL, Excel, VBA Used for documentation. Not “executable” SQLyog uses green font Most of the work you do out in the workplace is maintaining code--code without comments is unusable, and you have to start from scratch (!)

38 # 38 Spring 2010 CS105 To Summarize: How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field?


Download ppt "# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105."

Similar presentations


Ads by Google