Skip to content
Exit presentation

Data Handling II: Databases & Structured Data

Asking the register one question

The register answers one plain question at a time. Such a question is a query. Its test, such as "section is 9-A", is the condition. To walk the rows is to read them one by one.

Five rows, the register's own order.
Step 1 of 4
  1. Five rows, the register's own order.
  2. The condition: section is 9-A.
  3. Three rows keep; two are dropped.
  4. The answer: 3 of 5 kept.

The rule

A query walks the rows. It keeps rows where one field meets the condition. The answer is whatever the walk kept — including nothing.

Here at is a variable name, not the English word. It holds the position of the due column. The sign === means is equal to.

  1. const at = headers.indexOf('due');
  2. const answer = [];
  3. for (const row of rows) {
  4. if (row[at] === 1450) {
  5. answer.push(row);
  6. }
  7. }

Line 2 makes answer an empty list. Line 3 takes each row in turn. Line 4 tests its due. Line 5 adds the whole row to answer.

The condition here is a due of 1450 rupees. This walk keeps four whole records: A-103, A-106, A-108 and A-111. That is four records, not just four dues.

Ask for section 10-C, and the walk keeps nothing. No row passes the test, so the answer is an empty list. That answer is correct, not broken — no record carries 10-C yet.

Tap a question below. Watch which rows are kept.

Loading lab…

An empty answer is still an answer.

Jump to lesson

This track

  1. 0 Start here
  2. 1 One record, one row
  3. 2 The naming value
  4. 3 Naming the columns
  5. 4 Asking the register one question
  6. 5 Two conditions, both true
  7. 6 The answer, in order
  8. 7 Counting the groups in the answer
  9. 8 What a database adds
  10. 9 The big picture
  11. 10 Track quiz

Other tracks