Information Technology Topic

Querying with Multiple Conditions

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. A query selects which fields to show and which records to pick out, using criteria written as a field, a relational operator (=, <>, <, >, <=, >=) and a value
  2. Several conditions are joined with AND (every condition true, so fewer records), OR (at least one true, so more records) and NOT, and brackets or separate rows of a criteria grid control how they group
  3. A calculated field is a new column worked out from other fields, such as Pay = Hours * Rate, and it can be shown, sorted on and used in a condition without changing the stored table

Section 1What a query is

A query is a question you ask the database. You choose which fields to show, which records to pick out and, if you like, the order to show them in. The answer is a list of records. The answer is only a view of the data. The stored table is not changed and not copied, and if the table changes, the next run of the query shows the new data. We use one table in this topic. A call centre in Montego Bay keeps an AGENT table with 12 records. AGENT (AgentID, Name, Dept, Hours, Rate, Years): 1, Andre Brown, Sales, 38, 1500, 2; 2, Shanice Reid, Support, 40, 1400, 5; 3, Kemar Lewis, Sales, 35, 1600, 7; 4, Tanya Gordon, Billing, 42, 1300, 1; 5, Omar Clarke, Support, 30, 1400, 3; 6, Marcia Francis, Sales, 40, 1500, 4; 7, Devon Campbell, Billing, 36, 1350, 6; 8, Keisha Hall, Support, 44, 1450, 8; 9, Ryan Thompson, Sales, 28, 1600, 1; 10, Alicia Morgan, Billing, 40, 1300, 9; 11, Jermaine Wright, Support, 32, 1400, 2; 12, Nadine Brown, Sales, 45, 1550, 10. Hours is the hours worked this week, Rate is the pay in J$ for each hour and Years is the years of service. The departments are Sales (5 agents), Support (4) and Billing (3).

Section 2Selecting fields and setting criteria

Every query makes two separate choices. Choosing the fields decides the columns you see. Setting the criteria decides the records you see. For example, 'show Name and Dept for agents with more than 3 years of service' selects two fields (Name, Dept) and sets one criterion on a third field (Years > 3). A field can be used in a criterion without being displayed. You can also choose a sort order, such as highest Pay first. The sort changes the order of the records, not which ones are picked. Here's the trick: ask yourself 'which columns?' and 'which rows?' as two different questions.

Section 3A condition and the relational operators

One condition has three parts: a field, a relational operator and a value. Text values go inside quotation marks, numbers do not. Dept = "Support" and Hours > 40 are two conditions. The relational operators are = (equal to), <> (not equal to), < (less than), > (greater than), <= (less than or equal to) and >= (greater than or equal to). Some results from the AGENT table. Dept = "Support" gives 4 records. Dept <> "Sales" gives 7. Hours > 40 gives 3 (Tanya Gordon, Keisha Hall and Nadine Brown). Watch out for the boundary value. Keisha Hall has a Rate of exactly 1450, so Rate > 1450 leaves her out and Rate >= 1450 brings her in.

Section 4Using AND, OR and NOT

When one condition is not enough, join conditions with a logical operator. AND: every condition must be true. Dept = "Support" AND Years > 3 gives 2 records: Shanice Reid and Keisha Hall. AND narrows the search, so it gives the same or fewer records. OR: at least one condition must be true. Dept = "Sales" OR Dept = "Billing" gives 8 records, the 5 Sales agents and the 3 Billing agents. OR widens the search. NOT: reverses a condition. NOT (Dept = "Sales") gives 7 records, the same as Dept <> "Sales". Two tricks to remember. A range uses AND on one field: Hours >= 35 AND Hours <= 40 gives 6 records. And an agent has only one department, so Dept = "Sales" AND Dept = "Billing" gives 0 records. Easy to mix up: in everyday English 'Sales and Billing agents' means both groups together, which in a query is OR.

Section 5Brackets and the criteria grid

Brackets show which conditions belong together. To find agents in Sales or Billing who have at least 4 years of service, write (Dept = "Sales" OR Dept = "Billing") AND Years >= 4. The records are AgentID 3, 6, 7, 10 and 12: Kemar Lewis, Marcia Francis, Devon Campbell, Alicia Morgan and Nadine Brown. Always put brackets round an OR that sits inside an AND. Many packages let you build the query in a criteria grid. There is one column for each field and one or more rows of criteria. Conditions on the same row are joined by AND. Conditions on different rows are joined by OR. Example grid: Row 1: Dept = "Support" and Years > 3 Row 2: Dept = "Billing" This means (Dept = "Support" AND Years > 3) OR Dept = "Billing". It picks Shanice Reid and Keisha Hall from Support, and Tanya Gordon, Devon Campbell and Alicia Morgan from Billing, which is 5 records.

Section 6Calculated fields

A calculated field is a new column that the query works out from other fields in each record. You write an expression and give the new field a name, called an alias, which is the heading shown. The real fields are not renamed and the stored table does not change. Pay = Hours * Rate. Tanya Gordon: 42 * 1300 = 54600. Bonus = Years * 500. Devon Campbell: 6 * 500 = 3000. Total = Pay + Bonus. Devon Campbell: Pay 36 * 1350 = 48600, so Total = 48600 + 3000 = 51600. You can use a calculated field in a condition too. Pay > 55000 gives 6 records: Andre Brown, Shanice Reid, Kemar Lewis, Marcia Francis, Keisha Hall and Nadine Brown. (In some packages you type the whole expression, Hours * Rate > 55000, in the criterion instead of the alias.) You can also sort on it. The Support agents sorted by Pay, highest first, are Keisha Hall 63800, Shanice Reid 56000, Jermaine Wright 44800 and Omar Clarke 42000.

Section 7Planning and checking a query

Write the request in words first. Then choose the fields to show, write each condition as field, operator, value, decide how they join (AND, OR, NOT), add brackets where needed, and add any calculated field and sort order. Then check the result. Pick one record you know should be included and one that should not, and test them against every condition. Count the records: an AND result is never bigger than either condition on its own, and an OR result is never smaller. For example Dept = "Support" OR Years > 8 should give at least 4 records, because Support alone gives 4. It gives 6: the 4 Support agents plus Alicia Morgan and Nadine Brown, who have more than 8 years. If a result looks wrong, check the boundary values (> or >=), the quotation marks round the text, and whether AND should have been OR.

Examples

AGENT (AgentID, Name, Dept, Hours, Rate, Years): 1, Andre Brown, Sales, 38, 1500, 2; 2, Shanice Reid, Support, 40, 1400, 5; 3, Kemar Lewis, Sales, 35, 1600, 7; 4, Tanya Gordon, Billing, 42, 1300, 1; 5, Omar Clarke, Support, 30, 1400, 3; 6, Marcia Francis, Sales, 40, 1500, 4; 7, Devon Campbell, Billing, 36, 1350, 6; 8, Keisha Hall, Support, 44, 1450, 8; 9, Ryan Thompson, Sales, 28, 1600, 1; 10, Alicia Morgan, Billing, 40, 1300, 9; 11, Jermaine Wright, Support, 32, 1400, 2; 12, Nadine Brown, Sales, 45, 1550, 10. Hours is the hours worked this week, Rate is the pay in J$ for each hour and Years is the years of service. The manager wants the Support agents who have MORE THAN 3 years of service. (a) Write the criteria for the query. (b) List the agents the query picks out. (c) The manager changes the request to AT LEAST 3 years of service. Write the new criteria and name the extra agent who now appears.

(a) Two conditions must both be true, so join them with AND: Dept = "Support" AND Years > 3. (b) The Support agents are Shanice Reid (5 years), Omar Clarke (3), Keisha Hall (8) and Jermaine Wright (2). Only Shanice Reid and Keisha Hall have more than 3 years, so 2 agents are picked out. (c) 'At least 3' includes 3, so the operator changes to >=: Dept = "Support" AND Years >= 3. Omar Clarke has exactly 3 years, so he is the extra agent, and the query now picks out 3 agents.

AGENT (AgentID, Name, Dept, Hours, Rate, Years): 1, Andre Brown, Sales, 38, 1500, 2; 2, Shanice Reid, Support, 40, 1400, 5; 3, Kemar Lewis, Sales, 35, 1600, 7; 4, Tanya Gordon, Billing, 42, 1300, 1; 5, Omar Clarke, Support, 30, 1400, 3; 6, Marcia Francis, Sales, 40, 1500, 4; 7, Devon Campbell, Billing, 36, 1350, 6; 8, Keisha Hall, Support, 44, 1450, 8; 9, Ryan Thompson, Sales, 28, 1600, 1; 10, Alicia Morgan, Billing, 40, 1300, 9; 11, Jermaine Wright, Support, 32, 1400, 2; 12, Nadine Brown, Sales, 45, 1550, 10. Hours is the hours worked this week, Rate is the pay in J$ for each hour and Years is the years of service. The manager wants the agents in Sales or Billing who have at least 4 years of service. The query must show Name, Dept and Pay, where Pay = Hours * Rate, with the highest Pay first. (a) Write the criteria, using brackets. (b) Calculate Pay for each agent picked out, and list them in the order the query shows them. (c) Show how the same request is laid out in a criteria grid.

(a) The department is Sales OR Billing, and that must be combined with Years >= 4 by AND. The OR needs brackets: (Dept = "Sales" OR Dept = "Billing") AND Years >= 4. (b) The agents with at least 4 years in those departments are Kemar Lewis (Sales, 7 years), Marcia Francis (Sales, 4), Devon Campbell (Billing, 6), Alicia Morgan (Billing, 9) and Nadine Brown (Sales, 10). Their Pay is Nadine Brown 45 * 1550 = 69750, Marcia Francis 40 * 1500 = 60000, Kemar Lewis 35 * 1600 = 56000, Alicia Morgan 40 * 1300 = 52000 and Devon Campbell 36 * 1350 = 48600. With the highest first, the order is Nadine Brown, Marcia Francis, Kemar Lewis, Alicia Morgan, Devon Campbell. (c) A grid has no brackets, so repeat the Years condition on each row. Row 1: Dept = "Sales" and Years >= 4. Row 2: Dept = "Billing" and Years >= 4. Each row is an AND, and the two rows are joined by OR. It picks out the same 5 agents.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from