You are on page 1of 6

Database Queries and Reports

1. Queries:
a. Select Queries: Extract data from tables according to criteria given and
forms a new table. The resulting collection of records is called a dynaset
(dynamic subset) because when the records in the table change, so do the
records in the query when it is run again. SELECT QUERIES CAN USE OTHER
QUERIES AS WELL, NOT JUST TABLES.

i. Simple select (using one table and straight forward criteria)

Example question: Show the first name, last name and form of all
English students. Name this query English_Students.

Answer:

Click create query in design view – Add table ‘Student’ – add the ‘first
name’, ’last name’, ‘form’ and ‘subject’ fields – under the subject field in
criteria enter ‘English’- unselect the ‘show’ box (you were not asked to
show the subject field you are only using it as a criteria) - save your
query – then open it to see it the results.

(figure this out on your own) -

Try adjusting the same query to show students who do English or are in
form4

Then try adjusting to show students who do English and are in form4

ii. Using two (or more) tables with straight forward criteria

Example question: Show the First and last names, Subject and
Semester 3 grades of all 3rd formers. Name this query 3rdform_sem3.

Answer:

Click create query in design view – Add tables ‘Student’ and ‘grades’ –
add the ‘first name’, ’last name’, ‘form’, ‘subject’ and ‘semester 3’ fields –
under the ‘form’ field in criteria enter ‘3’ - unselect the ‘show’ box (you
were not asked to show the form field you are only using it as a criteria) -
save your query – then open it to see it the results.
iii. Using operators in criteria [only on number and currency fields]: (>
greater than, >= greater than or equal to, < less than, <= less than or
equal to, <> not equal to, = equal to)

Example question: Assuming that the pass mark is 55; show the
name, surname and semester 3 grade of all students who failed that
semester. Sort the records in this query by last name in ascending
order. Name your query failed_sem3.

Answer:

Click create query in design view – Add tables ‘Student’ and ‘grades’ –
add the ‘first name’, ’last name’ and ‘semester 3’ fields – under the ‘last
name’ field in sort select ‘ascending’- under the ‘semester 3’ field in
criteria enter ‘<0.55’ (remember that 55% is actually 0.55) - save your
query – then open it to see it the results.

Iv. Using date criteria: To select records before a particular date use the <
(less than) sign eg: < 02/20/2005 – means before February 20th 2005. To
select records after a particular date use the > (greater than) sign eg: >
02/20/2005 – means after February 20th 2005. To select records between
two dates use the ‘between ---and ---‘ function; eg: between 02/20/2005
and 12/01/2005 – means between February 20th 2005 and December 1st
2005.

Example question: Show the First name and Date of birth of all
students born between January 1st 1991 and December 31st 1994.
Sort the records in this query by date of birth in descending order.
Name this query 91_to_94.

Answer:

Click create query in design view– Add table ‘Student’ – add the ‘first
name’ and ‘Date of birth’ fields – under the ‘Date of birth’ field in sort
select ‘ascending’- then in criteria enter ‘between 1991-01-01 and 1994-
12-31 - save your query – then open it to see it the results

(figure this out on your own) -

Try adjusting the same query to show students who are under 23.

V. A top/highest or bottom/lowest query: To find records with the


top/highest of a particular field sort the field by descending order, then go

to the limit box at the top and type in the number of


records you need (i.e. if you were asked to show top 4 type in ‘4’). To find
records with the bottom/lowest of a particular field, sort the field by
ascending order and follow the same procedure.

Example question: Show the First name, Last name, form and
semester 1 grade of the 4 students with the highest grades of
semester 1. Name this query top_4.

Answer:

Click create query in design view – Add tables ‘Student’ and ‘grades’ –
add the ‘first name’, ’last name’, ‘form’ and ‘semester 1’ fields – under the
‘semester 1’ field in sort select ‘descending’- then in the limit box (at the
top) type in ‘4’- save your query – then open it to see it the results

Vi. Functions query: Displays the average, minimum, maximum, sum


(and others that won’t be used at your level) of any numeric field.

Example question: Show the total of all teachers’ salaries. Name this
query Total_Sal.

Answer:

Click create query in design view– Add table ‘teachers’ – add the ‘salary’
field – Click on functions- select sum - save your query – then open it to
see it the results
**Note that it is not formatted in currency. Right click on the column
heading and select column format to change that.

**Note: the format you choose is not saved (on re-open).

If you want to save your format you can save your query as a view
(Meaning it will become a table) - Right click the query - Create As
View - Go to tables- open it- change the column format and save.

Vii. A calculated field query: A field that does not appear originally in any
of the tables but is created by the query using calculation involving one or
more fields from other tables.

Example question: Assuming tax is 7% of salary. Create a query to


show the teacher first name, last name, salary and tax. Name this
query Teacher_tax.

Answer:

Click create query in design view – Add table ‘teachers’- add the ‘first
name’, ‘last name’ and ‘salary’ fields – in the next column type salary *
0.7 (extend the column so that you see the whole formula) -
enter ‘Tax’ as the Alias.

(The quotes will insert automatically even if you do not type them)

**Right click on the column heading and select column format to change
the Tax field to currency.

2. Reports:
Reports are generated using fields from one or more tables or queries. (When
using fields from more than one table or query these must be connected by
appropriate relationships in order to co-exist in one report). Reports can be
grouped by certain fields, sorted by certain fields and you can also find averages,
sums, minimums and maximums.

Try this: Create a query showing the full names, form, subject and semester 1
grades of all students. Name it Rpt_query.

Example question: Generate a report on the 1st semester grades. Show the
full names, form, subject and semester 1 grades of all students. Group by
subject and sort by last name in ascending order; also show the number of
students in each subject group and the average Semester 1 grade for that
group. The report should be in Portrait orientation. Select the appropriate
table(s) or query(ies) to create this report. Title the report “Semester One
Grades”.

– Which table or tables, query or queries contain the information we need


for this report?

- The Rpt_query contains all the fields we need.

- Click on reports - Use wizard to create reports

- 1. Field selection - Select the Rpt_query – add the fields we need (In this case
all fields)

-2. Labeling fields: You can change how a field will be labeled in the report
(Don’t do this step this time)

-3. Grouping: group by Subject


- 4. Sort options: **Because Subject is a grouping it’s automatically there.
Add the ‘Last Name’ field in ‘then by’ and select ascending order

- 5. Choose layout: Select Tabular and portrait (in this case)

-6. Create report: Name the report “Semester One Grades” and select dynamic.

- Edit the Report - Insert a label box in the page header with the title. Use
properties to align it and change the font size.

- Select Sorting and grouping (at the top) and turn the group footer to present.

- Insert the labels “Total Students” and “Average grade” and insert textboxes next
to both of them

- Open the properties of the textbox for total students and open the data Tab.

- Under ‘Data field type’ select ‘Counter’

- Open the properties of the textbox for average grade and open the data Tab.

- Under ‘Data field type’ select ‘Field or Formula’

- Select the 3 dots next to ‘data field’

- choose the ‘Mathematical’ category and select the average function and click
NEXT

- Open the function dialog box

- Select Semester 1 and click OK

- Click the textbox again. This time go to the general tab.

- Change the formatting to percent with zero decimal places.

- Save the report and select execute.

You might also like