Skip to content
IGCSE·Tuition
Computer Science · Lesson

Combine query conditions with AND, OR and brackets

Two queries can use the same conditions and still return different rows, because grouping decides the logic.

On this page
  1. How do the operators behave?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

AND keeps a record only if both conditions are true. OR keeps it if at least one is true. Brackets force a part of the condition to be worked out first.

Without brackets, AND is worked out before OR. That is why bracket placement is a favourite exam trap in databases and query reasoning.

How do the operators behave?

ABA AND BA OR B
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
FALSETRUEFALSETRUE
FALSEFALSEFALSEFALSE

Adding an AND makes a query stricter, so it can only remove rows. Adding an OR makes it more generous, so it can only add rows. The Boolean and number-representation lab builds truth tables like this one.

Worked example

Here is the Plants table.

PlantIDNameTypePriceStockIndoor
P01FernFoliage12.508TRUE
P02CactusSucculent6.0025TRUE
P03RoseFlowering15.000FALSE
P04MintHerb4.5040FALSE
P05OrchidFlowering28.003TRUE
P06BasilHerb5.0012FALSE
P07AloeSucculent9.5015TRUE
P08HibiscusFlowering18.006FALSE

Compare two queries.

SELECT Name FROM Plants
WHERE Type = "Herb" OR Type = "Succulent" AND Price > 6
SELECT Name FROM Plants
WHERE (Type = "Herb" OR Type = "Succulent") AND Price > 6

Query 1. AND first, so the condition means: Herb, OR (Succulent AND Price > 6). Mint and Basil are herbs, so both pass. Cactus is a succulent but 6.00 is not above 6, so it fails. Aloe is a succulent at 9.50, so it passes. Result: Mint, Basil, Aloe (3 rows).

Query 2. Brackets first: the plant must be a Herb or a Succulent, and cost more than 6. Mint 4.50 fails, Basil 5.00 fails, Cactus 6.00 fails, Aloe 9.50 passes. Result: Aloe (1 row).

Same words, different brackets, three rows against one.

The mistake to watch for

A common slip is to read a condition left to right and ignore AND’s priority.

Mistaken answer: “Query 1 returns only Aloe, because the price test applies to herbs and succulents.”

That reading adds brackets which the query does not have.

The correction: write the brackets in yourself, around the AND, before testing a single row. When you write a query, always add brackets, even where they are optional.

Check yourself

1. Which plants does WHERE Type = "Flowering" AND Indoor = TRUE return?

Show answer

Flowering: Rose (outdoor), Orchid (indoor), Hibiscus (outdoor). Only Orchid passes both.

2. How many rows does WHERE Stock = 0 OR Price > 20 return?

Show answer

Stock = 0: Rose. Price > 20: Orchid. Together 2 rows: Rose and Orchid.

3. Rewrite Type = "Herb" OR Type = "Foliage" AND Indoor = TRUE so that the Indoor test applies to both types.

Show answer
(Type = "Herb" OR Type = "Foliage") AND Indoor = TRUE

Where this leads next

Next, learn to explain why a query returns no rows. The module practice set includes more grouping questions.

If AND and OR still go wrong under exam pressure, a teacher in online one-to-one Computer Science tuition can give you new queries to bracket and test row by row.

Updated:

Your next step

If AND and OR combinations make you second-guess every answer, a one-to-one teacher can work through them with you on small tables until the pattern becomes automatic.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students