Multiple Conditions Combined
Part of the Fundamentals section of Coddy's SQL journey — lesson 9 of 72.
Creating a query with only one condition is not sufficient. Sometimes we would like to check something more complicated. For that SQL (and many other programming languages) have the AND, OR, and NOT keywords to increase our ability to fetch the right result we need.
The AND and OR keywords are used like this:
SELECT col1, col2
FROM table1
WHERE condition1 AND condition2 OR condition3 ...We can stack as many conditions as we want together.
Challenge
EasyAvailable tables and columns:
<strong>candies</strong>:<strong>name</strong>,<strong>price</strong>,<strong>color</strong>,<strong>weight</strong>
Write a query to find all candies that are either:
- brown and cost 2 OR
- mixed and weigh less than 40
The query should return only the name and color of these candies.
Cheat sheet
Use AND, OR, and NOT keywords to create complex conditions in SQL queries:
SELECT col1, col2
FROM table1
WHERE condition1 AND condition2 OR condition3You can stack as many conditions as needed together using these logical operators.
Try it yourself
SELECT name, color
FROM candies
-- Write your code below
WHEREThis 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