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. 

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>

Put parenthesis in the right place so that the query will find all retired people who are either over 30 years old OR have a seeking status.

Cheat sheet

Use parentheses to control the order of operations when combining multiple conditions with AND and OR:

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

Returns all people under 30 (any gender) plus all females over 20.

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

Returns only people over 20 who are either under 30 or female.

Try it yourself

SELECT * FROM people
WHERE age > 30 OR status = 'seeking' AND status = 'retired'
quiz iconTest yourself

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

All lessons in Fundamentals