Designing a Simple Query


Introduction

The real power of a relational database is in the ability to quicklyretrieve and analyze your data by running a query. Queries allow you to pull information from one or more tables based on a set of search conditions you define. In this lesson, you will learn how to create a simple one-table query.

Throughout this course, we will be using a sample database. If you would like to follow along, you'll need to download our Access 2013 sample database. You will need to have Access 2013 installed on your computer in order to open the example.

What are queries?

Queries are a way of searching for and compiling data from one or more tables. Running a query is like asking adetailed question of your database. When you build a query in Access, you are defining specific search conditions to find exactly the data you want.

How are queries used?

Queries are far more powerful than the simple searches or filters you might use to find data within a table. This is because queries can draw their information from multiple tables. For example, while you could use a search in the customers table to find the name of one customer at your business or a filter on the orders table to view only orders placed within the past week, neither would let you view both customers and orders at once. However, you could easily run a query to find the name and phone number of every customer who's made a purchase within the past week. A well-designed query can give information you might not be able to find out just by examining the data in your tables.

When you run a query, the results are presented to you in a table, but when you design one you use a different view. This is called Query Design view, and it lets you see how your query is put together.

Click the buttons in the interactive below to learn how to navigate the Query Design view.

 

One-table queries

Let's familiarize ourselves with the query-building process by building the simplest query possible: a one-table query.

We will run a query on the Customers table of our bakery database. Let's imagine that our bakery is having a special event and we want to invite our customers who live nearby, since they are the most likely to come. This means we need to see a list of all customers who live close by, and only those customers.

If you think this sounds a little like applying a filter, you're right. A one-table query is actually just an advanced filter applied to a table.

To create a simple one-table query:

  1. Select the Create tab on the Ribbon, and locate the Queries group.
  2. Click the Query Design command.
    Screenshot of Access 2013
  3. Access will switch to Query Design view. In the Show Table dialog box that appears, select the table you would like to run a query on. We are running a query about our customers, so we'll select the Customerstable.
    Screenshot of Access 2013
  4. Click Add, then click Close.
  5. The selected table will appear as a small window in the Object Relationship pane. In the table window, double-click the field names you would like to include in your query. They will be added to the Design Gridin the bottom part of the screen. 

    In our example, we want to mail invitations to customers who live in a certain area, so we'll include the FirstName, Last NameStreet AddressCity, and Zip Code fields. We aren't planning on calling or emailing our customers, so we don't have to include the Phone Number or Email fields.
    Screenshot of Access 2013
  6. Set the search criteria by clicking on the cell in the Criteria: row of each field you would like to filter. Typing criteria into more than one field in the Criteria: row will set your query to include only results that meet all criteria. If you want to set multiple criteria but don't need the records shown in your results to meet them all, type the first criteria in the Criteria: row and additional criteria in the or: row and the rows beneath it.

    For this one-table query, we'll use simple search criteria.
    • We want to find our customers who live in the city of Raleigh, so in our City field we'll type "Raleigh". Typing "Raleigh" in quotation marks will retrieve all records with an exact match for "Raleigh" in the City field.
    • Some customers who live in the suburbs live fairly close by, and we'd like to invite them as well. We'll add their zip code, 27513, as another criteria. Because we want to find customers who either live in Raleigh orin the 27513 zip code, we'll type "27513" in the or: row of the Zip Code field.
    Screenshot of Access 2013
  7. After you have set your criteria, run the query by clicking the Run command on the Design tab.
    Screenshot of Access 2013
  8. The query results will be displayed in the query's Datasheet View, which looks like a table. If desired, saveyour query by clicking the Save command in the Quick Access Toolbar. When prompted to name it, type in the desired name, then click OK.
    Screenshot of Access 2013

Now you know how to create the simplest type of query with only one table. In the next lesson, you'll learn how to create a query that uses multiple tables.


Advertisements