Information Technology Topic
Predicting Query Results
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- To predict a search result, test every record one at a time: work out each condition as true or false, combine the answers with AND, OR and NOT, and keep the records whose whole condition is true
- AND is true only when both conditions are true, OR is false only when both are false, and NOT reverses a result, with brackets worked out first
- Check the answer for sense: an AND result is never bigger than either condition alone, an OR result is never smaller, and the same words with different brackets can give very different results
Section 1Predicting a search by hand
In the exam you are often given a table and a search condition, and asked which records come out, or how many. You work it out by testing every record, one at a time, against the whole condition. A record appears in the result only if the whole condition works out as true for it. We use an invented table for a coach company that runs between Jamaican towns. ROUTE (Code, Origin, Dest, Fare, Mins, Express): K1, Kingston, Portmore, 200, 40, No; K2, Kingston, Spanish Town, 250, 35, Yes; K3, Kingston, May Pen, 400, 60, Yes; K4, Kingston, Mandeville, 700, 120, Yes; K5, Kingston, Ocho Rios, 850, 110, No; M1, Montego Bay, Negril, 600, 75, No; M2, Montego Bay, Ocho Rios, 900, 120, Yes; M3, Montego Bay, Mandeville, 800, 130, No; P1, Portmore, Spanish Town, 180, 30, No; P2, Portmore, May Pen, 350, 55, No; S1, Spanish Town, Ocho Rios, 600, 70, Yes; S2, Mandeville, May Pen, 300, 45, No. Fare is in J$, Mins is the journey time in minutes and Express is a logical field holding Yes or No. There are 12 records. Origin is where the route starts and Dest is where it ends.
Section 2Relational operators on numbers and text
A condition has a field, a relational operator and a value. For each record it works out as true or false. Numbers are compared by size, using = , <>, <, >, <= and >=. Text is compared by exact spelling and goes inside quotation marks, so a two-word town is one value: Origin = "Montego Bay". Watch the boundary value. Fare < 400 gives 5 records (K1, K2, P1, P2 and S2). Fare <= 400 gives 6, because K3 has a fare of exactly 400. On the logical field, Express = "Yes" gives 5 records (K2, K3, K4, M2 and S1) and Express <> "Yes" gives the other 7.
Section 3AND, OR and NOT
The logical operators join the true or false answers of the conditions. AND is true only when both are true: true AND true = true true AND false = false false AND true = false false AND false = false OR is true when at least one is true: true OR true = true true OR false = true false OR true = true false OR false = false NOT reverses a result: NOT true = false and NOT false = true. Here's the trick: AND is strict (every condition must pass) and OR is generous (one pass is enough).
Section 4Two fields: a worked trace
Take the condition Origin = "Kingston" AND Fare < 800. It uses two fields. Test each record and write down both answers. K1: Origin = "Kingston" is true and Fare < 800 is true, so true AND true = true. K1 is shown. K5: Origin = "Kingston" is true but Fare < 800 is false (850), so true AND false = false. K5 is not shown. M1: Origin = "Kingston" is false and Fare < 800 is true, so false AND true = false. M1 is not shown. Doing this for all 12 records gives 4 records: K1, K2, K3 and K4. A small table with one column for each condition and one for the whole condition keeps you from slipping.
Section 5Brackets and NOT
Brackets are worked out first. Take (Origin = "Kingston" OR Origin = "Portmore") AND Express = "No". P1 has Origin = "Portmore" true, so the bracket is true, and Express = "No" is true, so P1 is shown. K2 has the bracket true but Express = "No" false, so K2 is not shown. The records shown are K1, K5, P1 and P2, which is 4. NOT reverses whatever comes right after it. Without brackets it reaches only the next condition, so NOT Express = "Yes" AND Fare < 500 means (NOT Express = "Yes") AND Fare < 500 and gives 4 records: K1, P1, P2 and S2. With brackets, NOT (Express = "Yes" AND Fare < 500) reverses the whole AND and gives 10 records, every route except K2 and K3. Watch out: the same words with different brackets can give very different answers. (Express = "Yes" OR Fare < 300) AND Mins < 50 gives 3 records (K1, K2 and P1). Express = "Yes" OR (Fare < 300 AND Mins < 50) gives 7. If a condition has no brackets, NOT is worked out first, then AND, then OR. Do not rely on this: write brackets so the meaning is clear.
Section 6Counting rules and sense checks
Use these checks on your answer. An AND result can never be bigger than either condition on its own, and an OR result can never be smaller. Origin = "Kingston" gives 5 records. Adding AND Fare < 800 gives 4, which is not more than 5. Express = "Yes" gives 5 records and adding OR Fare < 200 brings in only P1 (fare 180), so 6. For OR, the count is A plus B minus the records in both, because a record that meets both conditions is shown once. Some conditions give extreme results. Origin = "Kingston" AND Origin = "Portmore" gives 0, because a record has only one origin. Fare < 500 OR Fare >= 500 gives all 12. NOT picks exactly the records the condition inside the brackets leaves out. If a condition picks out 8 of the 12 records, NOT that condition picks out the other 4.
Section 7Common traps
Boundary values come first. Check the records that sit exactly on the limit, such as K3 at a fare of 400 or K4 at 700. Next, check the wording. In English, 'routes to Ocho Rios and Mandeville' means either place, so in a search it is Dest = "Ocho Rios" OR Dest = "Mandeville", which gives 5 records (K4, K5, M2, M3 and S1). Then check how far NOT reaches, and make sure you test every field in the condition for every record. Reading only the first condition is the quickest way to lose marks. Finally, list the matching codes and count them. Counting from the list is safer than counting in your head.
Examples
ROUTE (Code, Origin, Dest, Fare, Mins, Express): K1, Kingston, Portmore, 200, 40, No; K2, Kingston, Spanish Town, 250, 35, Yes; K3, Kingston, May Pen, 400, 60, Yes; K4, Kingston, Mandeville, 700, 120, Yes; K5, Kingston, Ocho Rios, 850, 110, No; M1, Montego Bay, Negril, 600, 75, No; M2, Montego Bay, Ocho Rios, 900, 120, Yes; M3, Montego Bay, Mandeville, 800, 130, No; P1, Portmore, Spanish Town, 180, 30, No; P2, Portmore, May Pen, 350, 55, No; S1, Spanish Town, Ocho Rios, 600, 70, Yes; S2, Mandeville, May Pen, 300, 45, No. Fare is in J$, Mins is the journey time in minutes and Express is a logical field holding Yes or No. (a) State the records picked out by Express = "Yes". (b) State the records picked out by Express = "Yes" AND Fare < 700. (c) State how many records are picked out by Express = "Yes" OR Fare < 200, and name the record that was not in part (a).
(a) Test the Express field of every record. The routes with Yes are K2, K3, K4, M2 and S1, so 5 records. (b) Each of those 5 must also have a fare below 700. K2 is 250, K3 is 400 and S1 is 600, so they pass. K4 has a fare of exactly 700, which is not less than 700, and M2 is 900. The records are K2, K3 and S1. (c) With OR, a record needs only one true condition. The 5 express routes qualify. The only route with a fare below 200 is P1, at 180, and it is not an express route, so it is added. That gives 6 records, and P1 is the new one.
ROUTE (Code, Origin, Dest, Fare, Mins, Express): K1, Kingston, Portmore, 200, 40, No; K2, Kingston, Spanish Town, 250, 35, Yes; K3, Kingston, May Pen, 400, 60, Yes; K4, Kingston, Mandeville, 700, 120, Yes; K5, Kingston, Ocho Rios, 850, 110, No; M1, Montego Bay, Negril, 600, 75, No; M2, Montego Bay, Ocho Rios, 900, 120, Yes; M3, Montego Bay, Mandeville, 800, 130, No; P1, Portmore, Spanish Town, 180, 30, No; P2, Portmore, May Pen, 350, 55, No; S1, Spanish Town, Ocho Rios, 600, 70, Yes; S2, Mandeville, May Pen, 300, 45, No. Fare is in J$, Mins is the journey time in minutes and Express is a logical field holding Yes or No. (a) Predict the records picked out by (Origin = "Kingston" OR Origin = "Montego Bay") AND NOT (Express = "Yes"). Show the trace for K4. (b) Predict the records picked out by NOT (Fare >= 600 OR Express = "Yes"). (c) State how many records are picked out by Fare >= 600 OR Express = "Yes" on its own, and explain how that number is linked to your answer to part (b).
(a) The bracket is true for the routes that start in Kingston (K1 to K5) or Montego Bay (M1 to M3). NOT (Express = "Yes") keeps the routes where Express is No. Trace for K4: the bracket is true, Express = "Yes" is true so NOT (Express = "Yes") is false, and true AND false = false. K4 is not shown. The records shown are K1, K5, M1 and M3, which is 4. (b) First work out the bracket. Fare >= 600 is true for K4, K5, M1, M2, M3 and S1, and Express = "Yes" is true for K2, K3, K4, M2 and S1. The OR bracket is true for K2, K3, K4, K5, M1, M2, M3 and S1, which is 8 records. NOT reverses it, so the records shown are the other 4: K1, P1, P2 and S2. (c) The condition inside the brackets, Fare >= 600 OR Express = "Yes", picks out 8 records. NOT picks exactly the records the bracket leaves out, so 8 + 4 = 12, which is every record in the table.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 8, Specific Objective 10 · Syllabus scope
- PostgreSQL 18 Documentation (sections 2.2, 2.6, 2.7, 5.5, 5.7, 7.5, 8, 9.1, 9.2, 11.1) · PostgreSQL Global Development Group · Chapter 9, Section 9.2 Comparison Functions and Operators, checked against (https://www.postgresql.org/docs/current/functions-comparison.html) · Facts
- PostgreSQL 18 Documentation (sections 2.2, 2.6, 2.7, 5.5, 5.7, 7.5, 8, 9.1, 9.2, 11.1) · PostgreSQL Global Development Group · Chapter 9, Section 9.1 Logical Operators, checked against (https://www.postgresql.org/docs/current/functions-logical.html) · Facts
- LibreOffice Base Guide 7.2 · The Document Foundation (LibreOffice Documentation Team) · Chapter 5, Query enhancement using SQL Mode, checked against (https://books.libreoffice.org/en/BG72/BG7205-Queries.html) · Facts