Menu
Coddy logo textTech

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 icon

Challenge

Easy

Available 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 condition3

You can stack as many conditions as needed together using these logical operators.

Try it yourself

SELECT name, color 
FROM candies
-- Write your code below
WHERE
quiz iconTest yourself

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

All lessons in Fundamentals