QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1.

Slides:



Advertisements
Similar presentations
Access 2007 ® Use Databases How can Microsoft Access 2007 help you structure your database?
Advertisements

Microsoft® Office Access® 2007 Training
With Microsoft Access 2010 © 2011 Pearson Education, Inc. Publishing as Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access.
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 3 1 Microsoft Office Access 2003 Tutorial 3 – 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.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Querying a Database Using the Select Query Window
Querying a Database Microsoft Office Access 2003.
Concepts of Database Management Sixth Edition
Build a database IV: Create queries for a new Access database Overview: Ask your data — create queries It’s time to create queries, one of the most powerful.
Microsoft Access 2010 Chapter 7 Using SQL.
MS Access: Database Concepts Instructor: Vicki Weidler.
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
1 Access Lesson 3 Creating Queries Microsoft Office 2010 Introductory Pasewark & Pasewark.
1 Access Lesson 3 Creating Queries Microsoft Office 2010 Introductory.
Copyright © 2003 by Prentice Hall Module 4 Database Management Systems 1.What is a database? Data hierarchy and data organization Field, record, file,
SQL 資料庫查詢語言 取材自 EIS, 3 rd edition By Dunn et al..
CS1100: Access Reports A (Very) Short Tutorial on Microsoft Access Report Construction Created By Martin Schedlbauer With contributions from Matthew Ekstrand-Abueg.
Queries. What is a query? A query is actually the name for any database manipulation operation. The most commonly used type is a select query, which is.
Analyzing Data For Effective Decision Making Chapter 3.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Examining.
Microsoft ® Access ® 2010 Training Create Queries for a New Database If a yellow security bar appears at the top of the screen in PowerPoint, click Enable.
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
® Microsoft Office 2010 Access Tutorial 3 Maintaining and Querying a Database.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
CS1100: Microsoft Access Managing Data in Relational Databases Created By Martin Schedlbauer CS11001Microsoft Access - Introduction.
SESSION 3.1 This section covers using the query window in design view to create a query and sorting & filtering data while in a datasheet view. Microsoft.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
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?
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
® Microsoft Access 2010 Tutorial 3 Maintaining and Querying a Database.
COMPREHENSIVE Access Tutorial 3 Maintaining and Querying a 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.
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.
1 Access Lesson 3 Creating Queries. 2 Creating a Query with the Simple Query Wizard Query-- database object that lets you ask the database about the data.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Access Chapter 3-Obtaining Answers to Your Data Questions.
1 Chapter 4: Creating Simple Queries 4.1 Introduction to the Query Task 4.2 Selecting Columns and Filtering Rows 4.3 Creating New Columns with an Expression.
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.
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.
Microsoft Access Lesson 5 Lexington Technology Center February 25, 2003 Bob Herring On the Web at
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?”
Lesson 4: Querying a Database. 2 Learning Objectives After studying this lesson, you will be able to:  Create, save, and run select queries  Set query.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Reports. Reports display information retrieved from a database in an attractive printed format. Reports can be created directly from tables, but More.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Microsoft Office Access 2003
Microsoft Office Access 2003
Aggregations Various Aggregation Functions GROUP BY HAVING.
Introduction to Database Programs
Topic 12 Lesson 1 – Data and databases
Creating complex queries using nesting
Building Queries using the Principle of Simplest Query (POSQ)
Introduction to Database Programs
Joins and other advanced Queries
Topic 12 Lesson 2 – Retrieving Data with Queries
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

QUERY CONSTRUCTION CS1100: Data, Databases, and Queries CS1100Microsoft Access1

LAYOUT OF THE ORDERS DATABASE Microsoft Access Tutorial: Data, Databases, and Queries CS1100Microsoft Access2

The Orders Database We will be using a database that contains data for orders placed by customers for our examples. This database stores the following information: – For each order know what was ordered, how much of that item was ordered, and at what price. – For each order know who placed that order. – For each customer (called a contact) store where they live. – For each product track its description and price. CS11003Microsoft Access

A Sample Order 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: $ CS11004Microsoft Access

Tables, Rows, and Identifiers Microsoft Access is a relational database which means that it stores data in tables Each table contains rows; one row for each record, i.e., a contact, order, product, etc. Each row in a table has a unique identifier, e.g., OrderID, ProductID, ContactID, etc. CS11005Microsoft Access

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

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: $ CS11007Microsoft Access Orders.OrderID Contacts ZipCodes Orders.OrderDate LineItems ExtendedPrice = Quantity * UnitPrice Total Order Amount

RETRIEVING DATA WITH QUERIES Microsoft Access Tutorial: Data, Databases, and Queries CS1100Microsoft Access8

Queries Data is retrieved through queries. Queries are formulated in a specialized language called SQL (pronounced SEQUEL). Microsoft Access makes it easy to create SQL queries through a simple drag-and-drop interface called the Query Builder. Queries are eventually integrated into reports, forms, programs, or executed by themselves. CS1100Microsoft Access9

Queries on Tables Queries retrieve data from one or more tables. You can specify which – rows to include in the result through filters (WHERE clause in SQL terminology) – columns to include in the results The result of a query is a table that can be used in other queries (as subqueries). CS1100Microsoft Access10

Creating Queries in Access To create a query: – Select the Create tab – Pick Query Design – Select the tables to include in the query or simply close the dialog and drag the needed tables into the query designer CS1100Microsoft Access11

Running a Query To run a query, click on: To return to the query design, click on: CS1100Microsoft Access12

Example: Simple Query Find the contact id, first name, and last name for each contact. CS1100Microsoft Access13

Removing Duplicates Queries often result in duplicate rows. These are removed by “grouping rows” with the same value as a single row. To do a Group By, follow these steps: – Select the function button in the ribbon – Select “Group By” for each field CS1100Microsoft Access14

Example: Group By Find the dates on which orders were placed. Here’s the result without a Group By: CS1100Microsoft Access15 Note the duplicate rows

Example: Group By Here’s the same query with a Group By: CS1100Microsoft Access16 Note that the duplicate rows have been “collapsed” into groups and only the group is displayed

Duplicates with Group By Group By collapses all rows that contain the same data across all columns. Contacts are appearing multiple times in this example as the rows are not exactly the same. CS1100Microsoft Access17

Joining Tables A “join” is a query operation in which rows are selected that have a common value for some row. – Two columns (each from a different table) can be related if their values represent the same things To find contacts that actually placed an order 1, the ContactID column value must be the same in the Contacts and the Orders table. – This essentially finds all contacts who placed at least one order. CS1100Microsoft Access18 1 There may be contacts in the Contacts table that are not linked to any order, i.e., they never placed an order.

Table Joins In this example, the ContactID in the Contact table and the ContactID in the Orders table represent the same thing so they can be related. CS1100Microsoft Access19 Contacts Table Orders Table

Steps for Joining Tables 1.For every possible combination, take a row from the first table and a row from the second table 2.Remove all the rows that do not have equal values in the related columns 3.Merge the related columns into one column CS1100Microsoft Access20

Step 1 – all combinations CS1100Microsoft Access21

Step 2 – remove rows with unequal values in related columns CS1100Microsoft Access22

Step 3 – merge related columns CS1100Microsoft Access23

Example: Group By and Join Find the first name, last name, and zip code of all contacts that placed an order. Here’s the result without a Group By: CS1100Microsoft Access24 Note the duplicate rows

Example: Group By and Join Find the first name, last name, and zip code of all contacts that placed an order. Here’s the result with a Group By: CS1100Microsoft Access25 All rows with the same first name, last name, and zip code have been collapsed into a single “group”

CS1100Microsoft Access26

Who ordered which products? Unrelated tables -- wrong CS1100Microsoft Access27 Every contact is paired with every product.

Who ordered which products? Correct: CS1100Microsoft Access28

Filtering Selecting rows that meet certain criteria is done through a WHERE clause. CS1100Microsoft Access29 Lists all of the line items (ID only) that have a Quantity > 2.

Selection Criteria Selection criteria are specified as an algebraic relationship, but queries are generally stated as a narrative, so we need to “translate”. CS1100Microsoft Access30 NarrativeAlgebraic Term At least X>= X No more than X< X More than X> X No less than X>= X Less than X< X Up to X< X At most X<= X

Calculated Fields A query can also use calculated expressions. Example: – List the extended prices for each line item. CS1100Microsoft Access31 Note the user defined field name (ExtPrice) and the use of brackets for the field names, e.g., [Quantity].

Aggregate Functions Aggregate functions work on multiple rows. If used within a Group By, they work on the rows within each group. If used on an entire table or query, they work on all of the rows in the table or query. CS1100Microsoft Access32

Aggregate Functions Aggregate FunctionDescription COUNTCounts the rows in a table or a group if used in a Group By SUMAdds the values of the summed field in a table or a group if used in a Group By AVGAverages the values of the field in a table or a group if used in a Group By MINFinds the minimum value of the field in a table or a group if used in a Group By MAXFinds the maximum value of the field in a table or a group if used in a Group By STDEVFinds the standard deviation of the field in a table or a group if used in a Group By CS1100Microsoft Access33

Example: SUM Find the total of each order, i.e., the sum of the extended prices for each line item within an order. CS1100Microsoft Access34 Note the user defined field name (Total) and the use of brackets for the field names, e.g., [Quantity]. The SUM applies to the values of the [Quantity]*[UnitPrice] within each group.

Example: SUM and Filter Find all orders which have a total value of more than $1000. CS1100Microsoft Access35

Counting Items in a Group To count the rows that were collapsed into a group, use the COUNT(*) function. CS1100Microsoft Access36 The Group By collects identical rows into a group. Only the group is displayed, but COUNT counts the number of rows that are in each group. So the above query tells us how many orders each contact placed.

Expressions Note the use of the “Expression” in the Total row for COUNT(*). Expression is often required when an aggregate function is used. Aggregate functions work on the items within a group of the Group By clause: – COUNT – counts items in a group – SUM – adds the items in a group (numbers only) CS1100Microsoft Access37

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

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

TRY FOR YOURSELF… Access Queries CS1100Microsoft Access40

Question 1 How many orders were placed from each state? CS1100Microsoft Access41

Question 1 Solution How many orders were placed from each state? – Find the state from which each order was placed – Group By state and use a COUNT on the group CS1100Microsoft Access42 Answer

Question 2 Which contacts placed three or more orders? CS1100Microsoft Access43

Question 2 Solution Which contacts placed three or more orders? CS1100Microsoft Access44 Answer

Question 3 How often was each product ordered? CS1100Microsoft Access45

Question 3 Solution How often was each product ordered? CS1100Microsoft Access46 Answer (partial)

Question 4 Which products have a total sales volume of at least $10,000, i.e., for which products did the total amount sold across all orders exceed $10,000? CS1100Microsoft Access47

Question 4 Solution Which products have a total sales volume of at least $10,000? CS1100Microsoft Access48 Answer

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 CS1100Microsoft Access49