Menu
Coddy logo textTech

Parenthesis

Part of the Fundamentals section of Coddy's SQL journey — lesson 10 of 72.

It is important to use parenthesis when combining different conditions.

Without parenthesis SQL does not simply read the conditions left to right: AND is evaluated before OR, because AND has the higher precedence. Parenthesis let you override that order and group the conditions the way you actually mean them.

For example the following queries look similar but they are not the same:

WHERE age < 30 OR gender = 'female' AND age > 20
WHERE (age < 30 OR gender = 'female') AND age > 20

The first query returns all people under 30 (any gender) plus all females over 20.

The second query returns only people over 20 who are either under 30 or female.

challenge icon

Challenge

Easy

Available tables and columns:

  • <strong>people</strong>: <strong>name</strong>, <strong>age</strong>, <strong>status</strong>

Add the missing condition and put parenthesis in the right place so that the query will find people who are either retired AND over 30 years old, OR who have a seeking status.

Try it yourself

SELECT * FROM people
WHERE age > 30 AND status = 'retired' -- Add the condition for 'seeking' status
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals