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 > 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>
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 > 20Returns all people under 30 (any gender) plus all females over 20.
WHERE (age < 30 OR gender = 'female') AND age > 20Returns 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'This 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