Está en la página 1de 8

QUERIES USING RELATIONAL/COMPARISON OPERATORS IN ACCESS 2007 If you want to practice using a database you can do so by completing the

following activities. If you would prefer to use the skills on your own database read the instructions and apply them to your work. You will use the database Southsea Leisure. A copy of this can be found by clicking on the link Southsea Leisure. Save the database to your computer and open it from there. This topic will build on skills developed in the topics Simple Database Queries and Multiple Search Queries. If you do not have much experience of designing queries it would be useful to look at these topics first. What are relational/comparison operators? Relational operators (also known as comparison operators) allow you to compare the values of data. = <> < <= > >= Equal to 2 = 2 If you were searching the database for all products costing 20 you would use the comparison operators =20. Not equal to 3 <> 4 If you were searching the database for all products that did not cost less than 20 you would use the comparison operator <>20 Less than 5 < 9 If you were searching the database for all products that cost less than 20 you would use the comparison operator <20 Less than or equal to If you were searching the database for all products costing 20 or less you would use the comparison operator <=20 Greater than 4 > 2 If you were searching the database for all products that cost more than 20 you would use the comparison operator >20 Greater than or equal to If you were searching the database for all products that cost 20 or more you would use the comparison operator >=20

When would you use relational/comparison operators? You would use a relational/comparison operator if you needed to find all data values that are equal to, greater than, less than or not equal to a certain amount. Using the equal to operator The Leisure Centre wants to find all of the members in Membership Category 5 so that they can target them with a special offer. The Membership Category field has a number data type so it needs to be searched using an operator to find the number required.

1. Open the Southsea Leisure database as described above. If you have used it for previous activities it will be saved on your computer. 2. Click on the Create tab. 3. Click on the Query Design button.

This will open the Show Table dialogue-box.

4. Click on the Membership table to select it. 5. Click on the Add button to add it to the new query. 6. Click on the Close button to close the dialogue-box. A table icon will appear in the top left-hand corner of the query window (see overleaf). 7. Double-click on the field name Membership No on the table icon to add it to the query. 8. Repeat step 7 to add the fields: Category No Last name First name

Table icon

9. To find all of the members who are in Category 5 click in the Criteria: row in the Category No column and type =5 and press the Return key on your keyboard. 10. Click on the Run button to run the query and see the results.

You can see that three members are in Category 5. 11. Save the query with an appropriate name, e.g. Category 5. Do this using the Office button in the top left-hand corner of the screen and the Save As command, then select the option Save Object As Using the less than operator Southsea Leisure wants to target its members who are under the age of 24 to see what activities they are interested in. 1. With the database Southsea Leisure open follow steps 1 to 6 above. 2. Add the following fields to the query as described in step 7 above. Membership No Last name First name Street Town County Post Code Age

3. To find all of the members who are under 24 years of age click in the Criteria row in the Age field and type <24 and press the Return key on your keyboard.

4. Click on the Run button as illustrated in step 10 above to view the query results.

You will see that there are four members under the age of 24. 5. Save the query with an appropriate name as described in step 11 above. Using the greater than operator Southsea Leisure is thinking of starting a programme for members who are over 50 years of age and would like to see how many would be interested. 1. With the database Southsea Leisure open follow steps 1 to 6 above (under the heading Using the equal to operator). 2. Add the following fields to the query as described in step 7 above: Membership No Last name First name Street Town County Post Code Age 3. Click in the Criteria: row in the Age field column. 4. Type in >50, the greater than operator tells the query that you are looking for people who are over 50 years of age, not including people who are 50 years of age. 5. Use the Run button as described above to run the query. 5

You will see that there are three members who are over 50 years of age. 6. Save the query with an appropriate name. Using the equal to and greater than/less than operators Southsea Leisure wants to find all of their members who are aged 40 or more. 1. Start a new query as described above. 2. Add the following fields to the query: Membership No Last name First name Street Town County Post Code Age 3. In the Criteria row of the Age field enter >=40, this is telling the query to find all members who are aged 40 or more. 4. Run the query using the Run button as described above.

You can see that the result shows those who are 40 and those who are more than 40. 5. Save the query with an appropriate name. 6

6. Change the query to read <=40 which means less than or equal to 40. 7. Run the query.

Now the query shows all those who are aged 40 or less. 8. Save the query with a new name that is appropriate for the new query. This can be done in exactly the same way as saving a query for the first time, but do not just click on the Save button as this will overwrite the original query. Using the not equal to operator You can use the not equal to operator to find any data that is not equal to a particular value. Southsea Leisure wants to find all members who live in Southsea so that they can see how far people are willing to travel to use their facilities. 1. With the Southsea Leisure database open start a new query as described above. 2. Add the following fields to the query: Membership No First name Last name Street Town Post Code 3. In the Criteria: row of the Town column type <>Southsea. You are asking the query to find all the members whose town of residence is not Southsea. 4. Run the query using the Run button as described above.

All of the members who do not live in Southsea are identified. 5. Save the query with an appropriate name. OTHER TOPICS IN ACCESS 2007: Introduction to databases in Access 2007 Sorting a database table in Access 2007 Simple database queries in Access 2007 Multiple search queries and wildcards in Access 2007 Using And and Or operators in Access 2007 Producing reports based on queries in Access 2007 Adding a header or footer to a report in Access 2007 Constructing a query using more than one table

También podría gustarte