Database Management · Beginner level

Beginner level · Module 2: Filtering and Sorting with SQL · Lesson 1 of 3

Filtering Rows with WHERE

Select rows that meet conditions, combining them with AND, OR and NOT.

In this lesson you will

  • Write conditions with comparison operators
  • Combine conditions correctly with AND, OR and parentheses

Key concepts

WHEREcomparison operatorsAND / OR / NOToperator precedence

The lesson

WHERE keeps only rows that meet a condition: WHERE amount >= 10000 or WHERE class = 'S2'. Text values go in single quotes; numbers do not.

AND requires both conditions; OR requires at least one. AND is evaluated before OR, so WHERE class = 'S1' OR class = 'S2' AND paid = 0 does not mean what most people expect. Use parentheses to make the meaning clear.

Dates stored as text in the ISO format YYYY-MM-DD compare correctly, so WHERE visit_date >= '2026-03-01' finds visits from March onwards.

Step by step

  1. Find visits with a given diagnosis.
  2. Find visits in a date range with AND.
  3. Find visits for two diagnoses with OR and parentheses.
  4. Predict, then check, the row count of each query.

Practice activity

Answer five filtering questions about the clinic visits table.

Expected result: Each query returns exactly the rows that match the question.

Check your understanding

  1. Which is evaluated first, AND or OR?

    Show answer

    AND.

  2. Why do ISO dates such as 2026-03-01 compare correctly as text?

    Show answer

    The most significant part (year) comes first and every part has a fixed width.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.