SQL WHERE Clause — Filtering Data
Filter with WHERE
The class teacher wants students who scored at least 85, including students exactly on that boundary.
WHERE keeps rows whose condition evaluates to true. False and unknown conditions do not qualify a row.
Explain and apply
Combine conditions to select the intended students and predict which records qualify.
Work it out
List students with marks at least 85 and predict whether 85 itself qualifies.
Avoid mistakes
Use >= when the boundary is included and > when it is excluded.
Filter with WHERE: the request and its result
School need
The class teacher wants students who scored at least 85, including students exactly on that boundary.
Database operation
WHERE keeps rows whose condition evaluates to true. False and unknown conditions do not qualify a row.
Check the outcome
Both 85-valued rows satisfy >=. Replacing it with > would exclude both, so the boundary changes the result.
Understand what the database does
A comparison such as marks >= 85 is evaluated for each candidate row. AND combines required conditions; OR accepts either alternative. Parentheses make combined conditions explicit. A NULL mark makes an ordinary numeric comparison unknown, so it is not included by that comparison.
Related concepts: Combine SQL Conditions and Missing Values and NULL and Filter Groups with HAVING.
SQL WHERE Clause
Imagine walking into a school library and asking for *every* book. Not practical, right? Instead, you specify — “Books by R.K. Narayan” or “Books for Class 10.” SQL works the same way. The WHERE clause lets you filter records and get only what matters.
Syntax of WHERE Clause
SQL · portable concepts; preserved examples identify their engine
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Sample Table – Students
SQL · portable concepts; preserved examples identify their engine
We’ll use this familiar table for examples:
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
1. Simple WHERE Clause
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to retrieve a list of all students enrolled in class '9B' to prepare a class roster.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and class columns from the students table where the class is exactly '9B'.
SELECT name, class FROM students
WHERE class = '9B';
Result: Result contains the names and class details of students who are enrolled in class '9B'.
| name | class |
|---|---|
| Diya Iyer | 9B |
| Mehul Agarwal | 9B |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
2. WHERE with Numeric Condition
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to retrieve a list of all students who are 15 years old to assign them to an age-specific program.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and age columns from the students table where the age is equal to 15.
SELECT name, age FROM students
WHERE age = 15;
Result description: Result contains the names and ages of students who are exactly 15 years old.
| name | age |
|---|---|
| Aarav Sharma | 15 |
| Rohit Menon | 15 |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
3. Using AND with WHERE
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to get the names of students who are enrolled in class '9B' and reside in the city of Delhi for location-specific communication or reporting.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name column from the students table where the city is 'Delhi' and the class is '9B'.
SELECT name FROM students
WHERE city = 'Delhi' AND class = '9B';
Result description: Result contains the names of students who belong to class '9B' and live in Delhi.
| name |
|---|
| Mehul Agarwal |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
4. Using OR with WHERE
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to generate a list of students who are from the cities of Delhi or Pune for city-wise reporting or regional event planning.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and city columns from the students table where the city is either 'Delhi' or 'Pune'.
SELECT name, city FROM students
WHERE city = 'Delhi' OR city = 'Pune';
Result description: Result contains the names and city information of students who reside in either Delhi or Pune.
| name | city |
|---|---|
| Aarav Sharma | Delhi |
| Mehul Agarwal | Delhi |
| Sneha Patil | Pune |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
5. Using NOT
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to get a list of students who are not from the city of Delhi to organize regional events for out-of-town students.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and city columns from the students table where the city is not equal to 'Delhi'.
SELECT name, city FROM students
WHERE NOT city = 'Delhi';
Result description: Result contains the names and city information of all students who do not belong to Delhi.
| name | city |
|---|---|
| Diya Iyer | Chennai |
| Rohit Menon | Kochi |
| Sneha Patil | Pune |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
6. Using BETWEEN
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to retrieve a list of students who are aged between 14 and 15 to plan age-specific academic programs.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and age columns from the students table where the age is between 14 and 15, inclusive.
SELECT name, age FROM students
WHERE age BETWEEN 14 AND 15;
Result description: Result contains the names and ages of students who are either 14 or 15 years old.
| name | age |
|---|---|
| Aarav Sharma | 15 |
| Diya Iyer | 14 |
| Rohit Menon | 15 |
| Mehul Agarwal | 14 |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
7. Using IN
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to identify students who are from the cities of Delhi or Chennai in order to organize city-specific events or communications.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and city columns from the students table where the city is either 'Delhi' or 'Chennai'.
SELECT name, city FROM students
WHERE city IN ('Delhi', 'Chennai');
Result description: Result contains the names and city information of students who belong to Delhi or Chennai.
| name | city |
|---|---|
| Aarav Sharma | Delhi |
| Diya Iyer | Chennai |
| Mehul Agarwal | Delhi |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
8. Using LIKE for Pattern Matching
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
Use case: A school administrator wants to find all students whose names start with the letter 'S' for organizing name-based reports or communications.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name column from the students table where the name begins with the letter 'S', using the LIKE operator with the pattern 'S%'.
SELECT name FROM students
WHERE name LIKE 'S%';
Result description: Result contains the names of all students whose names start with the letter 'S'.
| name |
|---|
| Sneha Patil |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
9. WHERE with Comparison Operators
Input definitions and data for this example. The shown statements establish the base fixture. Where this section changes an existing object, its required object definition is included below; use the stated before/after table to follow a sequence of changes.
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Aarav Sharma', '10A', 15, 'Delhi'),
(2, 'Diya Iyer', '9B', 14, 'Chennai'),
(3, 'Rohit Menon', '10A', 15, 'Kochi'),
(4, 'Sneha Patil', '8C', 13, 'Pune'),
(5, 'Mehul Agarwal', '9B', 14, 'Delhi');
SQL · portable concepts; preserved examples identify their engine
=(equal)!=or<>(not equal)<(less than)>(greater than)<=,>=
Example: Students younger than 14
Use case: A school administrator wants to retrieve a list of all students who are younger than 14 years old to identify students eligible for a junior program.
| roll_no | name | class | age | city |
|---|---|---|---|---|
| 1 | Aarav Sharma | 10A | 15 | Delhi |
| 2 | Diya Iyer | 9B | 14 | Chennai |
| 3 | Rohit Menon | 10A | 15 | Kochi |
| 4 | Sneha Patil | 8C | 13 | Pune |
| 5 | Mehul Agarwal | 9B | 14 | Delhi |
SQL query description: SQL query to select the name and age columns from the students table where the age is less than 14.
SELECT name, age FROM students
WHERE age < 14;
Result description: Result contains the names and ages of students whose age is less than 14.
| name | age |
|---|---|
| Sneha Patil | 13 |
The surrounding query explains the specific highlighted values. Highlights annotate this example; they do not change the stored table.
Best Practices
Query discussed in this section:
SELECT name, age FROM students
WHERE age < 14;
- Use quotes for string values, but not for numbers.
- Use parentheses to group conditions when mixing AND/OR.
- Preview with SELECT before applying updates or deletes.
Important differences and mistakes
Use >= when the boundary is included and > when it is excluded. Use IS NULL for missing values. A WHERE condition on the right side of a LEFT JOIN can remove the unmatched rows that the join originally preserved.
Try it with the school data
List students with marks at least 85 and predict whether 85 itself qualifies.
This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
SQL · create only these practice tables in a disposable database:
CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');
Worked answer and reasoning
This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
SQL · create only these practice tables in a disposable database:
CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');
SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect
SELECT name, marks FROM practice_students WHERE marks >= 85 ORDER BY roll_no;| name | marks |
|---|---|
| Aarav | 85 |
| Sneha | 85 |
Both 85-valued rows satisfy >=. Replacing it with > would exclude both, so the boundary changes the result.
Check your understanding
Use the idea independently
WHERE keeps rows whose condition evaluates to true. False and unknown conditions do not qualify a row.
Both 85-valued rows satisfy >=. Replacing it with > would exclude both, so the boundary changes the result.
Continue your understanding with Combine SQL Conditions and Missing Values and NULL and Filter Groups with HAVING.
Source for the engine-specific behavior: MySQL 8.4 reference documentation.
Reaching the recap and completing the quizzes are separate milestones.