Either One with OR
At least one must be true.
Helen wants to know how many orders went wrong. An order went wrong if it was cancelled, or if it was returned. Either one counts. That's the job of or.

- Which orders went wrong?
- Cancelled, or returned
- Either one counts
One of them is enough
With or, a row is kept if at least one test is true. Status equals cancelled, or status equals returned. Run it. Two hundred and three orders went wrong. That's worth looking into.
SELECT COUNT(*)
FROM orders
WHERE status = 'cancelled'
OR status = 'returned';โโโโโโโโโโโโ โ COUNT(*) โ โโโโโโโโโโโโค โ 203 โ โโโโโโโโโโโโ
The classic trap
Now a trap that catches almost everyone. We want thrillers or mysteries, under twelve dollars. Run it. That's wrong! There are thrillers here that cost more than twelve. The reason is that SQL does and before or, just like multiplication comes before addition in maths. So it read this as, any thriller, or a mystery under twelve.
SELECT title, genre, price
FROM books
WHERE genre = 'Thriller'
OR genre = 'Mystery'
AND price < 12;โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโ โ title โ genre โ price โ โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโค โ Code of the River โ Thriller โ 12.99 โ โ Moonlight Express โ Thriller โ 11.99 โ โ Murder at Mill Lane โ Mystery โ 10.99 โ โ Deep Water โ Thriller โ 13.49 โ โ Night Train to Oslo โ Thriller โ 14.99 โ โโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโ
Brackets fix it
The fix is brackets. We put the two genre tests inside brackets, so they're checked together first. Then the price test applies to the whole group. Run it. Now we get only the two books that are a thriller or a mystery, and under twelve dollars. Whenever you mix and with or, use brackets. Always.
SELECT title, genre, price
FROM books
WHERE (genre = 'Thriller' OR genre = 'Mystery')
AND price < 12;โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโ โ title โ genre โ price โ โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโค โ Moonlight Express โ Thriller โ 11.99 โ โ Murder at Mill Lane โ Mystery โ 10.99 โ โโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโ
Summary
Or needs just one test to pass, and brackets keep and and or in the right order. Next, two shortcuts that make long lists of ors much shorter.
Key takeaways- OR needs one true test
- AND runs before OR
- Mixing them? Use brackets