Queries in Base Notes Readers Venue - Compressed
Queries in Base Notes Readers Venue - Compressed
Using a query, we can retrieve and display data from one or more tables in a
database.
Using a query, we can specify the fields that we want to display and also the
criterion based on which the records to be filtered.
The result of the query is displayed in tabular form.
Creating a Query
A query can be created in three ways.
1. Using a Wizard
2. In Design View
3. In SQL view
Creating a Query Using a Wizard
To execute query, we wil use two tables- Events and EventCategory.
We will create a query that will display the Event Name and Winner for each
event with CategoryID as C001.
Step 1 In the Database Design window, click on Queries button present in the
Database Pane on the left.
Step 2 In the Tasks Area, click on Use Wizard to Create Query Option.
The Query Wizard window will appear. It contains the Steps Pane on the
left and the Query Details Area on the right.
Step 3 The first step of the wizard is to select fields from the respective tables.
Select Events table from the Tables list box.
Step 4 Select Event Name, Winner and CategoryID field from the list box and click
the right arrow (>) button to move all fields to Fields in the Query box.
Step 5 Clicking on the Next button will display the screen to select the sorting
order. The result of the query can be displayed in ascending or
descending order of any particular field of the table.
If we do not want any particular order, we can directly click on Next.
Step 6 The next step is to set the search conditions or the criteria on the basis of
which records will be filtered from the table.
Step 7 Select CategoryID field from Fields drop down list, is equal to from
Condition drop down list and type the value as C001. Click Next.
[Three search conditions can be given at the most in the wizard.]
Step 8 If we want to display a different name instead of the original column
name, we can set it here.
Step 9 The last step of the Query wizard displays the entire overview of the
query. It displays the overall structure of the query.
Name of the Query - By default, the name of the query is Query_Events
by default. If desired, type the new name in the text box.
The action to be performed after the wizard finishes- By default
Display Query option will be selected.
Click and select the Modify Query radio button if the query has to be
edited in the Design view.
Step 10 Click on Finish button. The records with CategoryID as C001 will be
displayed on the screen.
Creating a Query in Design View
We will create a query to display records of Athletics category. For this query,
records have to be filtered from both Events and EventCategory tables.
Step 1 Click Queries icon on the Database Pane in the Database Window.
Step 2 Click Create Query in Design View… icon in the Tasks Pane. The Query
Design Window appears. In the middle of the window the Add Table or
Query dialog box is displayed.
Step 3 Click on the Event table to be used in the query and then click on Add
button. Alternatively double click on the Events table. The Event table
will be added to the Tables Pane.
Step 4 Similarly add the EventCategory table.
Step 5 Click Close button in the Add Table or Query dialog box to close it.
Step 6 Next step is to select the fields. For our query we want to display Event
Name and Winner from the Events table and Category Name from the
EventCategory table.
So in the list box of Events table, double click on EventName and Winner
field, and they will be displayed in the Design grid.
Note:-Observe that the Visible Check Box is selected by default. This means
that all these three fields will be visible when you run the query.
If you do not want the data values for the particular field to be
displayed, click to deselect the respective check box.
Step 7 Next we will set Alias names for the columns that will be displayed in the
output. For example, to change Winner -> Winner Name, type Winner
Name in the Alias text box under Winner column.
Step 8 To sort the records in either ascending or descending order of a
particular field, use the Sort row given in the grid.
Step 9 To display records of Athletics category only, type ‘Athletics’ in the
Criterion row under the Category Name column.
Step 10 Once the query is designed, click Run Query ( ) button on the toolbar or
press F5 key. The query result will be displayed in the Tables Pane area.
Step 11 Click on Save button to save the query. The Save As dialog box will be
displayed. By default, the Query Name as Query1 will be displayed. Type
a different name if required. Click on OK button to save the query.
Editing a Query
Step 1 Right click on the Query Name in the Objects Area of the Database
window.
Step 2 Select Edit option from the popup menu.
Step 3 Make the desired changes and click the Save button.
Note:- We can use the Criterion row to apply multiple conditions. We can apply
all relational operators like <, >, <=, >=, != and = for all conditions that can
be given in Criterion row.
For example, if you want to see only those records where points scored
are more than 10, then add Points field to the grid and then set the
Criterion for it as >10.
Working with Numerical Data
Till now, we have been displaying data from tables in the query.
We can even use certain mathematical functions to find the count, sum,
minimum, maximum or average of data values.
Step 1 Click Create Query in Design View. Add Events and EventsCategory
tables.
Step 2 Add Category Name field from EventCategory table and Points field
from Events table.
Step 3 Under the Category Name field, in the Function row, select Group option
from the drop down list.
Step 4 Similarly, under the Points column, select the Average function from the
drop down list.
Step 5 Press F5 to run the query. The query result depicting average points in
all the categories will be displayed.