CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.

Slides:



Advertisements
Similar presentations
Concepts of Database Management Seventh Edition
Advertisements

Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
Microsoft Office 2007 Access Chapter 2 Querying a Database.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Creating a Blank Database 1. Open up Microsoft Access 2. Click on Blank document button 3. On the right panel, Specify the location for saving your database.
Creating And Maintaining A Database. 2 Learn the guidelines for designing databases When designing a database, first try to think of all the fields of.
Chapter 2 Querying a Database
Mary K. Olson PS Reporting Instance – Query Tool 101.
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Access Tutorial 3 Maintaining and Querying a Database
Chapter 9 Macros, Navigation Forms, PivotTables, and PivotCharts
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1.
Chapter 2 Querying a Database
DATA, DATABASES, AND QUERIES Managing Data in Relational Databases CS1100Microsoft Access - Introduction1 Created By Martin Schedlbauer
Copyright 2007, Paradigm Publishing Inc. BACKNEXTEND 3-1 LINKS TO OBJECTIVES Save a Filter as a Query Save a Filter as a Query Parameter Query Inner, Left,
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 1Advanced Tables, Relationships, Queries, and Forms Chapter 3Advanced Query Techniques.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. ACCESS 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 4 – Creating New.
Chapter 3 Single-Table Queries
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 4 BACKNEXTEND 4-1 LINKS TO OBJECTIVES Query Design Query Criteria Modify a Query Using OR.
Microsoft ® Office Access ® 2007 Training Datasheets II: Sum, sort, filter, and find your data ICT Staff Development presents:
Database Queries. Queries Queries are questions used to retrieve information from a database. Contain criteria to specify the records and fields to be.
Microsoft Access 2010 Building and Using Queries.
Analyzing Data For Effective Decision Making Chapter 3.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Concepts of Database Management Seventh Edition
Chapter 17 Creating a Database.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
Copyright 2008 McGraw-Hill Ryerson 1 TECHNOLOGY PLUG-IN T7 PROBLEM SOLVING USING ACCESS.
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Pasewark & Pasewark 1 Access Lesson 3 Creating Queries Microsoft Office 2007: Introductory.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
McGraw-Hill/Irwin © 2008 The McGraw-Hill Companies, All Rights Reserved Plug-In T7: Problem Solving Using Access 2007 Business Driven Technology.
What are queries? Queries are a way of searching for and compiling data from one or more tables. Running a query is like asking a detailed question of.
XP. Objectives Sort data and filter data Summarize an Excel table Insert subtotals into a range of data Outline buttons to show or hide details Create.
You can sort Access data so you can view records in the order you want to view them, and you can filter data so you only see the records you want to see.
© Paradigm Publishing, Inc Access 2010 Level 1 Unit 1Creating Tables and Queries Chapter 3Performing Queries.
T7-1 LEARNING OUTCOMES – ACCESS PROBLEM SOLVING 1.Describe the process of using the Simple Query Wizard using Access 2.Describe the process of using the.
Excel part 5 Working with Excel Tables, PivotTables, and PivotCharts.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Microsoft Access Lesson 5 Lexington Technology Center February 25, 2003 Bob Herring On the Web at
QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
21 Copyright © 2009, Oracle. All rights reserved. Working with Oracle Business Intelligence Answers.
CS1100: Data, Databases, Queries Action Queries CS11001Advanced Queries.
Access Queries and Forms. Adding a New Field  To insert a field after you have saved your table, open Access, and open the table  It is easier to add.
McGraw-Hill/Irwin © 2008 The McGraw-Hill Companies, All Rights Reserved Plug-In T7: Problem Solving Using Access 2007 Business Driven Technology.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Tutorial 5: Working with Excel Tables, PivotTables, and PivotCharts
Queries.
Plug-In T7: Problem Solving Using Access 2007
Access Maintaining and Querying a Database
Database Queries.
Creating and Modifying Queries
SQL – Entire Select.
Topic 12 Lesson 1 – Data and databases
REACH Computer Resource Center
Creating complex queries using nesting
Building Queries using the Principle of Simplest Query (POSQ)
Joins and other advanced Queries
Topic 12 Lesson 2 – Retrieving Data with Queries
Presentation transcript:

CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1

Nested Queries Some complex queries must be based on the results of other queries. The result of a query is a virtual table, i.e., something that looks and can be used like a table, but is not actually stored in the database as a table. A query can be used (as a table is used) in the design of a query. CS1100Microsoft Access2

Subqueries A subquery is a query statement that’s nested inside of another query Sometimes need to use the results of a query as a field in another query, or as a criterion for a query field. Example: – How many orders have a total under $2,000? – To find the answer, first need to calculate order totals and filter out those that are $2,000 and over. Then do a count. CS1100Microsoft Access3

The Database Layout These are all of the tables in the database: CS1100Microsoft Access4

Where Does The Data Come From? Order O0001 Customer Contact Contact ID:C0004 Name:Colon, Nicholas Address:9020 N.W. 75 Street Coral Springs, FL Order Date:4/15/1999 Product IDProduct NameQuantityUnitPriceExtendedPrice P0013DVD Disks1 $ P0014HD Floppy Disks4 $ 9.99 $ P0027Norton Anti-Virus1 $ Order Total: $ CS11005Microsoft Access Orders.OrderID Contacts ZipCodes Orders.OrderDate LineItems ExtendedPrice = Quantity * UnitPrice Total Order Amount

Query What is the total for each order? CS1100Microsoft Access6

Query What is the total for each order? CS1100Microsoft Access7

Query Which orders are for less than $2,000? CS1100Microsoft Access8

Query Which orders are for less than $2,000? CS1100Microsoft Access9

Query How many of these orders (orders less than $2,000) are there? CS1100Microsoft Access10

Query How many of these orders (orders less than $2,000) are there? – This requires that we build a new query with the previous query as a subquery CS1100Microsoft Access11

Query Who placed these orders less than $2,000? CS1100Microsoft Access12

Query Who placed these orders less than $2,000? – Use OrdersTotal again as a subquery – Combine with Orders and Contacts to get customer information – Use Group By to remove duplicates CS1100Microsoft Access13 There are 15 contacts in total

Watch Out: Access Caches Queries Whenever Access executes a query it saves (“caches”) the result. When that query is used as a subquery, Access uses the cached result instead of running it again. If you then update the subquery, Access does not automatically refresh the result for the query that uses the subquery. CS1100Microsoft Access14

Refreshing Queries To refresh all queries, you need to: – Close all queries Click the right mouse button on the query tab and select “Close” – Load the query again double-click on the query in the navigator This forces a refresh. CS1100Microsoft Access15

Step by Step How many orders are there that have a total value of less than $1000? – Create a query that finds all orders with a value less than $1000 – Save the query under an appropriate name – Create another query based on the previous query that COUNTs all of the rows CAUTION: Do not modify or rename queries that are used as subqueries. CS1100Microsoft Access16

Example: Subquery CS1100Microsoft Access17 Subquery: OrdersLessThan1000 Note the use of the subquery and the aggregate COUNT function So there are 21 orders with a total less than $1000

Example: AVG What is the average cost of all orders? CS1100Microsoft Access18 Subquery: OrderTotals Note the use of the subquery and the aggregate AVG function

TRY FOR YOURSELF… Access Queries CS1100Microsoft Access19

Question 1 Which contacts placed three or more orders? CS1100Microsoft Access20

Question 1 Solution Which contacts placed three or more orders? CS1100Microsoft Access21 Answer

Question 2 How many contacts placed three or more orders? CS1100Microsoft Access22

Question 2 Solution How many contacts placed three or more orders? – Use the previous query as a subquery and build a new query that counts the rows CS1100Microsoft Access23 Answer

Question 3 Which states had contacts that placed 3 or more orders? CS1100Microsoft Access24

Question 3 Solution Which states had contacts that placed 3 or more? CS1100Microsoft Access25 Answer

Maximum and Minimum What is the maximum amount of any order that was ever placed? What is the minimum amount of any order that was ever placed? Which order was the minimum order? Who placed the minimum (smallest) order? CS1100Microsoft Access26

Query What is the maximum amount of any order that was ever placed? CS1100Microsoft Access27

Query What is the maximum amount of the any order that was ever placed? – Use OrdersTotal again as a subquery – Apply the MAX aggregate function CS1100Microsoft Access28

Query What is the minimum amount of any order that was ever placed? CS1100Microsoft Access29

Query What is the minimum amount of any order that was ever placed? – Use OrdersTotal again as a subquery – Apply the MIN aggregate function CS1100Microsoft Access30

Query Which order was the minimum order? CS1100Microsoft Access31

Query Which order was the minimum order? – Requires the previous query and OrdersTotal as subqueries – Can use one of two approaches CS1100Microsoft Access32 Approach 1: Use Criteria Approach 2: Create Relationship

Query Who placed the smallest order? CS1100Microsoft Access33

Query Who placed the smallest order? – Use previous query as subquery – Combine with Orders and Contacts table CS1100Microsoft Access34

Parameterized Query To allow user input for a query value: – specify a variable that has a name different from any of the field names CS1100Advanced Queries35

Hiding Subqueries Queries (and tables) can be “hidden”: – Right-click on query in navigation panel – Select “Hide in this Group” CS1100Microsoft Access36

Unhiding Queries Click anywhere in the Query Explorer Select “Navigation Options…” Check “Show Hidden Objects” Now all hidden queries are visible and can be unhidden. CS1100Advanced Queries37

Summary Group By removes duplicate rows where the Group By values are the same Aggregate functions apply to groups or entire tables depending how they are used Subqueries are necessary to build complex queries CS1100Microsoft Access38