Microsoft Access Queries Birgül Kutlu. SORTING AND FILTERING Sorting and filtering allow you to view records in a table in different ways such as: reordering.

Slides:



Advertisements
Similar presentations
Database Basics. What is Access? Database management system Computer-based equivalent of a manual database Makes it easy to organize and update information.
Advertisements

Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
EXCEL Spreadsheet Basics
Microsoft Office 2010 Access Chapter 1 Creating and Using a Database.
Chapter 5 Creating, Sorting, and Querying a Table
Microsoft Office 2007 Access Chapter 2 Querying a Database.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Access Project 2 Querying a Database Using the Select Query Window.
Access Tutorial 3 Maintaining and Querying a Database
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.
SUNY Morrisville-Norwich Campus-Week 10 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Querying a Database Using the Select Query Window
Querying a Database Microsoft Office Access 2003.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Access Project 1 Creating and Using a Database.
Chapter 2 Querying a Database
Chapter 3 Maintaining a Database
XP New Perspectives on Microsoft Access 2002 Tutorial 41 Microsoft Access 2002 Tutorial 4 – Creating Forms and Reports.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
Introduction to Access By Mary Ann Chaney and Alicia Harkleroad.
Chapter 2 Querying a Database MICROSOFT ACCESS 2010.
Working with a Database
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.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 4 BACKNEXTEND 4-1 LINKS TO OBJECTIVES Query Design Query Criteria Modify a Query Using OR.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
Analyzing Data For Effective Decision Making Chapter 3.
© 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 6 – Designing.
Microsoft Access 2000 Presentation 2 Creating Databases Part I (Creating Tables)
XP New Perspectives on Microsoft Office Access 2003 Tutorial 9 1 Microsoft Office Access 2003 Tutorial 9 – Using Action Queries, and Defining Table Relationships.
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
Microsoft Office 2007 Access Chapter 2 Querying a Database.
XP 1 Microsoft Access 2003 Introduction To Microsoft Access 2003.
MS-ACCESS BY SANGEETHA PARTHASARATHY Topics to be covered §Comparing Values in Selection Criteria §Calculating Values in a Query §Changing the appearance.
® Microsoft Office 2010 Access Tutorial 3 Maintaining and Querying a Database.
Database Systems Microsoft Access Practical #1 Creating Tables Nos 215.
Analyzing Data Using Access. Creating a new database To create a new database 1.Start Access. In the Task Pane, click Blank Database. 2.The File New Database.
Database Systems Microsoft Access Practical #3 Queries Nos 215.
Microsoft Office 2007 Access Chapter 3 Maintaining a Database.
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.
Copyright 2008 McGraw-Hill Ryerson 1 TECHNOLOGY PLUG-IN T7 PROBLEM SOLVING USING ACCESS.
BUSINESS DRIVEN TECHNOLOGY Plug-In T5 Touring Access.
Course ILT Forms and queries Unit objectives Create forms by using AutoForm and the Form Wizard, and add or modify form headers and footers Open and enter.
Microsoft Access. Microsoft access is a database programs that allows you to store retrieve, analyze and print information. Companies use databases for.
1 After completing this lesson, you will be able to: Create and run a query. Specify criteria in a query. Create a calculated field. Create a multiple-table.
McGraw-Hill/Irwin © 2008 The McGraw-Hill Companies, All Rights Reserved Plug-In T7: Problem Solving Using Access 2007 Business Driven Technology.
Microsoft Access 2000 Presentation 3 Creating Databases Part II (Creating Forms)
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.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
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.
XP New Perspectives on Microsoft Access 2002 Tutorial 31 Microsoft Access 2002 Tutorial 3 – Querying a Database.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
Query Lab CSC 240 Blum1. Log on to PMA (PHPMyAdmin) and click on the Northwind database CSC 240 Blum2.
Microsoft Access 2000 Presentation 5 Creating Databases Part IV (Creating Queries)
Key Applications Module Lesson 22 — Managing and Reporting Database Information Computer Literacy BASICS.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
Microsoft Access Lesson 5 Lexington Technology Center February 25, 2003 Bob Herring On the Web at
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Access Project 2 Querying a Database Using the Select Query Window.
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.
Microsoft Access By Ritesh Sharma. Introduction Microsoft Access is a desktop database program that enables you to enter, store, analyze,and present data.For.
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.
COMPUTER SKILLS MS-ACCESS. Introduction Access is a piece of software known as a database management system. At its most basic level, it can be used to.
Databases Databases Using Microsoft Access 2003 Microsoft Access 2003 is a powerful, yet easy to learn, relational database application for Microsoft Windows.
Access Chapter 2 Querying a Database.
Plug-In T7: Problem Solving Using Access 2007
Microsoft Office Access 2003
Creating and Modifying Queries
Tutorial 3 – Querying a Database
Microsoft Office Access 2003
Presentation transcript:

Microsoft Access Queries Birgül Kutlu

SORTING AND FILTERING Sorting and filtering allow you to view records in a table in different ways such as: reordering all of the records in the table (sorting by date or alphabetical order) view only those records in a table that meet certain criteria that you specify (people in İstanbul).

SORTING Follow these steps to execute a simple sort of records in a table based on the values of one field: In table view, place the cursor in the column that you want to sort by. Select Records|Sort|Sort Ascending or Records|Sort|Sort Descending from the menu bar or click the Sort Ascending or Sort Descending buttons on the toolbar. To sort by more than one column (such as sorting by date and then sorting records with the same date alphabetically), highlight the columns by clicking and dragging the mouse over the field labels and select one of the sort methods stated above. Priority is given to the leftmost field.

FILTER BY SELECTION This feature will filter records that contain identical data values in a given field such as filtering out all of the records that have the value in a name field. To Filter by Selection, place the cursor in the field that you want to filter the other records by and click the Filter by Selection button on the toolbar or select Records|Filter|Filter By Selection from the menu bar.

TEXT AFFECTED BY Selected TextFilter Effect Entire fieldDisplays only records whose fields contain exactly matching values Characters anywhere in field Displays records in which any part of the field matches the selected text (If first character is selected, records that are starting with the selected character will be displayed)

FILTER BY FORM This method creates a blank version of the table with drop- down menus for each field that each contain the values found in the records of that field. Under the default Look for tab of the Filter by Form window, click in the field to enter the filter criteria.

FILTER BY FORM Specify more than one criteria for “and” operation. To specify an alternate criteria if records may contain one of two specified values, click the Or tab at the bottom of the window and select another criteria from the drop-down menu. More Or tabs will appear after one criteria is set to allow you to add more alternate criteria for the filter. After you have selected all of the criteria you want to filter, click the Apply Filter button on the toolbar.

METHODS The following methods can be used to select records based on the record selected by that do not have exactly the same value. Type these formats into the field where the drop-down menu appears instead of selecting an absolute value. FormatExplanation Like "*Street"Selects all records that end with "Street" <="G"Selects all records that begin with the letters A through G >1/1/00Selects all dates since 1/1/00 <> 0Selects all records not equal to zero

SAVING A FILTER The filtered contents of a table can be saved as a query by selecting File|Save As Query from the menu bar. Enter a name for the query and click OK. The query is now saved within the database. To view all records in a table again, click the depressed Apply Filter toggle button on the toolbar.

QUERIES Queries select records from one or more tables in a database so they can be viewed, analyzed, and sorted on a common datasheet. The resulting collection of records, called a dynaset (short for dynamic subset), is saved as a database object and can therefore be easily used in the future.

QUERIES The query will be updated whenever the original tables are updated. Types of queries are select queries that extract data from tables based on specified values, find duplicate queries that display records with duplicate values for one or more of the specified fields, and find unmatched queries display records from one table that do not have corresponding values in a second table.

QUERY WIZARD Click the Create query by using wizard icon in the database window to have Access step you through the process of creating a query.

QUERY WIZARD From the first window, select fields that will be included in the query by first selecting the table from the drop-down Tables/Queries menu. Select the fields by clicking the > button to move the field from the Available Fields list to Selected Fields. Click the double arrow button >> to move all of the fields to Selected Fields. Select another table or query to choose from more fields and repeat the process of moving them to the Selected Fields box. Click Next > when all of the fields have been selected.

QUERY WIZARD On the next window, enter the name for the query and click Finish.

CREATE QUERY From the Queries page on the Database Window, click the New button. Select Design View and click OK.

CREATE QUERY Add fields from the tables to the new query by double- clicking the field name in the table boxes or selecting the field from the Field: and Table: drop-down menus on the query form. Specify sort orders if necessary.

CREATE QUERY Enter the criteria for the query in the Criteria: field. The following table provides examples for some of the wildcard symbols and arithmetic operators that may be used. The Expression Builder can also be used to assist in writing the expressions. After you have selected all of the fields and tables, click the Run button on the toolbar. Save the query by clicking the Save button.

CRITERIA OperatorExplanation ? StreetThe question mark is a wildcard that takes the place of a single letter. 43th *The asterisk is the wildcard that represents a number of characters. < 100Value less than 100 >=1Value greater than or equal to 1 <>"FL"Not equal to (all states besides Florida) Between 1 and 10Numbers between 1 and 10

CRITERIA OperatorExplanation Is Null Is Not Null Finds records with no value or all records that have a value Like "a*"All words beginning with "a" >0 And <=10All numbers greater than 0 and less than 10 "Bob" Or "Jane"Values are Bob or Jane

Calculated Fields Use Customers, Orders and Order details tables. Select CompanyName, PostalCode and Country fields from Customers. Select OrderID (sort as ascending), OrderDate, ShippedDate and Freight fields from Orders. Write the following expression to an empty field: Amount: CCur([UnitPrice]*[Quantity]*(1-]Discount]) Click Totals button and select Sum from Totals for this field. Save as qryOrderAmount. CCur: Converts Numeric value to Currency

Query Categories of products purchased by each customer: We need Customers, Products and Categories tables, but because there is relationships all tables have to be included. Use Customers, Orders, Order details, Products and Categories tables. Select CompanyName and CategoryName fields. Write the following expression to an empty field: Amount: CCur([Order Details].[UnitPrice]*[Quantity]*(1- ]Discount])) Click Totals button and select Sum from Totals for this field. Save as qryOrdersByCompanyAndCategory.

Inner Join Query Name of Customers who have same billing and shipping addresses: Use Customers and Orders tables. Select CompanyName, Address and ShippingAddress fields. Join Address to Shipping Address. In order to eliminate duplicates from Properties set Unique Values to “Yes”. Save as qrySameAddress.

Outer Join Query Outer join is used to display all records regardless of corresponding records existing in joined table. By default if an item in primary table has no records in many table it is not displayed. Left: All records with unique primary key are displayed when does not exist in many table. Right: All records in many are displayed regardless of whether they exist in primary

Outer Join Query Display all employees and orders: Open Employee table. Add a new record to that table. Use Employee and Orders table for the query. Select LastName and OrderID fields. Double click on the link. Select item 2 which is left join. Item 3 is right join. Save as qryLeftJoin. In order to do Right Join add a record to “Order” table.

Find Duplicates Query This query will filter out records in a single table that contain duplicate values in a field. Click the New button on the Queries database window, select Find Duplicates Query Wizard from the New Query window and click OK.

Find Duplicates Query Select the table or query (Customers) that the find duplicates query will be applied to from the list provided and click Next >.

Find Duplicates Query Select the fields that may contain duplicate values (Region) by highlighting the names in the Available fields list and clicking the > button to individually move the fields to the Duplicate-value fields list or >> to move all of the fields. Click Next > when all fields have been selected.

Find Duplicates Query Select the fields that should appear in the new query along with the fields selected on the previous screen (CustomerName) and click Next >.

Find Duplicates Query Name the new query (qryFindDuplicatesForCustomersInSameRegion) and click Finish. Delete a Query: To delete a table from the query, click the table's title bar and press the Delete key on the keyboard.

Find Unmatched Query Creates a query that finds records in one table that have no related records in another table. Click the New button on the Queries database window, select Find Unmatched Query Wizard from the New Query window and click OK. We will find customers with no orders. Select the table or query (Customers) that “find unmatched query” will be applied to from the list provided and click Next >.

Find Unmatched Query Select the table or query (Orders) that will have no related records with the first table and click Next >. Select the fields that should appear in the new query (CustomerName, City, region, Country) and click Next >. Name the new query (qryCustomersWithoutMatchingOrders) and click Finish.

Crosstab Query Display quarterly average prices by products for 1997: Crosstab query requires only one table. If more than one table is to be used then a query has to be formed first. Select Order and Order Details tables. Select OrderDate, ProductID and UnitPrice fields. Specify criteria for the date as: Between # # and # # Save as qrySource.

Crosstab Query Cont’d. Display quarterly average prices by products for 1997: Click New button and select Crosstab query. Select from Queries tab qrySource. Select productID for the rows, OrderDate for the columns, specify quarters, select UnitPrice as average. Save as qry1997QtrSalesByProducts.

Pivot Table Display quarterly values of orders by salesperson and country for 1997: Select Employee, Order tables and OrderSubtotal query. Select LastName, ShipCountry, OrderDate and Subtotal fields. Specify criteria for the date as: Between # # and # # Add a new field to specify quartes as: 1997 Quarter: Format([OrderDate]; “q”) Hide OrderDate Save as qry1997OrdersByCountryPT.

Pivot Table Cont’d. Select View menu and Pivot Table View. Drag Quarter to column area, Name to row area, Orders to Total area. Drag Country to row area after Name. Right click, select AutoCalc and Sum. This will add subtotals to the pivot tabel. You can export this to Microsoft Excel by clicking “Export to Microsoft Excel” button.

Pivot Chart Use qry1997OrdersByCountryPT query. Select “Pivot Chart View” from “View” menu. Using Right Click and Properties, you can change the chart title, axis titles, legends and chart type.

Select Query Use Customers table. Select ContactName, CompanyName, Address, City, Region, PostalCode and Country fields. For Country field use “USA” for criteria. Save as qryUSMailList. Right click on gray area to access to query properties. Change RecordSet type: Dynaset: Query is dynamic, when changes are made, table is also affected. Snapshot: Query is static, no append or edit can be done.

Make-Table Query Use qryUSMailList. From Query Menu select MakeTable, when it asks for table name give as “tblUSMailList”. When you run the query, this will create a table of the query result. This is a snapshot. When you run it again, it is going to delete the old one.

Append Query To change a select query to an append query, choose Append Query from the Query type button's list. In the Append Query dialog box, you will specify to which table you want to append records. The table to be appended has to be created and has to have the same fields as the query. The Append To row will be displayed in the design grid indicating the fields that the query will append to the table.

Append Query Copy “Employees” table and paste as “EmployeeUSA”. Delete all records. Use “Employee” table to create a query. Select * and Country fields. Set “Append To” for * field to “EmployeeUSA.*”. Set criteria for Country field to USA. Run the query. It will append 5 rows to the table. If you run the query because of the key it is not going to append, you have to delete all records again.

Delete Query A delete query is used to delete records you no longer need in your database. To create a delete query, first create and run a select query in Design View, selecting the items you wish to delete. If the select query displays only the records you wish to delete, click the list arrow for the Query Type button in design view and then click Delete Query. Run the query to delete the records you previously selected.

Delete Query

Austrian authorities have declared Ernst Handel insolvent and any orders which are not shipped has to be deleted. Copy [Orders] table and paste it as [OrdersCopy]. Copy [Order Details] table and paste it as [OrderDetailsCopy]. Create relationships. Use OrdersCopy table to create a query. Select *, CustomerID and ShippedDate fields. Add “ERNSH” criteria for CustomerID field.

Delete Query Add “Is Null” criteria for Shippeddate field. Run query. Choose “Delete query” from “Query” menu. Sort and Show rows will be replaced by Delete row. Click Run. Check your files. There should be 2 rows deleted from OrdersCopy table and 7 records deleted from OrderDetailsCopy.

Update Query Create a select query with the necessary fields and selection criteria and click the Run button to preview the results. Click the list arrow for the Query Type button and select Update Query. Type the update values for the fields you need to update in their Update To text boxes. Click the Run button on the Query Design toolbar. Click the Yes button to confirm updating the records.

Update Query

Company decides to give 2% more discount to all customers. Copy [Order Details] table and paste it as [OrderDetailsCopy2]. Create query from OrderDetailsCopy2 table. Select Discount field. Choose “Update Query” from “Query” menu. Sort and Show rows will be replaced by Update To row. Change Update to row to [Discount] + 0,02. Click Run.

Union Query Close the Show Table dialog, choose Query, SQL Specific, Union. SQL window will open and you should type the SQL statement. Click the New button on the Queries database window, select Design View from the New Query window and click OK. Union queries let you combine the result set of two or more SELECT queries into a single result set. Union queries can only be created by SQL statements

Union Query Customers and Suppliers by City SQL Statement: SELECT City, CompanyName, ContactName, "Customers" AS [Relationship] FROM Customers UNION SELECT City, CompanyName, ContactName, "Suppliers“ FROM Suppliers ORDER BY City, CompanyName;

Sub Query Instead of using two table values and criterias you can write SELECT queries that use another SELECT query to supply the criteria for the WHERE clause. In order to display names and addresses of customers who placed orders between January 1, 1997 and June 30, 1997, we can choose only Customers table, select CompanyName, ContactName, Address and CustomerID fields.

Sub Query And we will write the criteria for the CustomerID field. IN (SELECT CustomerID FROM Orders WHERE OrderDate Between #1/1/1997# and #6/30/1997#) Save query as qrySubquery.