The WHERE Clause

How to filter rows based on conditions

← All topics

What is WHERE?

WHERE filters the rows returned by a query. Without it, SELECT returns every row in the table. Adding WHERE tells the database to only return rows where a specific condition is true.

SELECT column1, column2
FROM table_name
WHERE condition;

The condition is evaluated for every row. Rows where it is true are included; rows where it is false are excluded.

Filtering text values

To match a text value, use = and wrap the value in single quotes. SQL is generally case-insensitive for keywords like SELECT and WHERE, but the text values you compare against are often case-sensitive depending on your database.

-- Only return sales where the drink was a Latte
SELECT *
FROM coffee_shop_sales
WHERE drink_type = 'Latte';

Filtering numeric values

Numbers don't need quotes. You can use any of the standard comparison operators to filter on numeric columns.

-- Only return sales where the price was more than $5
SELECT *
FROM coffee_shop_sales
WHERE price_usd > 5;

💡 Comparison operators

OperatorMeaningExample
=Equal toWHERE size = 'Large'
!= or <>Not equal toWHERE size != 'Small'
>Greater thanWHERE price_usd > 5
>=Greater than or equal toWHERE price_usd >= 5
<Less thanWHERE price_usd < 10
<=Less than or equal toWHERE price_usd <= 10

Common mistakes

Quotes around numbers — writing WHERE price_usd > '5' may work in some databases but is incorrect. Numbers should never be quoted.

Using = for NULL — missing values in SQL are represented as NULL, and WHERE column = NULL will never match anything. You need WHERE column IS NULL instead. You'll cover this in a later topic.

WHERE comes after FROM — the clause order matters. SELECT … FROM … WHERE is correct; putting WHERE before FROM will cause an error.


Practice: WHERE

Apply what you learned above. Write a query that matches each task.

WHERE

Table Schema

Table:
Columns:

Task

Tip: Ctrl + Enter to submit