TExcelTAD
Home › SQL › SQL WHERE

SQL WHERE

The WHERE clause filters rows: only rows for which the condition is true make it into the result. It comes right after FROM.

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

A simple filter

Which customers are based in Germany?

sql · try it yourself
SELECT customer_id, name, city
FROM customers
WHERE country = 'Germany'
ORDER BY customer_id;
customer_id | name           | city
------------+----------------+-------
1           | Corner Cafe    | Berlin
13          | Priya Kowalski | Berlin
(2 rows)

Text values go in single quotes; numbers do not:

sql · try it yourself
SELECT name, price
FROM products
WHERE price > 40
ORDER BY price DESC;
name                        | price
----------------------------+------
Noise-Cancelling Headphones | 149
Mechanical Keyboard         | 89
Coffee Grinder              | 59.9
Chef Knife                  | 45
(4 rows)

Comparison operators

Operator Meaning Example
= equal status = 'shipped'
<> or != not equal status <> 'cancelled'
> / >= greater (or equal) stock >= 100
< / <= less (or equal) price < 3
BETWEEN a AND b within a range (inclusive) price BETWEEN 5 AND 10
LIKE text pattern name LIKE 'C%'
IN (…) one of a list country IN ('UK', 'Ireland')
IS NULL value is missing shipped_date IS NULL

BETWEEN, LIKE, IN and IS NULL each get their own lesson later.

Filtering dates

TAD Market stores dates as ISO text ('2025-06-13'). Because that format sorts like the calendar, ordinary comparisons work:

sql · try it yourself
SELECT order_id, order_date, status
FROM orders
WHERE order_date >= '2025-10-01'
ORDER BY order_date, order_id;
order_id | order_date | status
---------+------------+-----------
40       | 2025-10-08 | processing
38       | 2025-10-09 | processing
39       | 2025-10-09 | processing
37       | 2025-10-20 | processing
(4 rows)

Not equal

sql · try it yourself
SELECT employee_id, first_name, title
FROM employees
WHERE title <> 'Sales Rep'
ORDER BY employee_id;
employee_id | first_name | title
------------+------------+-------------------
1           | Grace      | CEO
2           | Victor     | Sales Manager
3           | Lin        | Operations Manager
7           | Marco      | Warehouse Lead
8           | Hana       | Support Specialist
10          | Elena      | Data Analyst
(6 rows)

WHERE with calculations

The condition can be any expression, including arithmetic on columns. Which products have more than 1,000 worth of stock on the shelf?

sql · try it yourself
SELECT name, price, stock, ROUND(price * stock, 2) AS stock_value
FROM products
WHERE price * stock > 1000
ORDER BY stock_value DESC;
name                        | price | stock | stock_value
----------------------------+-------+-------+------------
The SQL Handbook            | 29    | 50    | 1450
USB-C Cable                 | 9.99  | 140   | 1398.6
Noise-Cancelling Headphones | 149   | 8     | 1192
Mechanical Keyboard         | 89    | 12    | 1068
Green Tea                   | 8.75  | 120   | 1050
Python Recipes              | 34.5  | 30    | 1035
(6 rows)

Note: You cannot use the alias stock_value in WHERE in standard SQL, because WHERE is evaluated before the SELECT list. Repeat the expression instead. (SQLite happens to allow the alias, but other databases do not.)

WHERE in UPDATE and DELETE

WHERE is not only for SELECT. It also decides which rows an UPDATE or DELETE touches — forgetting it changes every row. You will see this in the UPDATE and DELETE lessons.

Tip: Before running an UPDATE or DELETE, run a SELECT with the same WHERE to see which rows would be affected.

Test yourself

Which operator means "not equal" in standard SQL?

  1. =!
  2. <>
  3. NOT=
  4. =/=

Which condition finds orders placed in 2025-09 or later?

  1. order_date > 2025-09
  2. order_date >= '2025-09-01'
  3. order_date >= "September"
  4. MONTH >= 9

What happens if you run DELETE FROM orders; without WHERE?

  1. Nothing
  2. An error
  3. Every row is deleted
  4. Only the first row is deleted