Skip to content

2.1 — WHERE: The SQL Filter

What You'll Learn

  • How to filter data in SQL using the WHERE clause
  • The Excel equivalent of SQL filtering
  • Basic comparison operators

From Excel to SQL: Filtering Rows

In Excel, you use filters to show only the rows you care about. In SQL, you use the WHERE clause to do the same.

Excel:
Apply a filter to the "Product" column to show only "Apples".

SQL:

sql
SELECT * FROM sales
WHERE Product = 'Apples';
  • WHERE tells SQL to return only rows that match your condition.
  • Product = 'Apples' is the condition.

Common Comparison Operators

OperatorMeaningExample
=EqualsProduct = 'Apples'
<>Not equalProduct <> 'Oranges'
>Greater thanAmount > 100
<Less thanAmount < 100
>=Greater or eq.Amount >= 100
<=Less or eq.Amount <= 100

Special Note on NULL Values

In SQL, missing data is called NULL.

  • WHERE Amount = NULL does not work.
  • Use WHERE Amount IS NULL to find rows with missing values in the Amouont column.
  • Use WHERE Amount IS NOT NULL to exclude rows with missing values.

Tip:
If you want to treat NULL values as a default (for example, treat missing amounts as 0), you can use the COALESCE function:

sql
SELECT *, COALESCE(Amount, 0) AS Amount_Defaulted
FROM sales
WHERE COALESCE(Amount, 0) > 100;
  • COALESCE(Amount, 0) returns the value of Amount if it is not NULL, otherwise returns 0.
  • This is useful when you want to include or filter rows as if missing values had a default.

Key Points

  • Use WHERE to filter rows, just like Excel filters.
  • Combine with comparison operators for flexible filtering.
  • Special care is needed for NULL values.

Next Steps

Next, you'll learn how to filter numbers and ranges—just like Excel's number filters.