Tutorials › SQL Tutorial › SQL WHERE

SQL WHERE

9 min read · Lesson 5 of 24

So far every query has returned every row of a table. Real questions are usually narrower: customers in Kisumu, products under KSh 2,000, orders still pending. The WHERE clause filters rows so you only get the ones that match a condition.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

The database checks the condition for each row. Rows where it is true are kept; all others are left out.

Filtering on text

Find every customer based in Kisumu:

Example SQL
SELECT name, contact, city
FROM customers
WHERE city = 'Kisumu';
Try it Yourself »

Text values go inside single quotes. Without the quotes, SQL would think Kisumu was the name of a column and give an error.

Note: Whether text comparisons are case-sensitive depends on the database. In SQLite and PostgreSQL, 'kisumu' does not equal 'Kisumu'. In MySQL with its usual settings, they are equal. Match the exact case of the stored data to be safe.

Filtering on numbers

Numbers are written without quotes. Find products that cost less than KSh 2,000:

Example SQL
SELECT name, price
FROM products
WHERE price < 2000;
Try it Yourself »

Comparison operators

OperatorMeaningExample
=equal tostatus = 'pending'
<>not equal tocountry <> 'Kenya'
!=not equal to (widely supported, not in the original standard)country != 'Kenya'
>greater thanstock > 50
<less thanprice < 1000
>=greater than or equal toprice >= 5000
<=less than or equal toquantity <= 2

Products with 50 or more units in stock:

Example SQL
SELECT name, stock
FROM products
WHERE stock >= 50;
Try it Yourself »

Orders that are not delivered yet (or were cancelled):

Example SQL
SELECT order_id, customer_id, status
FROM orders
WHERE status <> 'delivered';
Try it Yourself »

Filtering on dates

In the Skope Shop database dates are stored as text in the form YYYY-MM-DD. Because the year comes first, comparing them as text gives the right chronological order. Orders placed from 1 May 2024 onwards:

Example SQL
SELECT order_id, order_date
FROM orders
WHERE order_date >= '2024-05-01';
Try it Yourself »
Tip: Always store dates as YYYY-MM-DD (the ISO 8601 format). Formats like 05/01/2024 cannot be compared or sorted correctly as text, and they are ambiguous: is that 5 January or 1 May?

Conditions using calculations

The condition can use any expression, including arithmetic on columns. Which products have more than KSh 200,000 of value sitting in the warehouse?

Example SQL
SELECT name, price, stock
FROM products
WHERE price * stock > 200000;
Try it Yourself »

WHERE is not only for SELECT

The same WHERE clause decides which rows are changed by UPDATE and which rows are removed by DELETE. There, getting the condition wrong can change far more data than you meant to, so it pays to master WHERE now.

Warning: You cannot find empty values with = NULL. The condition country = NULL is never true. Use country IS NULL instead, as explained in NULL Values.

Summary

  • WHERE keeps only the rows for which the condition is true.
  • Quote text values, don't quote numbers.
  • Use =, <>, <, >, <=, >= to compare.

To combine several conditions, such as "in Kenya and joined in 2024", continue to AND, OR, NOT.

Exercise

Show customers whose country is Tanzania.

SELECT name FROM customers  country = ;
Exercise

Show products that are out of stock (stock is zero).

SELECT name FROM products WHERE stock  0;
Sign in or create a free account to track progress, earn XP, badges and a certificate.