In a query with multiple sort fields

Access 2010 offers many options that let you design and run queries that return exactly the information you're looking for. For instance, what if you need to find how many of something exists within your database? Or what if you want your query results to automatically be sorted a certain way? If you know how to use its query options, you can design almost any query you want in Access.

In this lesson, you'll learn how to modify and sort your queries within Query Design view, as well as how to use the Totals function to create a query that can perform calculations with your data. You'll also learn about additional query-building options offered in Access.

We will be showing you how to design and run queries with examples from our sample database. If you would like to follow along, download our example and use it to follow the procedures demonstrated in this lesson.

Modifying queries

Access offers many options for making your queries work better for you. In addition to modifying your query criteria and joins after you build your queries, you can choose to sort or hide fields in your query results.

To modify your query:

When you open an existing query in Access, it is displayed in Datasheet view, meaning you will see your query results in a table. To modify your query, you must enter Design view, the view you used when creating it. There are two ways to switch to Design view:

  • On the Home tab of the Ribbon, click the View command. Select Design View from the drop-down menu that appears.

    In a query with multiple sort fields
    Switching to Design View with the View command on the Ribbon

  • In the bottom-right corner of your Access window, locate the small view icons. Click the Design view icon, which is the icon farthest to the right.

    In a query with multiple sort fields
    Switching to Design View using the View Icon

Once in Design view, make the desired changes, then select the Run command to view your updated results.

You may notice that Access offers other query views, like Pivot Table View, Pivot Chart View, and SQL View. You can ignore them; these views permit advanced functions you will not need to use for this tutorial or for most Access functions.

Sorting queries

Access allows you to apply multiple sorts at once while you're designing your query. This allows you to view your data exactly the way you want, every single time you view it.

A sort that includes more than one sorted field is called a multilevel sort. A multilevel sort allows you to apply an initial sort, then further organize data with additional sorts. For instance, if you had a table full of customers and their addresses, you might choose to first sort the records by city, then further sort them alphabetically by last name.

When more than one sort is included in a query, Access reads the sorts from left to right. This means the leftmost sort will be applied first. In the below example, customers will be sorted first by the City they live in and then by the Zip Code within that city.

In a query with multiple sort fields
A multilevel sort. The records will be sorted by City first.

To apply a multilevel sort:

  1. Open the query and switch to Design view.
  2. Locate the field you want to sort first. In the Sort: row, click the drop-down arrow to select either an ascending or descending sort.

    In a query with multiple sort fields
    Applying an Ascending sort to a field

  3. Repeat the process in the other fields to add additional sorts. Remember, the sorts are applied from left to right, so any additional sorts must be applied to fields located to the right of your primary sort. If necessary, you can rearrange the fields by clicking a field and dragging it to a new location.

    In a query with multiple sort fields
    A multilevel sort

  4. To apply the sort, click the Run command.

    In a query with multiple sort fields
    The Run Query command

  5. Your query results will appear with the desired sort.

    In a query with multiple sort fields
    The sorted query results

You can also apply multilevel sorts to tables that don't have queries applied to them. From the Home tab on the Ribbon, select the Advanced drop-down command in the Sort & Filter group. Select Advanced Filter/Sort, and create the multilevel sort as you normally would. When you're finished, click the Toggle Filter command to apply your sort.

In a query with multiple sort fields
The Advanced Filter/Sort command

Hiding fields within queries

Sometimes you might have fields that contain important criteria, but you might not need to actually see the information from that field in the final results. For example, take one of the queries we built in our last lesson: a query to find the names and contact information of customers who had placed orders. We included order ID numbers in our query because we wanted to make sure we only pulled customers who had placed orders.

However, we really didn't need to see that information in our final query results. In fact, if we were just looking for customer names and addresses, seeing the order number mixed in there too might have even been distracting. Fortunately, Access makes it easy to hide fields while still including any criteria they contain.

To hide a field within a query:

  1. Open the query and switch to Design view.
  2. Locate the field you want to hide.
  3. Click the check box in the Show: row to uncheck it.

    In a query with multiple sort fields
    Unchecking a field to hide it

  4. To see the updated query, select the Run command. The field will be hidden.

To unhide a hidden field, simply return to Design view and click the check box in the field's Show: row again.

More types of queries

By this point, you should understand how to create a simple or multi-table query using multiple criteria. Additional queries offer you the ability to perform even more complex actions with your database. One of these is the totals query, which lets you perform calculations with your data.

Totals queries

Sometimes setting simple criteria won't give you the results you need, especially when you're working with numbers. You may want to see your query results grouped or counted in some way. Access 2010 offers several options that make these functions possible. Perhaps the easiest of these is the Totals command.

When you use the Totals function in your query, the data in your fields will be grouped by value, meaning all items of one type are listed together. For instance, in a totals query on the items sold at our bakery, each type of item sold would be listed on a single row, no matter how many times that item had been sold.

Once your records are grouped, you can perform calculations with them. These calculations include:

  • Count, which counts the number of the same items in a field
  • Sum, which adds the numbers in that field
  • Average, which finds the average of the numbers that occur in that field
  • Maximum, which returns the highest value that has been entered in that field
  • Minimum, which returns the lowest value that has been entered in that field
  • First, which returns the first—or earliest—value that has been entered in that field
  • Last, which returns the last—or most recent—value that has been entered in that field

These calculations will apply to the rows containing your grouped items. For example, if you decided to use Sum to find out how many of each item on a menu has been ordered, you would get a subtotal for each item in your query rather than a grand total of all of the items combined.

In a query with multiple sort fields

To add a calculation like a grand total to your query or table, review the instructions for creating a Totals row in our Modifying Tables lesson.

To create a totals query:

  1. Create or open a query you want to use as a totals query. For our example, we want to find the total number we've sold of each of our menu items, so we'll use a query showing us all of the menu items we've sold. If you want to follow along in our database, open the Menu Items Ordered query.
  2. In the Query Design tab, locate the Show/Hide group, then select the Totals command.

    In a query with multiple sort fields
    The Totals Command

  3. A row will be added to the table in the design grid, with all values in that row set to Group By. Select the cell in the Total: row of the field you want to perform a calculation on, and click the drop-down arrow that appears.

    In a query with multiple sort fields
    Selecting the totals row of the field we want to perform a calculation on

  4. Select the calculation you want to be performed in that field. In our example, we want to add the quantities of products we've sold, so we'll select the Sum option.

    In a query with multiple sort fields
    Setting the Totals calculation to Sum

  5. When you are satisfied with your query design, select the Run command on the Query Tools Design tab to run the query.

    In a query with multiple sort fields
    The Run Query command

  6. The query results will be displayed in the query's Datasheet view, which looks like a table. If you want, save your query by clicking the Save command in the Quick Access toolbar. When prompted to name it, type the desired name, then click OK.

    In a query with multiple sort fields
    Saving the totals query. Note the sums in the far-right field.

More query options

We offer shorter lessons on creating additional types of queries in our Extras section. Below is a list of the queries we currently cover.

How can you perform sorting on multiple columns?

Sort data in a table.
Select Custom Sort..
Select Add Level..
For Column, select the column you want to Sort by from the drop-down, and then select the second column you Then by want to sort. ... .
For Sort On, select Values..
For Order, select an option, like A to Z, Smallest to Largest, or Largest to Smallest..

Can you sort query results by more than one column?

To sort query results by more than one column In the Criteria pane, click the Sort Order field for the column upon which you want to sort. Type an integer or select an integer from the drop-down list to indicate this column's place in the order of sorted columns.

Can you sort multiple columns in power query?

You can sort a table in Power Query by one column or multiple columns.

Is it possible to sort multiple columns in a table?

There is no limit on the number of columns that can be used to sort a table. Just hold the shift key and keep choosing columns. – This feature is not available for matrices. – To switch the sorting from ascending to descending or vice-versa continue to hold shift and click on the column header again.