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.

  1. Explain and apply

    Combine conditions to select the intended students and predict which records qualify.

  2. Work it out

    List students with marks at least 85 and predict whether 85 itself qualifies.

  3. Avoid mistakes

    Use >= when the boundary is included and > when it is excluded.

Filter with WHERE: the request and its result

  1. School need

    The class teacher wants students who scored at least 85, including students exactly on that boundary.

  2. Database operation

    WHERE keeps rows whose condition evaluates to true. False and unknown conditions do not qualify a row.

  3. Check the outcome

    Both 85-valued rows satisfy >=. Replacing it with > would exclude both, so the boundary changes the result.

A conceptual model. The query examples below show the actual syntax, input data and results.

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');
students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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'.

Query Result
name class
Diya Iyer9B
Mehul Agarwal9B
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name age
Aarav Sharma15
Rohit Menon15
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name
Mehul Agarwal
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name city
Aarav SharmaDelhi
Mehul AgarwalDelhi
Sneha PatilPune
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name city
Diya IyerChennai
Rohit MenonKochi
Sneha PatilPune
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name age
Aarav Sharma15
Diya Iyer14
Rohit Menon15
Mehul Agarwal14
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name city
Aarav SharmaDelhi
Diya IyerChennai
Mehul AgarwalDelhi
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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'.

Query Result
name
Sneha Patil
FocusSelected or comparedAdded or changedRemoved or failing

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.

students
roll_nonameclassagecity
1Aarav Sharma10A15Delhi
2Diya Iyer9B14Chennai
3Rohit Menon10A15Kochi
4Sneha Patil8C13Pune
5Mehul Agarwal9B14Delhi

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.

Query Result
name age
Sneha Patil13
FocusSelected or comparedAdded or changedRemoved or failing

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.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

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.

Practice input: practice_students
roll_nonameclassmarkscity
1Aarav10A85Pune
2Diya9B70Chennai
3Sneha10A85Pune

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;
Expected query result
namemarks
Aarav85
Sneha85

Both 85-valued rows satisfy >=. Replacing it with > would exclude both, so the boundary changes the result.

Check your understanding

Which students qualify for WHERE marks > 85 when both named students have 85?
Choose one answer. You can try again.

Answer and explanation

Neither of the students with exactly 85.

Both 85-valued rows satisfy >=. Replacing it with > would exclude both, so the boundary changes the result.

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.

    Next lesson: SQL ORDER BY Clause — Sorting Results in SQL Queries →