Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Advanced Querying

Similar presentations


Presentation on theme: "Chapter 5 Advanced Querying"— Presentation transcript:

1 Chapter 5 Advanced Querying
University of Manitoba Asper School of Business 3500 DBMS Bob Travica Chapter 5 Advanced Querying Based on G. Post, DBMS: Designing & Building Business Applications Updated 2015 1

2 Use of Advanced Queries
Harder questions calling for joining tables Subqueries In, Not In clauses INNER JOIN, LEFT JOIN, RIGHT JOIN UNION, INTERSECT & other support to Boolean logic, CASE Download Sally’s Pet Store 2010 (queries work with it)

3 Harder Questions (Numbering continues from last lecture) 15) Which animals sold for more than the average price of animals in their category? 16) Which animals have not been sold? 17) Which animals have been sold? 17) Which customers (who bought something at least once) did not buy anything between 11/1/2010 and 12/31/2010? 18) Which customers who bought dogs also bought products for cats (at any time)? 11) List last names of customers who bought something between 4/1/2010 and 5/31/2010. 12) List the Last Name and Phone of anyone who bought a registered white cat between 6/1/1998 and 12/31/2010. 13) How many cats are “in-stock” on 10/1/2010? 14) Which cats sold for more than the average price of cats?

4 Know your data! Sally’s Pet Store Schema
(2007 version) SupplierID Name ContactName Phone Address ZipCode CityID Supplier PONumber OrderDate ReceiveDate EmployeeID ShippingCost Merchandise Order OrderID AnimalOrder AnimalID Cost Animal OrderItem City State AreaCode Population1990 Population1980 Country Latitude Longitude LastName FirstName TaxPayerID DateHired DateReleased Employee ItemID Quantity Category Registration Breed DateBorn Gender Registered Color ListPrice Photo SaleID SaleDate CustomerID SalesTax Sale SalePrice SaleItem Description QuantityOnHand SaleAnimal Customer *

5 Joining Two Tables Query #11: List LastNames of Customers who bought something between 4/1/2010 and 5/31/2010. SELECT DISTINCT Sale.CustomerID, Customer.LastName FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE Sale.SaleDate BETWEEN #4/1/2010# AND #5/31/2010# ORDER BY Customer.LastName; Output: 20 rows

6 SQL Inner Join Syntax SELECT column(s) FROM table1
INNER JOIN table2 ON table1.columnA = table2.columnA WHERE constraints Primary Key Foreign Key Inner Join is most frequently used Matches rows from different tables based on matching values of PKs and FKs

7 Joining More than Two Tables
Query #12: List the Last Name and Phone of customers who bought a registered white cat between 6/1/2010 and 12/31/2010. SELECT Customer.LastName, Customer.Phone, Animal.Category, Animal.Color, Animal.Registered, Sale.SaleDate FROM Customer INNER JOIN (Sale INNER JOIN Animal ON Sale.SaleID=Animal.SaleID) ON Customer.CustomerID=Sale.CustomerID WHERE Animal.Category="Cat" AND Animal.Color LIKE "*White*" AND Animal.Registered IS NOT Null AND Sale.SaleDate Between #6/1/2010# And #12/31/2010# ; Output: 4 rows LastName Phone Category Color Registered SaleDate Bell (717) Cat White CFA 22/12/2010 Kidd (207) 09/12/2010 Hooks (618) White/Black 03/12/2010 Hinton (606) 16/08/2010 To makes senses of the parentheses and order of listing tables, try this: Line up the tables needed following their order in schema: Customer—Sale—Animal. In the FROM close, write names of these tables line by line. (See the SQL statement below.) Add the word INNER JOIN after each of the tables but the last one (Animal) Start joining tables from the last line Animal by using the word ON and appropriate PK-FK (SaleID). Once you are done, enclose the two joined tables in parentheses. Continue working from inside out. The table Sale is the next to join. Use the appropriate PK-FK association (SaleID). To get a clear view of the pairing of words INNER JOIN and ON, you can use formatting as shown below. What remains to join is Customer to Sale on the attribute these share (CustomerID). Add the ON part to the end of the FROM line. SELECT Customer.LastName, Customer.Phone, Animal.Category, Animal.Color, Animal.Registered, Sale.SaleDate FROM Customer INNER JOIN ( Sale INNER JOIN Animal ON Animal.SaleID=Sale.SaleID ) ON Customer.CustomerID=Sale.CustomerID WHERE Animal.Category="Cat" AND Animal.Color LIKE "*White*" AND Animal.Registered IS NOT Null AND Sale.SaleDate Between #6/1/2010# And #12/31/2010# ; And here is a bit differently formatted query (different formats make sense to different people): FROM Customer INNER JOIN (Sale INNER JOIN Animal ON Animal.SaleID=Sale.SaleID) ON Customer.CustomerID=Sale.CustomerID Animal.Category="Cat" AND Animal.Color LIKE "*White*" AND Animal.Registered IS NOT Null AND Sale.SaleDate Between #6/1/2010# And #12/31/2010# ; This version of query joins tables in the opposite order, plus indents the WHERE segment in a programming style. Tables can be joined in various orders as long as there is a PK-FK link between a table to get joined and at least one table in the already joined set of tables. FROM Animal INNER JOIN ( Sale INNER JOIN Customer ON Customer.CustomerID=Sale.CustomerID ) ON Animal.SaleID=Sale.SaleID WHERE ( AND Sale.SaleDate Between #6/1/2010# And #12/31/2010# ) ; See Footnote!

8 Disadvantages: the mix-up in the WHERE line (FKs/PKs and constraints).
Query #16 simplified - To avoid complications with parentheses, you can write the same query by suing an older SQL statement: the WHERE line both joins the tables and sets the constraints. SELECT Customer.LastName, Customer.Phone, Animal.Category, Animal.Color, Animal.Registered, Sale.SaleDate FROM Customer, Sale, Animal WHERE Sale.SaleID=Animal.SaleID AND Customer.CustomerID=Sale.CustomerID AND Animal.Category="Cat" AND Animal.Color LIKE "*White*" AND Animal.Registered IS NOT Null AND Sale.SaleDate Between #6/1/2010# And #12/31/2010# ; Disadvantages: Try to see what happens if you miss to write some of the join columns. Disadvantages: the mix-up in the WHERE line (FKs/PKs and constraints). Still may save time for query writing.

9 Building a Query Identify the tables involved.
Query #12: List the Last Name and Phone of anyone who bought a registered white cat between 6/1/2010 and 12/31/2010. Identify the tables involved. Look at the columns you want to see. LastName, Phone: Customer Look at the columns used in the constraints. Registered, Color, Category: Animal SaleDate: Sale Find linking tables. To link Animal with Sale: SaleAnimal Select the desired columns (may be computed as well) and test the query. Enter the constraints.

10 SQL Mnemonic (Remember!)
Someone From Ireland (Italy, India…) Will Grow Horseradish & Onions SELECT FROM INNER JOIN WHERE GROUP BY HAVING ORDER BY SQL is picky about putting the commands in the proper sequence. For memorizing the sequence, use this mnemonic.

11 Levels of automation: Sub-query 1/2
Query #14: List the cats that drew a donation bigger than the average. We can find the answer in 2 steps: 1. Find what the average price for cats is ($171.82). SELECT Category, AVG(Donation) As [Average Donation] FROM Animal WHERE Category="Cat“ ; Output: Category Average Donation Cat $171.82 2. Use the average price as a constraint in another query: Note about parentheses: Often times you can go with less parentheses rather than more. For example, in query 2 the WHERE segment can be like this: WHERE (Animal.Category="Cat") AND (SaleAnimal.SalePrice>171.86); … and even like this: WHERE Animal.Category="Cat" AND SaleAnimal.SalePrice>171.86; Parentheses show logical grouping of items, and sometimes Access will add parentheses to your parentheses-less query when it is executed. SELECT Animal.AnimalID, Animal.Category, Animal.Breed, Animal.Donation FROM Animal WHERE (Animal.Category="Cat") AND (Animal.Donation>171.82); See Note…

12 Levels of automation: Sub-query 2/2
A more automated solution that executes in 1 step. Uses query 2 on top and query 1 in the sub-query. SELECT Animal.AnimalID, Animal.Category, Animal.Breed, Animal.Donation FROM Animal WHERE (( (Animal.Category="Cat") AND ((Animal.Donation > (SELECT AVG(Donation) WHERE Animal.Category="Cat")) ))) ORDER BY Animal.Donation; The query above while playing with parentheses a bit: SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice > (SELECT AVG(SalePrice) FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (Animal.Category="Cat")))); Logic: There should be as any closing parentheses as there are parentheses used in the given segment of a SQL statement. But some DBMS like Access may not be rigid on this. One previous example also demonstrate this aspects – which? Another way of writing this query at a mid-level automation: 1. write the query for finding the average cat price as in the previous slide, and save it; 2. write the SELECT query as on this and the previous slide and call the saved query from step 1 within it. So you get: WHERE ( Animal.Category='Cat' AND SaleAnimal.SalePrice > ( SELECT * From [Average Price of Cats] ) ); Output: 23 rows

13 Querying Set of Rows with operator IN
Query: List names of customers who purchased one of the following items: 1, 2, 30, 32, 33. SELECT Customer.LastName, Customer.FirstName, SaleItem.ItemID FROM (Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE SaleItem.ItemID In (1,2,30,32,33) ORDER BY Customer.LastName, Customer.FirstName; Output: 39 rows

14 Using IN with a Sub-query
Query: List all customers who bought items for cats. SELECT Customer.CustomerID, Customer.LastName, Customer.FirstName, SaleItem.ItemID FROM (Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE (SaleItem.ItemID In (SELECT Merchandise.ItemID FROM Merchandise WHERE Category="Cat") ) ORDER BY Customer.LastName Asc; Note: 3 tables joined and 4 tables used. Output: 79 rows. An alternative simplified using older SQL syntax and 4 tables to show description of merchandise: SELECT Customer.CustomerID, Customer.LastName, Customer.FirstName, SaleItem.ItemID, Merchandise.Description FROM Customer, Sale, SaleItem, Merchandise WHERE (Customer.CustomerID = Sale.CustomerID) AND (Sale.SaleID = SaleItem.SaleID) AND (SaleItem.ItemID = Merchandise.ItemID) AND (Merchandise.Category="Cat") ORDER BY Customer.LastName Asc;

15 SubQuery: NOT IN (Subtract)
Query #16: Which animals have not been sold (adopted)? Start with list of all animals. Select those whose IDs are not is sales records. Output: 10 rows AnimalID Name Category SaleID 2 Fish 4 Simon Dog 12 Leisha 19 Gene 25 Vivian 34 Rhonda 88 Brandy 175 Donald Cat 181 201 Mammal SELECT Animal.AnimalID, Animal.Name, Animal.Category, Animal.SaleID FROM Animal WHERE Animal.AnimalID NOT IN (SELECT AnimalID FROM Animal INNER JOIN Sale ON Animal.SaleID=Sale.SaleID); Alternative: SELECT Animal.AnimalID, Animal.Name, Animal.Category, SaleID FROM Animal WHERE (Animal.SaleID IS Null); See Note for optional query.

16 Stop! SQL Mnemonic (again) Someone - SELECT From - FROM
Ireland - INNER JOIN Will - WHERE Grow - GROUP BY Horseradish & - HAVING Onions - ORDER BY Stop!

17 Outer Join: Left Outer Join
Query #16: “Which animals have not been sold?” with LEFT JOIN Output building: SELECT * FROM Animal LEFT JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID; LEFT JOIN creates a transient table with: All rows from left table Animal pasted with matching rows in SaleAnimal Blank attributes (null values) in SaleAnimal for the rows in Animal that were not macthed.** To see the difference between LEFT JOIN and INNER JOIN, run the above query as an INNER JOIN. To get just the non-matched rows, add the WHERE line: SELECT Animal.AnimalID, Animal.Name, Animal.Category FROM Animal LEFT JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (SaleAnimal.AnimalID Is Null);

18 Outer Join: Right Outer Join
Query #17: Which animals have been sold? SELECT Animal.AnimalID, Animal.Name, Animal.Category FROM Animal RIGHT JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID; Actually, in this case equivalent to: SELECT Animal.AnimalID, Name, Category FROM SaleAnimal INNER JOIN Animal ON SaleAnimal.AnimalID=Animal.AnimalID; … because RIGHT JOIN selects all the records from the right table (SaleAnimal) and matching records from the left table (Animal). There can be no blank fields, because Animal provides FK for SaleAnimal—all AnimalIDs in SaleAnimal are matched in Animal.

19 Types of Join - Summary + Inner Join = + Left Join = Right Join + =
Note: RIGHT JOIN can be presented as LEFT JOIN by reversing order of tables.

20 UNION Operator Offices in Los Angeles and New York.
SELECT EID, Name, Phone, Salary, ‘East’ AS Office FROM EmployeeEast UNION SELECT EID, Name, Phone, Salary, ‘West’ AS Office FROM EmployeeWest EID Name Phone Salary Office 352 Jones ,000 East 876 Inez ,000 East 372 Stoiko ,000 East 890 Smythe ,000 West 361 Kim ,000 West Offices in Los Angeles and New York. Each has an Employee table (East and West). Need to search data from both tables. Columns in the two SELECT lines must match.

21 UNION, INTERSECT, EXCEPT (Boolean Logic)
SELECT EID, Name FROM EmployeeEast INTERSECT FROM EmployeeWest List the name of any employee who has worked for both the East and West regions: A AC C T1 T2

22 CASE Function ( SQL Server, Oracle) Select AnimalID, CASE
WHEN Date()-DateBorn < 90 Then “Baby” WHEN Date()-DateBorn >= 90 AND Date()-DateBorn < 270 Then “Young” WHEN Date()-DateBorn >= 270 AND Date()-DateBorn < 365 Then “Grown” ELSE “Experienced” END FROM Animal; Used for grouping objects, complex decisions. Example: Define age categories for the animals. Less than 3 months Between 3 months and 9 months Between 9 months and 1 year Over 1 year


Download ppt "Chapter 5 Advanced Querying"

Similar presentations


Ads by Google