SQL WHERE Clause: Syntax, Examples & Practice
The SQL WHERE clause filters rows based on a condition. It is used with SELECT, UPDATE and DELETE statements, and only the rows that satisfy the condition are returned or affected.
What Is the SQL WHERE Clause?
WHERE is how you tell a database which rows you want. The database checks your condition against every row in the table: rows where the condition is true are kept, and rows where it is false — or unknown, because of a NULL value — are skipped. Leave WHERE out and the statement applies to every row.
SELECT … WHEREreturns only the matching rows.UPDATE … WHEREchanges only the matching rows.DELETE … WHEREremoves only the matching rows.
Basic WHERE Syntax
SELECT column1, column2
FROM table_name
WHERE condition;
The condition is usually a column, an operator and a value — for example marks > 80.
A Simple WHERE Example
SELECT *
FROM students
WHERE marks > 80;
| name | class | marks |
|---|---|---|
| Priya | 10 | 92 |
| Aman | 9 | 85 |
| Alice | 10 | 88 |
| Bob | 8 | 42 |
| Karan | 10 | 76 |
| name | class | marks |
|---|---|---|
| Priya | 10 | 92 |
| Aman | 9 | 85 |
| Alice | 10 | 88 |
The query reads the students table and keeps only the rows where marks is greater than 80. Priya, Aman and Alice are returned; Bob (42) and Karan (76) are filtered out. The * means "all columns", so WHERE decides which rows appear, not which columns.
Think of WHERE as a Filter
WHERE with UPDATE and DELETE
WHERE works the same way when you change data: it picks the rows the statement is allowed to touch.
-- Change only the rows that match
UPDATE students
SET class = 11
WHERE class = 10;
-- Remove only the rows that match
DELETE FROM students
WHERE marks < 35;
UPDATE or DELETE without a WHERE clause affects every row in the table. Run the same condition as a SELECT first to check which rows it matches.
Where Is WHERE Used in Real Systems?
SQL WHERE Clause Syntax
The exact structure SQL expects — get this right every time
Every WHERE condition follows the same pattern: a column, an operator, and a value.
SELECT column1, column2
FROM table_name
WHERE column_name operator value;
SELECTcomes first — pick which columns (new to SELECT? start with the BIG 6 SQL clauses)FROMcomes second — pick which tableWHEREcomes third — define the filter condition- A condition follows the pattern:
column operator value - Text values need single quotes:
'Delhi','Active' - Numbers and booleans need no quotes:
80,10,true(see SQL data types)
Multiple Conditions Syntax
-- Both conditions must be true (AND)
SELECT *
FROM students
WHERE class = 10 AND marks > 75;
-- Either condition can be true (OR)
SELECT *
FROM students
WHERE city = 'Delhi' OR city = 'Mumbai';
Where WHERE Goes: SQL Clause Order
SELECT *
WHERE marks > 80
FROM students;
SELECT *
FROM students
WHERE marks > 80;
WHERE is written third. It comes after FROM — including any SQL JOINs that combine tables — and before GROUP BY, HAVING and ORDER BY. That is why WHERE filters individual rows before any grouping or sorting happens.
SQL WHERE Clause Examples
Filtering numbers, text and dates — the three things you'll do most
WHERE with Numbers
Numeric values are written without quotes and work with every comparison operator.
School"Show only class 10 students"
SELECT *
FROM students
WHERE class = 10;
| name | class |
|---|---|
| Priya | 10 |
| Alice | 10 |
| Karan | 10 |
Shopping"Show products cheaper than ₹500"
SELECT *
FROM products
WHERE price < 500;
| product | price |
|---|---|
| Pencil | ₹10 |
| Notebook | ₹120 |
| Bag | ₹450 |
WHERE with Text
Text values go inside single quotes. The match is exact — 'Delhi' does not match 'New Delhi'. For partial matches, use the SQL LIKE operator.
Cities"Show students who live in Delhi"
SELECT *
FROM students
WHERE city = 'Delhi';
| name | city |
|---|---|
| Priya | Delhi |
| Karan | Delhi |
WHERE with Dates
Dates are written as quoted strings in 'YYYY-MM-DD' format and can be compared like numbers — earlier dates are "less than" later ones. To extract a year or month before filtering, see SQL date functions.
Orders"Show orders between January and June 2026"
SELECT *
FROM orders
WHERE order_date BETWEEN
'2026-01-01' AND '2026-06-01';
| order_id | order_date |
|---|---|
| 001 | 2026-02-14 |
| 002 | 2026-04-01 |
| 003 | 2026-05-30 |
SQL WHERE Operators
Your tools for filtering — pick an operator, or expand any of them below
Equal to (=)
Returns rows where the column exactly matches the value.
SELECT *
FROM students
WHERE class = 10;
| name | class |
|---|---|
| Priya | 10 |
| Aman | 10 |
Not equal to (!= or <>)
Returns rows that do not match the value. <> is the standard SQL spelling; != works in almost every database too.
SELECT *
FROM students
WHERE class != 10;
| name | class |
|---|---|
| Alice | 9 |
| Bob | 8 |
Greater than (>)
Returns rows where the column is strictly larger than the value. A row with exactly 80 marks is not included.
SELECT *
FROM students
WHERE marks > 80;
| name | marks |
|---|---|
| Priya | 92 |
| Aman | 85 |
Less than (<)
Returns rows where the column is strictly smaller than the value.
SELECT *
FROM students
WHERE marks < 50;
| name | marks |
|---|---|
| Bob | 42 |
Greater than or equal to (>=)
Like >, but the boundary value itself is included — a student with exactly 80 marks matches.
SELECT *
FROM students
WHERE marks >= 80;
| name | marks |
|---|---|
| Priya | 92 |
| Aman | 80 |
Less than or equal to (<=)
Returns rows where the column is at most the value, boundary included.
SELECT *
FROM products
WHERE price <= 500;
| product | price |
|---|---|
| Pencil | 10 |
| Book | 250 |
BETWEEN (range)
Matches values inside a range. BETWEEN is inclusive: marks BETWEEN 70 AND 90 means the same as marks >= 70 AND marks <= 90. It works with numbers, dates and text.
SELECT *
FROM students
WHERE marks BETWEEN 70 AND 90;
| name | marks |
|---|---|
| Alice | 75 |
| Aman | 85 |
LIKE (pattern match)
Matches text against a pattern instead of an exact value. % stands for any number of characters, so 'A%' means "starts with A". The WHERE with LIKE section below covers every wildcard.
SELECT *
FROM students
WHERE name LIKE 'A%';
| name |
|---|
| Alice |
| Aman |
IN (list of values)
Matches any value in a list — a shorter way to write several OR conditions on the same column. NOT IN, subqueries and NULL pitfalls are covered in the SQL WHERE IN operator lesson.
SELECT *
FROM students
WHERE class IN (9, 10);
| name | class |
|---|---|
| Alice | 9 |
| Priya | 10 |
IS NULL and IS NOT NULL — explained in WHERE and NULL values below.
Using AND and OR with WHERE
Combine conditions — this is where SQL WHERE gets really powerful
🔗 AND — both conditions must be true
SELECT *
FROM students
WHERE class = 10
AND marks > 80;
| name | class | marks |
|---|---|---|
| Priya | 10 | 92 |
| Alice | 10 | 85 |
🔀 OR — any condition can be true
SELECT *
FROM students
WHERE class = 10
OR class = 9;
| name | class | marks |
|---|---|---|
| Priya | 10 | 92 |
| Alice | 10 | 85 |
| Aman | 9 | 78 |
| Bob | 9 | 65 |
Both conditions must be true
Narrows down results — fewer rows returned
Any condition can be true
Expands results — more rows returned
Combining AND + OR — Use Parentheses
SELECT *
FROM users
WHERE is_premium = 1
AND city = 'Houston'
OR city = 'Austin';
-- Returns: (premium AND Houston) OR anyone in Austin: 13 users
SELECT *
FROM users
WHERE is_premium = 1
AND (city = 'Houston'
OR city = 'Austin');
-- Returns: premium users in Houston or Austin: 7 users
Checking one column against several values? city IN ('Houston', 'Austin') says the same thing as the OR above with less typing — see filtering multiple values with WHERE IN.
WHERE and NULL Values
The most misunderstood part of SQL WHERE — NULL is not zero
The Golden Rule: Never Use = NULL
WHERE column = NULL always returns 0 rows. NULL cannot be compared with =. Use IS NULL instead.
SELECT *
FROM employees
WHERE manager_id = NULL;
-- NULL = NULL is NULL, not TRUE
-- So no rows match
SELECT *
FROM employees
WHERE manager_id IS NULL;
-- Returns employees with
-- no assigned manager
IS NOT NULL — Finding Rows That Have a Value
-- Employees who have a manager
SELECT first_name, last_name, manager_id
FROM employees
WHERE manager_id IS NOT NULL; -- 110 of 120 employees
-- Orders that have shipped
SELECT id, status, shipped_date
FROM orders
WHERE shipped_date IS NOT NULL; -- 103 of 200 orders
Why Does = NULL Fail?
NULL = NULL evaluates to NULL — not TRUE. WHERE only returns rows where the condition is TRUE. So WHERE column = NULL effectively matches nothing, because the result is always NULL.
Real Business Examples
-- Orders that haven't been shipped yet
SELECT id, total
FROM orders
WHERE shipped_date IS NULL; -- 97 orders
-- Employees with no manager (the top of the org chart)
SELECT first_name, last_name, department
FROM employees
WHERE manager_id IS NULL; -- 10 employees
Column has no value
Missing data, optional fields left blank
Column has a value
Required data that was filled in
Filtering NULLs out is one option; replacing them with a default value is another. That is what the COALESCE function is for.
WHERE vs HAVING
One of the most common SQL interview questions — know this cold
Side-by-Side Comparison
| Feature | WHERE | HAVING |
|---|---|---|
| Filters | Individual rows | Grouped results |
| Position in query | Before GROUP BY | After GROUP BY |
| Runs at | Row-level (pre-aggregation) | Group-level (post-aggregation) |
| Can use aggregates? | ✗ No (COUNT, SUM, AVG…) | ✓ Yes |
| Requires GROUP BY? | ✗ No | ✓ Yes |
| Example | WHERE marks > 50 | HAVING COUNT(*) > 5 |
WHERE — Filter Before Grouping
-- Step 1: WHERE removes rows with marks ≤ 50
-- Step 2: GROUP BY groups remaining rows by class
-- Step 3: AVG() is calculated on the filtered rows
SELECT class, AVG(marks) AS avg_marks
FROM students
WHERE marks > 50
GROUP BY class;
HAVING — Filter After Grouping
-- Step 1: GROUP BY groups ALL rows by class
-- Step 2: COUNT(*) counts each group
-- Step 3: HAVING removes groups with fewer than 5 students
SELECT class, COUNT(*) AS total_students
FROM students
GROUP BY class
HAVING COUNT(*) >= 5;
Using Both Together
-- WHERE filters failing students first (marks > 40)
-- GROUP BY groups passing students by class
-- HAVING keeps only classes with 5 or more students
SELECT class, COUNT(*) AS passing_students
FROM students
WHERE marks > 40
GROUP BY class
HAVING COUNT(*) >= 5;
SQL WHERE Clause with LIKE
Pattern matching — search text columns with wildcards
The Two Wildcards
| Wildcard | Meaning | Example Pattern | Matches |
|---|---|---|---|
| % | Any sequence of characters (including none) | 'A%' | Alice, Aman, Arjun |
| _ | Exactly one character | '_a%' | Rahul, Manish, Karan |
| %x% | Contains x anywhere | '%ith%' | Smith, Prithvi, Rithesh |
| %x | Ends with x | '%son' | Johnson, Anderson |
| x% | Starts with x | 'De%' | Delhi, Denver, Dean |
LIKE Examples
-- First names starting with 'A'
SELECT first_name, last_name FROM users WHERE first_name LIKE 'A%';
-- 14 users: Anthony, Angela, Amy, Andrew, Amanda, Ashley ...
-- Last names ending with 'son'
SELECT first_name, last_name FROM users WHERE last_name LIKE '%son';
-- 14 users: Jackson, Wilson, Robinson, Johnson, Anderson, Nelson ...
-- Email addresses from Gmail
SELECT first_name, email FROM users WHERE email LIKE '%@gmail.com';
-- 28 users
-- Product names containing 'phone'
SELECT name FROM products WHERE name LIKE '%phone%';
-- Magnetic Phone Mount Pro, Noise-Cancelling Headphones
-- First names whose second letter is 'a'
SELECT first_name FROM users WHERE first_name LIKE '_a%';
-- 41 users: Barbara, Carol, David, James, Karen, Laura ...
NOT LIKE — Exclude Patterns
-- Find all products that are NOT electronics
SELECT * FROM products
WHERE category NOT LIKE '%electronics%';
-- Exclude test accounts
SELECT * FROM users
WHERE email NOT LIKE '%test%'
AND email NOT LIKE '%example%';
LIKE 'term%' (prefix match) is fast and can use a database index. LIKE '%term%' (contains) performs a full table scan — use it carefully on large tables. Case sensitivity, escaping wildcards and more patterns are covered in the full SQL LIKE operator lesson.
Common SQL WHERE Mistakes
Avoid these — every SQL beginner makes them at least once
WHERE before FROM
SELECT *
WHERE marks > 80
FROM students;SELECT *
FROM students
WHERE marks > 80;SQL requires a strict clause order. WHERE must come after FROM, not before it.
Using = NULL instead of IS NULL
SELECT *
FROM employees
WHERE manager_id = NULL;SELECT *
FROM employees
WHERE manager_id IS NULL;NULL = NULL evaluates to NULL (unknown), not TRUE. Always use IS NULL.
AND when OR is needed for the same column
SELECT *
FROM students
WHERE city = 'Delhi'
AND city = 'Mumbai';SELECT *
FROM students
WHERE city = 'Delhi'
OR city = 'Mumbai';A column can't equal two different values simultaneously. AND here returns 0 rows.
Forgetting quotes around text values
SELECT *
FROM students
WHERE name = Priya;SELECT *
FROM students
WHERE name = 'Priya';Without quotes, SQL reads Priya as a column name, not a text value — causing an error.
Using WHERE with aggregate functions
SELECT class, COUNT(*)
FROM students
WHERE COUNT(*) > 10
GROUP BY class;SELECT class, COUNT(*)
FROM students
GROUP BY class
HAVING COUNT(*) > 10;WHERE runs before GROUP BY, so it cannot see aggregate results. Filter groups with HAVING instead.
Missing parentheses with mixed AND + OR
WHERE is_premium = 1
AND city = 'Houston'
OR city = 'Austin'WHERE is_premium = 1
AND (city = 'Houston'
OR city = 'Austin')AND has higher precedence than OR. Without parentheses, the logic is applied in the wrong order.
Practice SQL WHERE Queries
Learn → Practice — write the queries yourself
These five exercises use the tables in the SQLabHub SQL Playground (products, employees, orders and users). Write each query yourself first, run it, then compare it with the solution.
-
1Find every product that costs less than 50.
Show solution
SELECT name, price FROM products WHERE price < 50; -
2List employees whose salary is between 50000 and 80000.
Show solution
SELECT first_name, last_name, salary FROM employees WHERE salary BETWEEN 50000 AND 80000; -
3Show orders that have not been shipped yet.
Show solution
SELECT id, order_date, status FROM orders WHERE shipped_date IS NULL; -
4Find users with a Gmail address.
Show solution
SELECT first_name, email FROM users WHERE email LIKE '%@gmail.com'; -
5List products rated 4 or higher that are also priced under 100.
Show solution
SELECT name, price, rating FROM products WHERE rating >= 4 AND price < 100;
📝 Quiz — SQL WHERE Clause
Test your understanding · 0/10 answered
What does the WHERE clause do in SQL?
WHERE filters rows — only rows that match the condition are returned. It's like telling SQL "I don't want everything, just this specific data."
Which SQL query correctly shows students with marks greater than 80?
The correct order is SELECT → FROM → WHERE. WHERE always comes after FROM, never before it.
You want students in class 9 OR class 10. Which query is correct?
OR returns rows where any condition is true. A student can't be in both class 9 AND 10 at once — that's why AND would return 0 rows here.
Which operator would you use to find all product names starting with "A"?
LIKE with the % wildcard matches any characters after "A". So LIKE 'A%' finds "Apple", "Aman", "Axis", etc.
You want orders placed between Jan 1 and June 30, 2026. Which clause works?
BETWEEN is perfect for ranges — it includes both endpoints. It's cleaner than writing two separate > and < conditions.
What happens if you use AND instead of OR — "WHERE class=10 AND marks>90"?
AND requires BOTH conditions to be true. Only students who satisfy both (class=10 and marks>90) appear in the result.
Which query correctly finds employees with no manager (NULL manager_id)?
NULL cannot be compared with = or ==. Always use IS NULL. WHERE column = NULL always returns 0 rows because NULL = NULL evaluates to NULL, not TRUE.
You grouped students by class and want only classes with more than 5 students. Which clause do you use?
HAVING filters after GROUP BY aggregation. WHERE runs before grouping and cannot use aggregate functions like COUNT(). Use HAVING for group-level filtering.
Which LIKE pattern finds product names that contain "phone" anywhere?
% on both sides means 'match anything before AND after'. LIKE '%phone%' finds 'smartphone', 'headphone', 'phone case', etc.
What is wrong with: WHERE city = 'Delhi' AND city = 'Mumbai'?
A single column cannot simultaneously equal two different values. This condition is always FALSE. Use OR: WHERE city = 'Delhi' OR city = 'Mumbai'.
SQL WHERE Interview Questions
Beginner to intermediate — with model answers
Q1: What does the SQL WHERE clause do?
Q2: What is the difference between WHERE and HAVING?
COUNT() or SUM(); HAVING can. Use WHERE for row-level filters, HAVING for group-level filters.Q3: Can the WHERE clause use aggregate functions like COUNT() or SUM()?
Q4: How do you filter NULL values using WHERE?
IS NULL to find rows with missing values and IS NOT NULL for rows with a value. Never use = NULL — NULL comparisons return NULL (not TRUE), so WHERE column = NULL always returns zero rows.Q5: What is the difference between AND and OR in a WHERE clause?
Q6: What is the IN operator and when should you use it?
WHERE class IN (9, 10) is equivalent to WHERE class = 9 OR class = 10 but is cleaner and easier to read, especially with more values. The WHERE IN lesson goes deeper.Q7: What wildcards does the LIKE operator support?
% matches any sequence of characters (including zero characters). _ matches exactly one character. Examples: LIKE 'A%' (starts with A), LIKE '%son' (ends with son), LIKE '%phone%' (contains phone), LIKE '_a%' (second character is a).Q8: What does BETWEEN do in a WHERE clause?
WHERE marks BETWEEN 80 AND 100 includes both 80 and 100 and everything in between. It works with numbers, dates, and text. It is equivalent to WHERE marks >= 80 AND marks <= 100.Q9: What is the correct clause order in a SQL SELECT statement?
SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY. WHERE must come after FROM and before GROUP BY. Placing WHERE in the wrong position causes a syntax error.Q10: Can WHERE filter on multiple columns at the same time?
WHERE class = 10 AND marks > 80 filters by both the class column and the marks column simultaneously.Frequently Asked Questions
What does the SQL WHERE clause do?
The WHERE clause filters rows based on a condition. Only rows where the condition is TRUE are returned by a SELECT, or changed by an UPDATE or DELETE. Rows where the condition is FALSE or NULL are skipped. In a SELECT, WHERE runs after FROM and before GROUP BY, HAVING and ORDER BY.
Can WHERE be used with UPDATE and DELETE?
Yes. In an UPDATE or DELETE statement, WHERE decides which rows are changed or removed. Without a WHERE clause the statement applies to every row in the table, so always double-check the condition — running it as a SELECT first is a safe habit.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before GROUP BY. HAVING filters groups after aggregation, so it can use functions like COUNT(*) or SUM(amount) and WHERE cannot. Use WHERE to exclude rows early, and HAVING only for conditions on aggregated results. See the SQL HAVING lesson for worked examples.
How do I check for NULL values in a WHERE clause?
Use IS NULL or IS NOT NULL. Never use = NULL — it returns zero rows because any comparison with NULL evaluates to NULL (unknown), not TRUE. Example: WHERE email IS NULL finds customers with no email address on file.
Can I use a column alias in a WHERE clause?
No. WHERE is evaluated before the SELECT list, so an alias defined with AS does not exist yet when WHERE runs. Repeat the original expression in WHERE, or wrap the query in a subquery and filter on the alias in the outer query.
Is the SQL WHERE clause case-sensitive?
SQL keywords such as WHERE are not case-sensitive. Whether text comparisons like WHERE city = 'delhi' are case-sensitive depends on the database and its collation: PostgreSQL compares text case-sensitively by default, while MySQL and SQL Server are case-insensitive under their default collations.
How do I filter by multiple values in a WHERE clause?
Use the IN operator. WHERE class IN (9, 10) is shorter and easier to read than WHERE class = 9 OR class = 10, especially as the list grows. The WHERE IN lesson covers NOT IN and subqueries.
How does the LIKE operator work in a WHERE clause?
LIKE matches text against a pattern: % matches any sequence of characters and _ matches exactly one character. For example, LIKE 'A%' finds values that start with A and LIKE '%son' finds values that end with son. The SQL LIKE operator lesson has more patterns.
Where can I practice SQL WHERE clause queries?
Write and run queries in the free SQL Playground, or work through the SQL challenges for structured problems that use WHERE filters on realistic datasets.