Fall 2009 - Lab 131 CS105 Lab 13 – Logical Operator Precedence and Joining Tables Announcements: MP 3 released Friday, 11/20 Honors project due: Tuesday,

Slides:



Advertisements
Similar presentations
CC SQL Utilities.
Advertisements

Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
+ Structured Query Language Part 2 KROENKE and AUER - DATABASE CONCEPTS (6th Edition) Copyright © 2013 Pearson Education, Inc. Publishing as Prentice Hall.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
1 Working with MS SQL Server II. 2 The sqlcmd Utility Command line utility for MS SQL Server databases. Previous version called osql Available on classroom.
IFS Intro. to Data Management Chapter 6 Filtering your data.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
 The WHERE clause, also called the predicate, provides the power to narrow down the scope of the data retrieved.  Comparison Operators Comparison OperatorDefinition.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
Concepts of Database Management Seventh Edition
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
® Microsoft Access 2010 Tutorial 5 Creating Advanced Queries and Enhancing Table Design.
Chapter 9 Joining Data from Multiple Tables
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Intro to JOINs SQL INNER JOIN SQL OUTER JOIN SQL FULL JOIN SQL CROSS JOIN Intro to VIEWs Simple VIEWs Considerations about VIEWs VIEWs as filters ALTER.
Examining data using Microsoft Access Queries Using Criteria and Calculations SESSION 3.2 This section covers specifying an exact match condition in a.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
SQL (DDL & DML Commands)
Concepts of Database Management Seventh Edition
DAY 19: MICROSOFT ACCESS – CHAPTER 3 CONTD. Aliya Farheen March 17, 2015.
MIS2502: Data Analytics SQL – Getting Information Out of a Database David Schuff
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
SQL queries basics. RHS – SOC 2 SQL query An SQL query is an SQL statement, which specifies a subset of the data in the database A subset in terms of.
DT228/3 Web Development Databases. Querying a database: Partial info Search engines, on-line catalogues often need to allow user to search a database.
Lab_03: Basic SQL.
# 1# 1 Creating Tables, Setting Constraints, and Datatypes What is a constraint and why do we use it? What is a datatype? What does CHAR mean? CS 105.
SQL for Data Retrieval. Save your SQL Scripts When working with SQL Management Studio, you should keep saving your scripts as a.sql file to somewhere.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
INTRODUCTION TO DATABASE USING MICROSOFT ACCESS 2013 Part 5.2 November 16, 2014.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
® Microsoft Access 2010 Tutorial 5 Creating Advanced Queries and Enhancing Table Design.
Structured Query Language
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
# 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.
Lecture Access – Queries. What’s a Query? A question you ask a database –ie: “Who are my Stockton customers?” –ie: “How much did Bob sell on the 14th?”
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
MICROSOFT ACCESS – CHAPTER 3 CONTD. Sravanthi Lakkimsetty Mar 09, 2016.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IST 220 – Intro to DB Lab 2 Specifying Criteria in SELECT Statements.
Restricting and Sorting Data
Microsoft Office Access 2010 Lab 3
CSIS 115 Database Design and Applications for Business
Relational Database Design
Writing Basic SQL SELECT Statements
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
CIS 336 str Teaching Effectively-- snaptutorial.com.
M1G Introduction to Database Development
Lesson 3 Chapter 10.
Use of SQL – The Patricia database
Shelly Cashman: Microsoft Access 2016
Restricting and Sorting Data
Presentation transcript:

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