Database queries: turn conditions into exact results
A retrieval request has three separate decisions: which records qualify, which fields appear and how the result is ordered. Mixing them causes common errors. Sorting by quantity does not filter out small quantities, and hiding a field does not stop it being used as a condition.
Use the complete fictional equipment catalogue below. It contains no people or private records. ItemID, Category and ServiceStatus are text fields; Quantity is a whole-number field. There are no missing values, and category and status spellings match exactly. Those assumptions keep this lesson focused on two-valued AND/OR logic rather than database-specific handling of missing values or text matching.
Read the supplied table
| ItemID | Category | Quantity (items) | ServiceStatus |
|---|---|---|---|
| E01 | Audio | 4 | Ready |
| E02 | Audio | 5 | Service |
| E03 | Display | 5 | Ready |
| E04 | Display | 8 | Service |
| E05 | Storage | 0 | Ready |
| E06 | Audio | 8 | Ready |
| E07 | Storage | 5 | Service |
| E08 | Display | 8 | Ready |
Quantity zero is a supplied value, not an empty cell. The values 4, 5 and 8 make the boundary at five visible. Ready and Service are catalogue labels; do not infer any repair decision from them.
A condition such as Quantity ≥ 5 includes five. Quantity > 5 excludes five. Evaluate conditions for each row, retain qualifying rows, and only then apply the requested ordering. All worked results display the same four fields so you can check their selection.
AND requires both; OR includes either or both
| Condition A | Condition B | A AND B | A OR B |
|---|---|---|---|
| False | False | False | False |
| False | True | False | True |
| True | False | False | True |
| True | True | True | True |
OR is inclusive here. A row satisfying both conditions is included once in this single-table result; it is not copied into the answer twice. Parentheses make grouping explicit when AND and OR appear together.
The grids below use LibreOffice Base’s documented convention: conditions on one criteria row combine with AND; alternative criteria rows combine with OR. Empty grid cells impose no condition. They do not mean that the source field must be empty. These are teaching grids, not screenshots. The optional read-only SELECT statements use SQLite-compatible syntax; do not assume that every application accepts identical notation.
Worked request 1: ready equipment with at least five items
Request: Show Ready records whose quantity is at least five. Put the greatest quantity first, breaking quantity ties by ItemID ascending.
Logical form: (ServiceStatus = Ready) AND (Quantity ≥ 5).
| Criteria row | ItemID | Category | Quantity | ServiceStatus |
|---|---|---|---|---|
| Criteria | >= 5 | = ‘Ready’ |
SELECT ItemID, Category, Quantity, ServiceStatus
FROM Equipment
WHERE ServiceStatus = 'Ready' AND Quantity >= 5
ORDER BY Quantity DESC, ItemID ASC;
| ItemID | Category | Quantity | ServiceStatus |
|---|---|---|---|
| E06 | Audio | 8 | Ready |
| E08 | Display | 8 | Ready |
| E03 | Display | 5 | Ready |
E06 and E08 both have eight items and are Ready, so E06 precedes E08 by the tie-break rule. E03 qualifies at the inclusive boundary of five. E01 is Ready but has only four; E02 has five but is in Service. Each fails one required condition. E05 has zero, while E04 and E07 fail the status test. Exactly three rows remain.
Worked request 2: audio equipment or fewer than five items
Request: Show every Audio record or record with fewer than five items. Sort by ItemID ascending; status is unrestricted.
Logical form: (Category = Audio) OR (Quantity < 5).
| Criteria row | ItemID | Category | Quantity | ServiceStatus |
|---|---|---|---|---|
| Criteria | = ‘Audio’ | |||
| or | < 5 |
SELECT ItemID, Category, Quantity, ServiceStatus
FROM Equipment
WHERE Category = 'Audio' OR Quantity < 5
ORDER BY ItemID ASC;
| ItemID | Category | Quantity | ServiceStatus |
|---|---|---|---|
| E01 | Audio | 4 | Ready |
| E02 | Audio | 5 | Service |
| E05 | Storage | 0 | Ready |
| E06 | Audio | 8 | Ready |
E01 satisfies both conditions and appears once. E02 and E06 qualify because they are Audio even though neither has fewer than five items. E05 qualifies because zero is less than five even though its category is Storage. E03, E04, E07 and E08 satisfy neither condition. Replacing OR with AND would return only E01, answering a narrower request.
Worked request 3: ready audio or display equipment
Request: Show Ready equipment in either Audio or Display. Put the smallest quantity first, breaking ties by ItemID ascending.
Logical form: ((Category = Audio) OR (Category = Display)) AND (ServiceStatus = Ready).
| Criteria row | ItemID | Category | Quantity | ServiceStatus |
|---|---|---|---|---|
| Criteria | = ‘Audio’ | = ‘Ready’ | ||
| or | = ‘Display’ | = ‘Ready’ |
SELECT ItemID, Category, Quantity, ServiceStatus
FROM Equipment
WHERE (Category = 'Audio' OR Category = 'Display')
AND ServiceStatus = 'Ready'
ORDER BY Quantity ASC, ItemID ASC;
| ItemID | Category | Quantity | ServiceStatus |
|---|---|---|---|
| E01 | Audio | 4 | Ready |
| E03 | Display | 5 | Ready |
| E06 | Audio | 8 | Ready |
| E08 | Display | 8 | Ready |
The diagram’s two branches mean Audio AND Ready, or Display AND Ready. Repeat the Ready condition on both grid rows. E01, E03, E06 and E08 qualify. E02 and E04 are excluded by status; E05 and E07 are excluded by category. E06 and E08 tie at eight and remain in ItemID order.
Diagnose a plausible wrong result
A learner writes Category = ‘Audio’ OR Category = ‘Display’ AND ServiceStatus = ‘Ready’, with no parentheses. In SQLite, AND has higher precedence than OR. The condition therefore means Audio OR (Display AND Ready). The Ready requirement does not apply to the Audio branch.
The wrong result, sorted by quantity then ItemID, is E01, E02, E03, E06, E08. E02 exposes the mistake: it is Audio but in Service. Restore the parentheses around the category alternatives. In the grid, the equivalent repair is to require Ready on both rows. Changing the sort direction would not remove E02 and would not fix the selection.
Check every row, including excluded ones
| ItemID | Request 1 | Request 2 | Request 3 |
|---|---|---|---|
| E01 | No | Yes | Yes |
| E02 | No | Yes | No |
| E03 | Yes | No | Yes |
| E04 | No | No | No |
| E05 | No | Yes | No |
| E06 | Yes | Yes | Yes |
| E07 | No | No | No |
| E08 | Yes | No | Yes |
This row audit checks membership separately from order. Next verify the displayed values against the source table, then check adjacent results against the sort rules. A result can have the right row count but the wrong records. It can also have all the right records in an order the request did not specify.
Three solved practice tasks
Task 1: Select records in Service with quantity at least five, ordering quantity descending then ItemID ascending. Solution: Use (ServiceStatus = Service) AND (Quantity ≥ 5). The ordered rows are E04 (Display, 8, Service), E02 (Audio, 5, Service), E07 (Storage, 5, Service). The tie-break places E02 before E07.
Task 2: Select quantities from zero through five inclusive, sorting by quantity ascending then ItemID ascending. Solution: Use Quantity ≥ 0 AND Quantity ≤ 5. The complete order is E05 (0), E01 (4), E02 (5), E03 (5), E07 (5). Quantity five is included, and the three tied rows follow ItemID order.
Task 3: Request 1 should display only ItemID and Quantity. Must ServiceStatus be removed from the condition? Solution: No. Keep the Ready condition and display just the requested fields. The result is E06, 8; E08, 8; E03, 5. Field visibility and row selection serve different purposes.
Read the request back from the result
For each returned row, explain why it qualifies. For a row exactly on a boundary, explain why the inequality includes or excludes it. Finally, identify which condition rejects a near miss. This is more reliable than judging a query by whether its output “looks sensible”.
The LibreOffice Base Query Design reference documents criteria rows, alternative rows, visibility and sorting. The SQLite expression reference states operator precedence. Use explicit grouping even when an application’s default happens to give the result you intended.
A form and report for the same equipment catalogue
The query examples above retain the original eight Equipment records, E01–E08. A form provides a labelled view for entering or editing one record; a report presents selected records in a readable output. Neither replaces the underlying table or the query’s selection rules. This is a native HTML design and paper trace. It has no database connection, save action or installer.
Form specification: one equipment record
The following record card displays the existing E06 record. The disabled controls illustrate labels and control choices without submitting or changing anything. An actual application would need its own tested save transaction and database constraints.
| Field | Validation and readable error |
|---|---|
| ItemID | Required text: uppercase E followed by exactly two digits. It must not duplicate an existing ItemID. Error: “Enter an unused ID in the form E09.” Uniqueness must be checked against stored data, not merely by an HTML pattern. |
| Category | Exactly Audio, Display or Storage. Error: “Choose Audio, Display or Storage.” |
| Quantity | Required integer greater than or equal to zero. Blank is missing, not zero. Error: “Enter a whole number of items, zero or more.” |
| ServiceStatus | Exactly Ready or Service. Error: “Choose Ready or Service.” These labels do not certify a physical equipment inspection. |
For this design, a failed field prevents saving the whole new record. Keep the candidate visible so it can be corrected; do not silently insert a partial row. Validation can reject an impossible format but cannot prove that a plausible quantity was counted correctly. Verification would compare the entry against the authorised source record. In an edit of E06, its unchanged own ID is allowed; the duplicate-ID test below concerns a new record.
| Candidate: ItemID / Category / Quantity / ServiceStatus | Result | Reason |
|---|---|---|
| E09 / Audio / 6 / Ready | Valid | Unused ID, allowed text labels, nonnegative integer. |
| E03 / Audio / 6 / Ready | Reject | E03 already identifies the original Display record. Do not overwrite it. |
| E09 / Audio / blank / Ready | Reject | Required Quantity is missing; it is not treated as zero. |
| E09 / Audio / −1 / Ready | Reject | Quantity is negative. |
| E09 / Audio / 2.5 / Ready | Reject | Quantity is not a whole number of items. |
| E09 / Sound / 6 / Ready | Reject | Sound is not one of the three supplied category labels. |
| E09 / Audio / 6 / Broken | Reject | Broken is not an allowed ServiceStatus value. |
| E10 / Storage / 0 / Ready | Valid | Zero is a permitted quantity; this does not mean the record passes a quantity-at-least-five query. |
Readable report from the original eight records
Report title: Ready equipment with at least five items.
Source: the original eight-record Equipment catalogue above, classroom snapshot A.
Selection: ServiceStatus = Ready AND Quantity ≥ 5.
Order: Quantity descending, then ItemID ascending.
Display fields: ItemID, Category and Quantity. Ready remains in the selection even though it is not repeated in each report row.
| ItemID | Category | Quantity (items) |
|---|---|---|
| E06 | Audio | 8 |
| E08 | Display | 8 |
| E03 | Display | 5 |
| Record count | 3 records | |
| Sum of Quantity | 21 items | |
The count measures three catalogue records; the sum measures 8 + 8 + 5 = 21 items. They answer different questions. The title, source snapshot, selection, ordering, units and totals make the output usable without needing to reconstruct its query. No date or current stock claim is invented for the fictional dataset.
Worked input-to-output trace
Consider a separate hypothetical working copy B. A new candidate E09 / Audio / 6 / Ready first passes all four validation rules against A. If a real application then saved it successfully, B would contain nine records. Re-running the same report query would include E09 and order the rows E06 (8), E08 (8), E09 (6), E03 (5). The count becomes 4 records and the quantity sum becomes 27 items. The original A report above is intentionally unchanged. A previously generated static report would stay at A unless regenerated; a refreshed report reading B would use the new data.
If the candidate instead contained Quantity 2.5, validation would reject it: no new record and no changed report total. If a valid E10 / Storage / 0 / Ready were added instead, the table would gain a record but this report would still show 3 records and 21 items. Its quantity condition excludes E10. Passing input validation and passing a retrieval condition are separate decisions.
Independent output check
Start from A again. A proposed new record is E11 / Display / 5 / Service. Is it valid, and would it appear in the Ready report? Then use the unchanged original catalogue to prepare a Service report with Quantity ≥ 5, ordered Quantity descending and ItemID ascending. State the IDs, record count and quantity sum.
Checked answer: E11 is valid under the form rules, but Service fails the Ready report’s status condition. On the unchanged original catalogue, the Service report contains E04 (8), E02 (5), E07 (5), giving 3 records and 18 items. E02 precedes E07 on the tied quantity. E11 is not included because this second task explicitly uses original snapshot A. An answer containing E11 has silently changed the source snapshot.
Background: LibreOffice’s form concepts and Report Wizard concepts. The form rules, report layout and traces are original and can be studied directly here.
Frequently asked questions
Does OR mean exactly one condition?
No. Here OR is inclusive: either condition or both may be true. Exclusive OR would need to be requested separately.
Why is five included in the first result?
The condition is at least five, written Quantity ≥ 5. A strict greater-than condition would exclude five.
Does an empty criteria-grid cell select empty source values?
No. In these grids it means no condition for that field on that row. Missing source values require a separate test.
Can a condition use a field that is not displayed?
Yes. Request 1 can filter by ServiceStatus while displaying only ItemID and Quantity.
Why repeat Ready in request 3’s grid?
Each alternative grid row is a complete way to qualify. Both the Audio branch and the Display branch must require Ready.
What does the second sort field do?
It resolves ties in the first sort field. Equal quantities are ordered by ItemID ascending in these examples.
Does sorting change which rows qualify?
No. Sorting arranges selected rows. It cannot repair an incorrect AND, OR or boundary condition.
WhatsApp Us Now