0%

Ask Precise Questions · practice

Combine Search Rules with AND and OR

Riley’s repair session can use tools, while a community setup can use events equipment. Either category is useful, but only when the usual loan period is seven days or fewer. That is one question with two category alternatives and one rule shared by both.

Replace the current WHERE line in search.sql with this :

WHERE (category = 'tools' OR category = 'events')
  AND loan_days <= 7;

Keep the four-column SELECT and FROM items above it, then press Run. The answer contains the folding table, step ladder, and cordless drill. The fourteen-day projector screen is an events item, but it does not meet the loan-period rule.

The words OR and AND combine comparisons. Inside the parentheses, OR allows either named category. After that choice, AND loan_days <= 7 requires the chosen row to meet the period limit too. The parentheses make the intended grouping visible: both category alternatives share the same limit.

Trace three rows through that sentence before thinking about rules. A three-day tool passes the category choice and the period limit. A fourteen-day events item passes the category choice but fails the limit. A two-day electronic item passes the limit but fails the category choice. A row reaches the answer only when both parts succeed.

Without them, this condition would say something different:

WHERE category = 'tools'
   OR category = 'events' AND loan_days <= 7;

SQLite evaluates AND before OR. That version would admit every tool, even a tool with a thirty-day period, while limiting only events items. The visible catalog does not contain a long-period tool, so the mistake could hide here. Submit includes one, which makes the difference observable.

Parentheses are doing the same job they do in a Python : they make one smaller decision before the surrounding decision uses it. You are not adding decoration for the reader. You are recording the grouping that gives the condition its meaning.

The boundary is part of the question too. <= 7 includes an item whose period is exactly seven days. Writing < 7 would quietly remove the folding table and cordless drill.

Read the complete condition in plain language before submitting: the category is tools or events, and the loan period is at most seven days. Then compare that sentence with the parentheses and operators in your SQL.

If Run includes the projector screen, focus on loan_days <= 7. If it excludes all events equipment, focus on the OR inside the parentheses. Checking one visible symptom at a time is faster than rearranging every operator at once.

This is a compound condition: several smaller comparisons work together to decide whether one row belongs in the result. The name matters less than the habit you just practiced. Write the grouping so another reader does not have to guess which rule applies to which alternatives.

Task

Change the filter in search.sql so it keeps tools or events items only when loan_days is at most 7.

Use parentheses around the two category alternatives, then run and submit the complete query.