Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Database

AS 91892 4 credits

Project 4 –
Queries

By J Murfitt
AS 91892 Database 4 credits

Project 4 –Queries
Project 4 –Queries
Database

Learning Intentions (Project Overview)


In this project you will learn:
 How to create queries.
 How to use wildcards in queries.
 How to use multiple criteria.
 How to calculate things in a query (like someones age).
 The different between AND OR.
 How to sort queries.
 Practice making queries.

This project should take you 1 to 1 ½ classes to complete.

Quick Summary
Here is a quick summary of the tasks in this project. If you know what to do, just get on with it.
Otherwise, you will need to read the instructions on the following pages.

 Create a query to find the guy from Facebook called Mark. Display his mobile number. (pg 2)
 Save the query with “qry” at the start, followed by “MarkFromFacebook” (merit). (pg 2)
 Make a query to list all people who made their wealth from Microsoft. (pg 5)
 Modify the query to only include those who are in the H1, H2, or H3 on the rich scale. (pg 6)
 Modify the query so only those who have a birthday in May or June are included. (pg 7)
 Modify the query so that it calculates a 1% donation from their net worth (net worth x 1%). (pg 7)
 Modify the query so the “Donation” field is currency with zero decimal places (excellence). (pg 8)
 Notice that criteria across one single line means “Criteria 1 AND Criteria 2” (AND vs OR). (pg 9)
 Notice that criteria on multiple lines of criteria means Line 1 Criterai OR Line 2 Criteria (AND vs OR).
(pg 9)
 Modify your query so it includes the previous criteria, OR anyone with Bill in their firstname. (pg 9)
 Create a new query that calculates age, and then lists up anyone under 50 years of age, who are also
between 175 and 180 cm in height. (pg 10)
 Sort the previous query by age, then by height (merit). (pg 11)
 Practice making queries (Multiple AND criteria, Multiple line OR criteria, Calculations, Wildcards). (pg
12)

By J Murfitt Page 1 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Basic Queries
If you know how to do the task(s) below go to the next heading (page 5), otherwise read the instructions below.
 Create a query to find the guy from Facebook called Mark. Display his mobile number.
 Save the query with “qry” at the start, followed by “MarkFromFacebook” (merit).

1. For an “Achieved” at Level 2 you need to use advanced techniques, which include:
a. writing custom queries to filter and/or sort data
b. using logical, mathematical and/or wildcard operators

2. To “query” means to ask a question, so a query in a database is when we try to extract certain data
based on some criteria.

For example. I just got a phone call from the millionaire guy at Facebook…
Mark something, but I didn’t write down his last name or number. Can
someone tell me his last name and number?

3. If there were only 10 people in the database it would be easy to look


through and find the right person. However, in real life databases can contain millions of records so
it is not easy to just find the data you need. Queries will get the computer to quickly search through
millions of records and find any record that matches what you are searching for.

4. Open up your database from the previous project.

5. Open the table.

6. Scroll through the data and find as many people in “Facebook” as you can.

7. Scrolling through data would appear to be the slow


way of finding data, so let’s make a query instead.
Fast Track – Do this

Click on the “Create” tab at the top

and then click on “Query Design”.

8. You will be asked which table to use. We only have


one table so that makes the choice easier. Just click
on “Add” (or you could double click on the table
name in the list).

9. Click on “Close”.

By J Murfitt Page 2 of 15
AS 91892 Database 4 credits
Project 4 –Queries

10. The question or query we were asking was “Can someone tell me the name and number of the
millionaire from Facebook”. This means we need to add the name and source of wealth fields to our
query.

Either drag the fields to the grid below, or double click the fields in the top box so they are added to
the grid below. Do this for the lastname, firstname, mobile number and source of wealth fields.

Scroll down to find


the other two fields
Fast Track – Do this

11. The results of the query will be that name and source of wealth will display, but this will include every
person in the entire table. Next we need to add the search criteria.

Type in “Facebook” in the criteria part under the “SourceOfWealth” field.

Some things to note are:


a. If you spell Facebook incorrectly you wont get the correct results.
b. It doesn’t matter if you use lower case or upper case as it will still find the right person.
c. You don’t have to type in the quote marks. Access (the software) will add those automatically.

By J Murfitt Page 3 of 15
AS 91892 Database 4 credits
Project 4 –Queries

12. Run the query and you will now have all people who became millionaires from Facebook.

13. If you look at the bottom you should see that we have found 185 people.
Fast Track – Do this

14. But we don’t want all the millionaires from Facebook, we want the guy called Mark
something. So we need to narrow our search down. Click on the “View” icon at the
top left to let you modify your query.

15. Modify your query as shown below.

16. Now run your query again. This time you should have just one person, Mark
himself. Why not pick up your phone and give him a call?

17. Close the query


and save it as “qryPractice01_MarkFromFacebook”.

qryMarkFromFacebook

18. Merit involves “structuring, organising and querying the data logically”, which means
that for merit you need to
a. name your queries with “qry” at the start, followed by a name that describes
what the query does.
b. Not include spaces in your query name.

By J Murfitt Page 4 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Complex Queries
If you know how to do the task(s) below go to the next heading (page 6), otherwise read the instructions below.
 Make a query to list all people who made their wealth from Microsoft.

19. Here is the scenario for the query we need to create.


Fast Track – Read

Bill Gates of Microsoft is going to donate some money to charity. He will donate 1% of a persons net
worth to charity for anyone who made their money from Microsoft and is in the “H1 H2 H3” rich scale
this

(the lowest category in our imaginary scale), and if their birthday is in May or June. As a bonus, he
will also donate to anyone regardless of their source of wealth or location on the rich scale as long as
they have “Bill” anywhere in their first name (so Bill, Billy, or even Sibill).

20. Create a new blank query.

21. Add lastname and firstname so we know who each person listed up is.
Fast Track – Do this

22. Add the fields required for the criteria plus any fields required for calculations (see step 19 above).
If you are not sure have a go, but the answers are in the screen shot below.

23. Add the critiera to filter out just “Microsoft” people.

24. Run your query and you should have about 169 people.
Also check that the results you have are correct (you should only have “Microsoft” showing for the
source of wealth category.
If you find errors now is a good time to fix them up. If you do the whole query and then find errors it
will be harder to work out where the error was. If you find an error now it is probably going to be in
the source of wealth field since that is the only one with a criteria .

By J Murfitt Page 5 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Wildcards
If you know how to do the task(s) below go to the next heading (page 7), otherwise read the instructions below.
 Modify the query to only include those who are in the H1, H2, or H3 on the rich scale.

25. Next we need to display just those people who are in the H1, H2 or H3 rich scale.
There are two ways to do this.

26. Option 1 is to list all 3 categories and put and “or” between them. This means
if they are in H1, or in H2, or in H3, then list them up.

Option 2 is to use a wildcard. If you type in “h*” and press enter Access (the
software) will automatically convert what you typed to Like “h*”. This means
that any category that has an “h” at the front, then followed by any other
letter or letters (the * means anything), will list up.
Fast Track – Do this

27. Enter in the second option.

28. Run the query and check that it is correct, that you only have “H” rich scales people who made their
wealth from “Microsoft”.

By J Murfitt Page 6 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Calculated Fields using Dates


If you know how to do the task(s) below go to the next heading (page 7), otherwise read the instructions below.
 Modify the query so only those who have a birthday in May or June are included.

29. Next we need to filter out people who were born in May or June (month 5 and 6). To do this we
need to do a calculation, where we look at the date and then extract just the month from it. Luckily
there is a formula to do this for us.

30. To create a calculated field, we need to give it a name. Looking at the answer below, the name I
gave the field was “BirthMonth”. After the name add a colon (two dots on top of each other, : ).
Next add the formula, which was to calculate the “Month( )” of the “Birthdate” field.

31. If the screen shot above is too small, here is what it says.
Fast Track – Do this

BirthMonth: Month([Birthdate])
32. The square brackets [ ] are used around a field name in indicate the fields name (to state the
obvious). You don’t really need the square brackets unless you have spaces in your field names
(which you shouldn’t have anyway). But if a field was called “Date of Birth” the query would think
you had a field called “Date”, another called “of” and another called “Birth”. To tell the computer to
include spaces you add the square brackets. Incidentally, if you wanted to calculate the day of their
birthdate, use BirthDay: Day([Birthdate]).

33. We only want people born in May or June to display, so add criteria to only have birth month 5 and 6
display.

34. Run your query and test that it works. You should have 6 people showing in your results.

Calculated Fields using Numbers


If you know how to do the task(s) below go to the next heading (page 8), otherwise read the instructions below.
 Modify the query so that it calculates a 1% donation from their net worth (net worth x 1%).

35. Next we need to work out 1% of each persons net worth. We will do the same thing we just did to
Fast Track – Do this

calculate the month, but this time we will use the following formula. Note that the formula below is
written in English so you will need to modify it to fit into a query.

Donation = Net Worth * 0.01


*=times 0.01 = 1%

36. Modify your query to calculate the donation amount.

By J Murfitt Page 7 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Format a Field in a Query


If you know how to do the task(s) below go to the next heading (page 9), otherwise read the instructions below.
 Modify the query so the “Donation” field is currency with zero decimal places (excellence).

37. Run your query and check that the results are correct.
Fast Track – Do this

38. Hopefully you noticed that the donation amount doesn’t have $ sign in front of it.
39. Edit the query.
40. If you cant see the properties to the right, click on the “Property Sheet” button to display it.
41. Click somewhere in the donation field.
42. Change the format property to “Currency”.

Click here
to select
field

43. Run the query again and check if it is displaying better. It is still displaying 2 decimal places.
Fast Track – Do this

44. Edit the query again, and go back to the donation field
properties.
45. This time you will see “Decimal Places” so you can set
decimal places to zero. What happened was when you
set the format to currency the first time, the previous
format didn’t have decimal places so that property
didn’t display. When you went back again the currency
does have decimals so the property displayed the
second time.

46. For excellence you need to present the data effectively. This would include setting
fields in queries to display $ (currency) with the correct decimal places.

By J Murfitt Page 8 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Criteria 1 “AND” Criteria 2


If you know how to do the task(s) below go to the next heading (page 9), otherwise read the instructions below.
 Notice that criteria across one single line means “Criteria 1 AND Criteria 2” (AND vs OR).

47. Hopefully your query will look something like this.


Fast Track – Do this

48. You should note that all of our criteria are on the same line. This means than people in our results
will have H at the start of their rich scale, AND be born in month 5 or 6, AND earned their money from
Microsoft.
49. Run your query to check that it is working correctly.

Criteria 1 “OR” Criteria 2


If you know how to do the task(s) below go to the next heading (page 10), otherwise read the instructions below.
 Notice that criteria on multiple lines of criteria means Line 1 Criterai OR Line 2 Criteria (AND vs OR).
 Modify your query so it includes the previous criteria, OR anyone with Bill in their firstname.

50. Back to the original task we had to solve.


Bill Gates of Microsoft is going to donate some money to charity. He will donate 1% of a persons net
worth to charity for anyone who made their money from Microsoft and is in the “H1 H2 H3” rich scale
(the lowest category in our imaginary scale), and if their birthday is in May or June. As a bonus, he
will also donate to anyone regardless of their source of wealth or location on the rich scale as long as
they have “Bill” anywhere in their first name (so Bill, Billy, or even Sibill).

a. So far we have the first part, which is to find anyone…


Rich Scale starts with H AND birthday is in May or June AND wealth gained from Microsoft

51. Now we want to add the criteria above, OR that their first name has “Bill” somewhere in it.
52. If you look at the query below, you should see OR as part of the criteria.
Fast Track – Do this

53. Anything on the top line is all “Criteria 1 AND Criteria 2”. Anything on the second line means “Criteria
on top line, OR criteria on this line”.

54. Modify your query so it has Like “*Bill*” in the second line.
55. Run the query and check it for accuracy. You should have 10 people display.
56. Save the query as “qryPractice02-FirstnameWithBill-RichScaleH”.

By J Murfitt Page 9 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Calculated Fields using Dates


If you know how to do the task(s) below go to the next heading (page 11), otherwise read the instructions below.
 Create a new query that calculates age, and then lists up anyone under 50 years of age, who are also between
175 and 180 cm in height.
Fast Track – Do this

57. Our next query is to list up all millionaries who are under 50, but between 175cm and 180 cm tall. We
will sort these by age, then by height.
58. Create a new blank query.
59. Add the fields we need to display (both names, birthdate and height).
60. Add the search criteria (<=50 for the age, >=175 and <=180 for the height).
61. Add a field to calculate the age, using the following formula. Be careful with the number of brackets.

int(( Now( ) - [Birthdate] ) / 365.25 )


62. Here is how the formula works.

Now( ) Gets todays date 25-Dec-2019


Birthdate Gets their birthdate 25-Dec-2000

Now( ) - Birthdate Finds the number of days diffence between 6939


the two dates.
Divide by 365.25 The average days in a year is 365.25, so if 18.99 years old
we divided 6939 by 365.25 we get their
approximage age.
Int( ) Removes any decimals, so 18.99 becomes 18
18.

63. The age we calculate is approximate, meaning it won’t be 100% accurate. There are a few reasons for
that. Answer the following questions to find out why.
a. Is 28th Feb (last day of Feb) to 31st March (last day of March) one month?
b. Is 28th Feb to 28th March one month?
Both answers are actually correct, depending on how you look at it.

64. Run the query and check that it works. You should get over 60 people, but numbers will vary as
depending on todays date the people in the databases age will change.

65. We will look at sorting next, plus the answers are on the next page if you got stuck.

By J Murfitt Page 10 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Sorting Queries
If you know how to do the task(s) below go to the next heading (page 12), otherwise read the instructions below.
 Sort the previous query by age, then by height (merit).

66. For merit you need to structure, organize and query the data logically. This mean for
merit you need to sort your queries correctly.
Fast Track – Do this

67. Sorting in a query goes from left to right, so if I had the following query it would sort
by height, then by age (we want age, then by height).

Sort Height
First Sort Age Second

68. To get our query to sort by age first, then by height, we need to swap the two fields around. To do
this, go just above the age field until your cursor turns into a little black arrow.

69. Click and the top and the whole field will turn black.

70. Now click where the black arrow used to be, and drag the black field so that it is before the height
field. You can now set the sorting so that age is sorted first, then height second.

Sort Height
Sort Age First Second

71. Save your query as “qryPractice03_Under50years_age_Between175-180cm”.

By J Murfitt Page 11 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Practice Making Queries


If you know how to do the task(s) below go to the next heading (page 13), otherwise read the instructions below.
 Practice making queries (Multiple AND criteria, Multiple line OR criteria, Calculations, Wildcards).
72. Create queries to answer the following questions. Save each query as “qry” followed by its number,
followed by an appropriate name. You don’t need to make all of them (unless you want to), but you
should make at least one from each category (so at least 4). Try to extend yourself and make one
that looks hard.

Multiple Criteria (AND)

#2 List all females


with a net worth of
$100 million or more
(100,000,000) who are
tax evaders and got
their wealth from This one
Microsoft. also has a
calculation

Multiple Line Criteria (OR)


Calculations

#4 List all
females from
facebook, or all
males from
Microsoft. Sort by
gender, then name.

#7 What are the


names of the Wildcard
millionaires who
are 80 years old?
Sort by name.

Only A Genius Can Do These

#12
What is the most
common “Rich
Category”? Sort
by category.

By J Murfitt Page 12 of 15
AS 91892 Database 4 credits
Project 4 –Queries

Answers
If you know how to do the task(s) below go to the next heading (page 15), otherwise read the instructions below.
 Answers to the practice queries.

1 How many female tax evaders are over 175cm tall? Sort by height. The answer is 35 people.

2 List all females with a net worth of $100 billion or more (100,000,000) who are tax evaders and got their
wealth from Microsoft.

3 List females associated with Air NZ, between 170 - 180 cm tall, with a first name starting with the letter A.

4 List all females from facebook, or all males from Microsoft. Sort by gender, then name.

5 Will pay everyone born on 25-Dec or 1-Jan $25,000 for every year old they are. List up who we need to pay
and how much for each person.

(note that the last two fields don’t have ticks, meaning they don’t display in the results).

By J Murfitt Page 13 of 15
AS 91892 Database 4 credits
Project 4 –Queries
6 Who was born on 29th Feb? You should get zero records, meaning no-one was born on that date.

7 What are the names of the millionaires who are 80 years old? Sort by birthdate.

8 List all tax evaders and calculate the tax they owe based on their net worth (networth x 15%).

9 List every male that has “aa” in their firstname. Sort by name.

10 List everyone with a firstname that starts with j, and a last name that starts with j. Sort by name.

11 You could only see the last two digits of their phonenumber, which was “11”. Who could they be? Sort by
name.

By J Murfitt Page 14 of 15
AS 91892 Database 4 credits
Project 4 –Queries
12 What is the most common “Rich Category”? Sort by category?
To do this, click on the Sum button top right, then add the fields as shown below.

13 Count the number of females from facebook or males from Microsoft.


Same as above – click on sum button first.

Learning Outcomes (What You Should Have Learnt)


 How to import data from Excel.
 How to create queries.
 How to use wildcards in queries.
 How to use multiple criteria.
 How to calculate things in a query (like someones age).
 The different between AND OR.
 How to sort queries.
 Practice making queries.

By J Murfitt Page 15 of 15

You might also like