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 > 20WHERE (age < 30 OR gender = 'female') AND age > 20The 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
EasyAvailable 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' statusThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns