SQL WHERE Clause
The SQL WHERE clause filters rows, so a query returns only the records that match a condition. Without WHERE, SELECT returns every row in the table; with it you can ask for "customers in Pakistan" or "products under $10".
WHERE is also used with UPDATE and DELETE to choose which rows to change, which makes it one of the most important clauses to get right.
Filtering Text Values
Text values are written in single quotes. This query finds all customers based in Pakistan.
SELECT CustomerName, City
FROM Customers
WHERE Country = 'Pakistan';CustomerName | City ---------------+---------- Alpha Traders | Lahore Blue Sky Foods | Karachi Golden Grains | Islamabad (3 rows)
Filtering Numbers
Numbers are written without quotes. Here we find products that cost more than 12.
SELECT ProductName, Price
FROM Products
WHERE Price > 12;ProductName | Price -------------+------ Basmati Rice | 12.5 Olive Oil | 18.75 Coffee Beans | 22 Almonds | 15 (4 rows)
SQL Comparison Operators
- = equal to
- <> not equal to (many databases also accept !=)
- > greater than
- < less than
- >= greater than or equal
- <= less than or equal
- BETWEEN, LIKE and IN are covered in their own chapters
Filtering Dates
In our sample database dates are stored as text in the YYYY-MM-DD format, which sorts and compares correctly. This finds orders placed on or after 1 April 2026. In MySQL and SQL Server you would normally use a DATE column, but the query looks the same.
SELECT OrderID, OrderDate, Quantity
FROM Orders
WHERE OrderDate >= '2026-04-01';OrderID | OrderDate | Quantity --------+------------+--------- 107 | 2026-04-01 | 8 108 | 2026-04-18 | 30 109 | 2026-05-07 | 15 110 | 2026-05-22 | 5 (4 rows)
Not Equal
Use <> to exclude a value. This lists every product that is not a beverage.
SELECT ProductName, Category
FROM Products
WHERE Category <> 'Beverages';ProductName | Category ---------------+----------- Basmati Rice | Grains Olive Oil | Oils Dark Chocolate | Snacks Almonds | Snacks Wheat Flour | Grains Honey | Sweeteners (6 rows)
Frequently asked questions
Can I use a column alias in the WHERE clause?
No, in standard SQL WHERE is evaluated before SELECT, so aliases defined in SELECT are not available. Repeat the expression or use a subquery.
What is the difference between != and <> in SQL?
<> is the SQL standard not-equal operator. != is accepted by most databases including MySQL, SQL Server, PostgreSQL and SQLite, and behaves the same.
Why does WHERE column = NULL return nothing?
NULL means unknown, and comparing anything to unknown is unknown, not true. Use IS NULL or IS NOT NULL instead, as explained in the NULL Values chapter.