Module 2 · Filtering data

Practice: choose the right filter

By the end you will be able to choose and combine filters based on a question.

About 22 min

0 of 52 lessons

Before you start, you need

Combine conditions with AND, OR and NOT · Filter ranges and lists with BETWEEN and IN · Find text patterns with LIKE · Work correctly with NULL values

The problem

You will prepare two lists. The first asks for products in certain categories with a stock level within a range. The second asks for customers whose name matches a pattern or who have no email address.

Before writing SQL, break the question down into conditions. Decide which column each one uses and whether they must all be met, or one is enough. Then choose which columns the answer needs.

Test the filter with three cases: a row that should get in, one that should stay out and one right on the boundary. For example, if 1 to 10 units are allowed, 1 and 10 must also get in.

You can look at the tables and ask for hints if you need them. In the challenge the question will change; apply the same way of thinking instead of copying the previous filter.

How to think about it

Is the right category enough when a stock range is also required?

  • Split the rule into an accepted category and a stock range. Both must be met.
  • Check one row that is included, one excluded by category and one excluded by quantity. The exact values 1 and 10 must be accepted.
  • For the customer challenge, decide whether one reason is enough to include someone or whether they must meet both.

Guided practice

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

Combine categories and stock

Not started

Write a query that returns name, category and stock for Accessories or Displays products whose stock is between 1 and 10.

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

View tables and data

Open this panel to see the full data.

Challenge

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

Practice

Pattern or missing value

Not started

Show name and email for customers whose name starts with M or who have no email.

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

View tables and data

Open this panel to see the full data.

Key takeaways

  • Turn each requirement into a condition.
  • Check a boundary and an excluded row.
  • Use AND or OR as the question requires.

Next: Sort and limit results