Chapter 5 Advanced Querying

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
1 Materi Pendukung Pertemuan > Transaksi pendukung pada SQL Matakuliah: >/ > Tahun: > Versi: >
Chapter 6, part 2 Database System Development (cont.) Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School.
1 Copyright © 2010 Jerry Post with additions & narration by M. E. Kabay. All rights reserved. Advanced Queries IS240 – DBMS Lecture # 8 – M.
Introduction to Structured Query Language (SQL)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
DATABASE APPLICATION DEVELOPMENT SAK 3408 Query Statements.
DBSYSTEMS 1 of 13 Chapter 10 DB System Administration (Part II) 1 Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Based on G. Post, Database Management Systems University of Manitoba Asper School of Business 3500 DBMS Bob Travica Updated 2015 Chapter 1 Introduction.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Copyright © 2010 M. E. Kabay. All rights reserved. Analyzing and Building Simple Queries in SQL IS240 – Database Management Supplement to Chapter 4 M.
Jerry Post Copyright © 2013 DATABASE Database Management Systems Chapter 5 Advanced Queries 1.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
Chapter 3 Query Language Database Application SAK 3408.
Introduction to Databases Chapter 7: Data Access and Manipulation.
Chapter 4 Querying Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500 DBMS Bob Travica.
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2007.
Chapter 9 Joining Data from Multiple Tables
SQL 1: GETTING INFORMATION OUT OF A DATABASE MIS2502 Data Analytics.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
1.NET Web Forms Business Forms © 2002 by Jerry Post.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
© 2007 by Prentice Hall6-1 Introduction to Oracle 10g Chapter 6 Creating Multitable Queries and Views James Perry and Gerald Post.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
劉 志 俊 (Chih-Chin Liu) 中華大學 資訊工程系 October 2001 Chap 9 SQL (III): Advanced Queries.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 All Powder Board and Ski Oracle 9i Workbook Chapter 4: Queries Jerry Post Copyright © 2003.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 6: Forms, Reports, and Applications Jerry Post Copyright © 2003.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – M. E. Kabay, PhD, CISSP-ISSMP.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 4: Queries Jerry Post Copyright © 2003.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 All Powder Board and Ski Microsoft Access Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
Connecting (relating) Data Tables to get Custom Records (Queries) Database Basics.
MIS2502: Data Analytics SQL – Getting Information Out of a Database.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
1 All Powder Board and Ski SQL Server Workbook Chapter 5: Advanced Queries Jerry Post Copyright © 2003.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Chapter 6: Forms, Reports and Applications All Powder Board and Ski
Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 –
M. E. Kabay, PhD, CISSP-ISSMP V:
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
All Powder Board and Ski
David M. Kroenke and David J
Analyzing and Building Simple Queries in SQL
MIS2502: Data Analytics SQL – Getting Information Out of a Database Part 2: Advanced Queries Aaron Zhi Cheng
Relational Database Design
Manipulating Data Lesson 3.
Presentation transcript:

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

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)

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?

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 *

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

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

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) 244-3484 Cat White CFA 22/12/2010 Kidd (207) 585-7583 09/12/2010 Hooks (618) 109-9250 White/Black 03/12/2010 Hinton (606) 241-3182 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!

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.

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.

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.

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…

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

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

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;

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.

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

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);

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.

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

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 3352 45,000 East 876 Inez 8736 47,000 East 372 Stoiko 7632 38,000 East 890 Smythe 9803 62,000 West 361 Kim 7736 73,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.

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

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