Fall Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday, 12/1 First MP 3 deadline: Friday, 12/4 Final MP 3 deadline: Wednesday, 12/9 Last day to request conflict Final: Wednesday, 12/9
Fall Lab 132 Objectives Review Wildcards Understand logical operator precedence The Between keyword Get information from two tables.
Fall Lab 133 Connecting with SQLyog Start SQLyog. (Start / Class Software / CS105 / SQLyog) You should see login screen. If a connection is not already defined, click “New” and name the new connection CS105. Enter the following data into the login screen: Hostname: cs105-mysql1.cs.uiuc.edu or cs105-mysql2.cs.uiuc.edu User: cs105 Password: cs105 Leave the ‘Database(s)’ box blank. Click “Connect!”
Fall Lab 134 Wildcards – Review What does “cs105” match? What does % (percent) match? What does _ (underscore) match? What does “%sql%”, “marsha%”, “_a%”, match? Does character cases matter in the above examples? What does “cs_0_”, “_s%5”, “_s_%_” match? What does “i__i__is”, “_ee_” match?
Fall Lab 135 Try this query: select * from books where title like "%sql%" or title like "%html%" What does it do? If you change “or” to “and”, what will happen? AND & OR : Review
Fall Lab 136 Suppose we want to find all books that contain either “sql” or “html”, and less than $20. Would this work? select * from books where title like "%sql%" or title like "%html%" and price < 20 Where does “Learning SQL, $23.07” come from? AND & OR together
Fall Lab 137 select * from books where title like "%sql%" or title like "%html%" and price < 20 AND and OR have different order of precedence, but you can use parentheses to supersede them. AND & OR : how to fix this? ( )
NOT operator select * from books where not (title like "%sql%") It turns TRUE to FALSE and vice versa MUST ALWAYS have parentheses Fall Lab 138
9 Logical operators AND/OR/NOT You must always use AND/OR/NOT with boolean conditions, i.e., statements which are either True or False. Suppose we have the where clause : where title like "%sql%" or "%html%" This does not work as expected! How do we fix it?
Fall Lab 1310 Operator Precedence AND, OR, and NOT have different order of precedence. AND has a higher priority than OR. title like "%sql%" or title like "%html%" and price < 20 Is equivalent to title like "%sql%" or (title like "%html%" and price < 20) But not (title like "%sql%" or title like "%html%" ) and price < 20 NOT has highest priority NOT (title like "%sql%" ) and title like "%html%" The parenthesis should remind you of this.
Fall Lab 1311 Joins - Getting Data from Multiple Tables Suppose we want to retrieve all orders associated with each book Try this SQL query select * from orders, books How many records are there?
Fall Lab 1312 How Does Joining Work? select * from orders, books This query tells SQL to “join” the two tables. ISBNCustIdQuantity orders ISBNTitle Sams Teach Yourself SQL in 10 Minutes Learning SQL books
orders.ISBNCustIdQuantitybooks.ISBNTitle Sams Teach Yourself SQL in 10 Minutes Learning SQL Sams Teach Yourself SQL in 10 Minutes Learning SQL Sams Teach Yourself SQL in 10 Minutes Learning SQL select * from orders, books How Does a join work? (continued) 13Fall Lab 13
14 Joins - 2. Join Condition Take a look at the above result only two records are “consistent”, in a sense that they’re about the same book. Thus, the condition to make “consistent” records is where orders.ISBN = books.ISBN Join Conditions specify which column of one table should be “joined” to which column of the other table
How Does a Join Work? (continued) ISBNCustIdQuantity orders ISBNTitle Sams Teach Yourself SQL in 10 Minutes Learning SQL books orders.ISBNCustIdQuantitybooks.ISBNTitle Sams Teach Yourself SQL in 10 Minutes Learning SQL select * from orders, books where orders.ISBN = books.ISBN 15
Fall Lab 1316 Joins - 3. Avoid ambiguous field names If there are two fields with the same name, to avoid ambiguity, we have to specify the field’s table name as the following table_name.field_name e.g: orders.ISBN, books.ISBN
Fall Lab 1317 Joins - 4. More filtering Suppose we want to show orders for sql books only? We need to add this filter condition title like “%sql%” What logical operator should we use to combine this condition with previous?
Fall Lab 1318 Joins - 4. Final result The final query is: select * from orders, books where books.ISBN = orders.ISBN and title like "%SQL%"
Fall Lab 1319 Between Operator Suppose we want to look for orders with a quantity between 2 to 10 items. SQL provides a keyword “between”, for that select * from orders where quantity between 2 and 10 Note that “and” here is not the logical operators we talked about before
Fall Lab 1320 Between Operator equivalence Note that between is “inclusive”, i.e quantity 2 and 10 are included in the result. What is the equivalent query to the previous query? (Hints : use =) where quantity >= 2 and quantity <= 10
Fall Lab 1321 A more complex query We want to find the titles of the books in orders Idea 1: Use the Join idea as before select Title from books, orders where books.isbn = orders.isbn What does this query produce? Why? Idea 2: Use nested select statements: select Title from books where isbn in (select isbn from orders)
Fall Lab 1322 Summary title like "%sql%" or title like "%html%" and price < 20 is different from (title like "%sql%" or title like "%html%“) and price < 20 What strings will match the wildcard pattern “_s_%_”? What “between 2 and 10” means, and their equivalent expression? What is “join condition”? Why do we need them? Try TeachYourself database at home using your book.
Fall Lab 1323 Exercises If we join a table A, containing 11 rows, with a table B, containing 12 rows, without join condition, how many rows will be returned? Can you write an equivalent query for this, without using “between” select * from orders where not (quantity between 2 and 10)
Fall Lab 1324 Exercises Return a list of customers whose firstname contains at least two “a”s Return a list of customers whose firstname or lastname contains at least two “a”s. Return the above results, along with the orders they have made.
Fall Lab 1325 Exercises More