SQL WHERE
SQL

SQL WHERE Clause: Filter Rows with Conditions (Examples)

SQL Tutorial · Chapter 5 of 32

The SQL WHERE clause filters the rows a query works on by testing each row against a condition. Only rows where the condition is TRUE are returned by SELECT, changed by UPDATE or removed by DELETE. Without WHERE a statement touches every row, so it is the single most important clause for controlling exactly which data you get back.

SQL WHERE Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

A condition compares a column with a value or another column using an operator. Text values go in single quotes, numbers and dates follow the rules of your database (dates are usually quoted too, for example '2026-01-05').

Sample Tables

Customers
CustomerID Name City Country
1 Anita Sharma Mumbai India
2 John Smith London UK
3 Maria Garcia Madrid Spain
4 Wei Chen Singapore Singapore
5 Emma Brown Sydney Australia
Orders
OrderID CustomerID Product Amount OrderDate
101 1 Laptop 1200 2026-01-05
102 2 Mouse 25 2026-01-07
103 1 Monitor 300 2026-01-12
104 3 Keyboard 45 2026-02-02
105 6 Headset 80 2026-02-15
106 4 Laptop 1150 2026-03-01

Comparison Operators You Can Use in WHERE

Operator Meaning Example
= Equal to WHERE Country = ‘India’
<> or != Not equal to WHERE Product <> ‘Laptop’
>, < Greater / less than WHERE Amount > 100
>=, <= Greater / less than or equal WHERE Amount <= 80
BETWEEN Within a range (inclusive) WHERE Amount BETWEEN 40 AND 300
LIKE Pattern match WHERE Name LIKE ‘A%’
IN Matches any value in a list WHERE City IN (‘Mumbai’,’Sydney’)
IS NULL Value is missing WHERE OrderDate IS NULL

Example 1: Filter by a Text Value

SELECT Name, City
FROM Customers
WHERE Country = 'India';
Name City
Anita Sharma Mumbai

Example 2: Filter by a Number

Numbers are written without quotes. Here we find orders worth more than 100.

SELECT OrderID, Product, Amount
FROM Orders
WHERE Amount > 100;
OrderID Product Amount
101 Laptop 1200
103 Monitor 300
106 Laptop 1150

Example 3: Not Equal To

SELECT OrderID, Product
FROM Orders
WHERE Product <> 'Laptop';
OrderID Product
102 Mouse
103 Monitor
104 Keyboard
105 Headset

Example 4: Filter by Date

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate >= '2026-02-01';
OrderID OrderDate
104 2026-02-02
105 2026-02-15
106 2026-03-01

Example 5: WHERE in UPDATE and DELETE

WHERE is not only for SELECT. It decides which rows an UPDATE or DELETE affects, so leaving it out changes or deletes the entire table.

UPDATE Orders SET Amount = 30 WHERE OrderID = 102;
DELETE FROM Orders WHERE OrderID = 105;

After these two statements, order 102 costs 30 and order 105 no longer exists. Always run the matching SELECT with the same WHERE first to confirm which rows will be touched.

Works in MySQL, SQL Server, PostgreSQL, SQLite

  • All operators above are standard in the four systems. != is accepted everywhere but <> is the ANSI form.
  • Text comparison is case-insensitive by default in MySQL and SQL Server, case-sensitive in PostgreSQL and SQLite. Use LOWER(Country) = 'india' for portable matching.
  • Date literals in ISO format 'YYYY-MM-DD' work in all four.

How WHERE Is Evaluated

The database tests the WHERE condition on each row, keeping it when the result is TRUE and discarding it when the result is FALSE or UNKNOWN. UNKNOWN arises from comparisons with NULL, which is why Amount <> 100 silently drops rows where Amount is missing. If the filtered column has an index, the engine jumps straight to the matching rows instead of scanning the whole table, so WHERE conditions on indexed columns such as primary keys are the fastest filters you can write.

Common Mistakes

  • Missing quotes around text: WHERE City = Mumbai looks for a column called Mumbai.
  • Using = NULL instead of IS NULL; nothing equals NULL, so the query returns no rows.
  • Referring to a column alias from the SELECT list inside WHERE; WHERE runs before the alias exists.
  • Forgetting WHERE on UPDATE or DELETE and changing every row.

Practice Exercise

  1. Return the Name of every customer who is not from Australia.
  2. Return OrderID and Amount for orders under 100 placed on or before 2026-02-02.
Show answers
-- 1
SELECT Name FROM Customers WHERE Country <> 'Australia';
-- Anita Sharma, John Smith, Maria Garcia, Wei Chen

-- 2
SELECT OrderID, Amount FROM Orders
WHERE Amount < 100 AND OrderDate <= '2026-02-02';
-- 102 (25), 104 (45)

Related Chapters

FAQ

What does the WHERE clause do in SQL?

It filters rows. Each row is tested against the condition and only rows where the condition is TRUE are returned, updated or deleted.

Can I use WHERE with UPDATE and DELETE?

Yes, and you almost always should. Without WHERE, UPDATE changes every row and DELETE removes every row in the table.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY has run. Use HAVING when the condition involves an aggregate such as COUNT or SUM.

Chapter 5 of 32 · SQL from Scratch: all 32 chapters

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com