Download presentation
Presentation is loading. Please wait.
1
Queries
2
Queries Suppose you have a question about something in your data:
Who rented “The Godfather” since we got it? Who currently has the video “Star Wars”? What videos did the user John Doe not return yet? Rather then manually looking through the database, queries will help us answer such questions.
3
What Are Queries? The Query objects can answer question relating the data stored in the tables Retrieve Assemble Filter Sort
4
What Are Queries? (2) Queries can also make calculations over data
Which are the videos with a price less than 4 dollars? Queries can remove data Delete from the database all the videos of the category Action Important data can be lost by mistake. Backup!
5
Creating a Query There are a few different ways to create a query in Access. The simplest is to use the Query Wizard button in the Create tab. It contains some simple pre-defined queries. You can also use the Query Design button.
6
Query Wizard Clicking the Query Wizard button will open the New Query Window and offer a few options.
7
Query Wizard (2) While the Query Wizard has its uses, it is fairly limited. So instead we will show how to use the Query Design button for any type of queries we might need to create.
8
Under “create” we have the option of using either the Query Wizard or the Query design. The query design allows for a much more comprehensive approach.
9
Query Design After clicking the Query Design button, we must select the tables we need for our query. Suppose I just want to have a query that will list the customer names and their phone numbers, I would only need the Customer table.
10
Query Design (2)
11
Query Design (2) Now we need to add the “criteria” for our query.
In the cells at the bottom we can select the fields we want to see by clicking the fields in the table box. We also choose rules we want our query to use. In Field, select Customer Name and Phone Number.
12
Query Design (3) We can test our query by clicking the Run button at the top left. The result of our query will be displayed.
13
Query Design (4) We can then Save our query, and it will appear in our Navigation panel at the left (which also contains the Table list). We can run the query anytime by simply by double-clicking on it.
14
First Name Age Weight Joe Mary Tom Queries: List all the fields of people whose age is under 30.
15
Original table Result of Query
16
Note that the query result looks like a table
Note that the query result looks like a table. If necessary we can make it into a table using a “make table” query.
17
Another query: All the people under 30 And whose weight is over 40.
Age <30 AND Weight > 40 This is an AND between two “criteria” fields. Just put the conditions in each field.
18
Result
19
Another query: list all the people who are between 25 and 30 inclusive.
This is an AND within a “criteria” field.
20
Another query: list all the people who are 25 or older OR who are heavier than 40 .
This is an OR between two fields and uses the OR line..
21
Summary: Within a single field use “And” or “Or” “Joe” Or “Mary” >=35 And <=45 Between fields it is an “And” on the same level. For “Or” use the “Or” levels
22
First name = “Joe” Or ( ( Age>25) AND (Weight <50) )
23
First name = “Joe” Or Age>25 Or Weight <50
24
Calculated fields. You can create a new field in a query which is a result of computations on other fields. Note in the new field put: Age/weight: Round([Age]/[Weight],2) field name formula Note put a check in the field to display it. Note also that you can use functions.
25
Note new field created by query, with given name
Result: Note new field created by query, with given name
26
To concatenate two text fields use +
e.g. Whole Name: [First Name] + [ LastName]
27
Summing a column of numbers:
Also Count, Avg (for computing averages), Min and Max These are called aggregate functions. These perform calculations on columns of data and return a single value. Open your query in datasheet view. In the Home tab Click on the Sigma.
28
Home tab S Totals Click on totals
29
When you click on S (Totals) a new line appears. The arrows give a drop down menu with possible functions for the whole column. To get rid of the extra line click the S (Totals) again
30
Queries on Joined Tables
Consider the following two tables. You wish to ask for a list of the customers with details about the sales staff. Join the tables in a query.
31
We can create a query in which the tables are joined.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.