Module 2 · Filtering data

Combine conditions with AND, OR and NOT

By the end you will be able to combine conditions to express various requirements.

About 14 min

0 of 52 lessons

Before you start, you need

Compare values in your filters

Combine complete conditions

A question can have several requirements. An available accessory must be in the Accessories category and be in stock. A customer from London or Bristol can live in either city.

AND requires both conditions to be met. OR accepts at least one. NOT negates a condition: NOT category = 'Audio' looks for products that are not Audio.

Usa parentheses to group the alternatives. (category = 'Accessories' OR category = 'Displays') AND stock > 0 requires stock for both categories. If you put the availability check next to Displays only, an out-of-stock accessory could also get in.

SQL evaluates AND before OR; the parentheses make the intent visible. Each alternative must be a complete condition: repeat the category comparison on both sides of OR. The practice tasks include an out-of-stock accessory so you can check the scope of the filter.

Minimal syntax
WHERE condition_1 AND condition_2
-- You can also use OR, NOT and parentheses.

See it in action

We want accessories or displays, as long as they are in stock.

SQL
SELECT name, stock
FROM products
WHERE (category = 'Accessories' OR category = 'Displays')
  AND stock > 0;
Example result: 5 rows
namestock
Mouse18
Keyboard7
Monitor 244
Monitor 272
Cable USB-C30
  1. The parentheses express two accepted categories. AND additionally requires stock greater than 0.
  2. Monitor 24 is in Displays and has stock. An out-of-stock accessory would not get through, because availability is required for both alternatives.
  3. Each side of OR needs a complete condition. category = 'Accessories' OR 'Displays' does not express two comparisons.
  4. NOT (category = 'Audio') negates the equality check and keeps the other categories. Here too, write the text in single quotes.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Require both category and stock

Not started

Write a query that returns name and stock for products in Accessories that have stock greater than 0.

Expected result: 3 rows with the columns name, stock, in that order. Row order does not matter.

This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.

View tables and data

Open this panel to see the full data.

Group the alternatives

Guided practice

Group the alternatives

Not started

Show name and stock for accessories or displays that are available. Require stock greater than 0 for both categories.

Expected result: 5 rows with the columns name, stock, in that order. Row order does not matter.

This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.

View tables and data

Open this panel to see the full data.

Negate a category

Guided practice

Negate a category

Not started

Show name and category for products that are not Audio. Use NOT.

Expected result: 7 rows with the columns name, category, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Change the scope of the filter

Not started

Show name and stock for all accessories, in stock or not, or for displays with stock greater than 0.

Expected result: 6 rows with the columns name, stock, in that order. Row order does not matter.

This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.

View tables and data

Open this panel to see the full data.

Key takeaways

  • AND requires both conditions.
  • OR accepts at least one alternative.
  • NOT negates a condition.
  • Parentheses make the scope explicit.

Next: Filter ranges and lists with BETWEEN and IN