SQL · Level 1 Filtering

SQL AND, OR and Parentheses

Find cheap books and games: combine conditions with AND and OR, and use parentheses so the query means what you say.

  • AND, OR
  • AND is evaluated before OR
  • Parentheses group conditions

AND and OR combine conditions. Trap: AND is evaluated before OR, so a OR b AND c means a OR (b AND c). Add parentheses to say what you mean.

-- cheap books or games?
WHERE category = 'book' OR category = 'game' AND price < 20
-- really means: every book, plus the cheap games
WHERE (category = 'book' OR category = 'game') AND price < 20

Return the name of every product in the book or game category that costs less than 20, ordered by name. In the database below, Atlas is a book but too expensive.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

products 6 rows
idINTEGERnameTEXTcategoryTEXTpriceREAL
1Atlasbook35.0
2Chessgame15.0
3Dunebook9.5
4Lamphome12.0
5Unogame8.0
6Zeldagame59.0
solution.sql
Test Suites Pending
Output