GoalExplain that a query asks a table a question, and read the parts of a query.
New words
query · a question we ask the database, which answers with only the records and fields we wantcriteria · the conditions a record must meet to appear in the answerrun · to tell the database to find the answer to the queryresult · the small table of records that the query gives back
Explanation
A table may hold thousands of records. We seldom need all of them. A query lets us ask the database a question and get back only the records and fields that answer it. A question such as “Which pupils are in class 1A?” becomes a query. A query has a grid with these rows. Field: which field the column is about. Table: which table the field comes from. Show: a tick if we want to see this field in the answer. Criteria: the condition. The steps are: think of the question, fill in the grid, run the query, read the result. The query does not change the data in the table; it only reads it, like looking through a window. Programs differ in how they draw the grid, so we learn the idea on paper.
think of the question→fill in the grid→run the query→read the result
Worked examples
Field
Name
Class
Table
Pupils
Pupils
Show
yes
no
Criteria
1A
Question: “Which pupils are in class 1A?” In the grid, the field Name is shown, and the field Class has the criterion 1A. The result: Mina and Omar. Two records match.
A
B
C
D
1
id
Name
Class
English
2
1
Mina
1A
15
3
2
Salma
1B
17
4
3
Omar
1A
12
5
4
Nour
1C
19
6
5
Youssef
1B
14
Look at the pupils’ table. Which records have a Class of 1B? Salma (id 2) and Youssef (id 5). The query returns 2 records out of 5, and the table itself is not changed.
A
1
Name
2
Nour
Write each question as a query in words: “Show the Name of the pupils whose Class is 1C.” The result has one record: Nour.
Class activity
Use the pupils’ table in this lesson. Write three questions of your own on small cards, such as “Which pupils have an English mark of 15 or more?” For each, say which field has the criterion, which fields to show, and the result. Check each other’s answers.
Practice
1
What does a query do?
It asks the database a question and returns only the records and fields that answer it.
2
Name the four rows of the query grid.
Field, Table, Show, Criteria.
3
Put in order: run the query, fill in the grid, read the result, think of the question.
Think of the question; fill in the grid; run the query; read the result.