Home › SQL Tutorial › SQL WHERE Clause

SQL WHERE Clause

⏱ 9 min read Updated: 04 Oct 2026

The WHERE clause in SQL is used to filter records from a table based on a specified condition. It allows us to retrieve only the rows that match the condition instead of returning all records. For example, if a Students table contains hundreds of records, we can use the WHERE clause to find students from a particular city, students with a specific age, or students who scored more than 80 marks.

In this article, we will learn how to use the SQL WHERE clause with comparison operators, logical operators, pattern matching, ranges, NULL values, dates, joins, subqueries, and other advanced SQL concepts.

What Is the WHERE Clause in SQL?

The WHERE clause is used with SQL statements to specify which records should be selected, updated, or deleted. It checks each row against a condition. If the condition is true, that row is included in the result.

Syntax

The basic syntax is:

SELECT column1, column2

FROM table_name

WHERE condition;

Here:

  • SELECT specifies the columns we want to retrieve.
  • FROM specifies the table from which the data is retrieved.
  • WHERE specifies the condition used to filter the records.
  • condition defines which rows should be returned.

Example

Suppose we have the following Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

To find students who live in Delhi, we can use:

SELECT *

FROM Students

WHERE city = 'Delhi';

Output

idnameagecity
1Rahul20Delhi
3Amit21Delhi

Comparison Operator

Comparison operators are used with the WHERE clause to compare a column value with a specific value. They help filter records based on conditions such as equal to, not equal to, greater than, or less than.

OperatorMeaning
=Equal to
<>Not equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Using Equal To (=)

The = operator checks whether two values are equal.

Consider the following table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age = 21;

Output

idnameagecity
3Amit21Delhi

The query returns the student whose age is exactly 21.

Using Not Equal To (<>)

The <> operator returns rows where the value is different from the specified value.

Using the same Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE city <> 'Delhi';

Output

idnameagecity
2Priya22Noida
4Neha23Mumbai

The query excludes students who live in Delhi.

The != operator can also be used for not equal in many SQL database systems:

SELECT *

FROM Students

WHERE city != 'Delhi';

It produces the same result in systems that support !=.

Using Greater Than (>)

The > operator is used to find values greater than a specified value.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age > 21;

Output

idnameagecity
2Priya22Noida
4Neha23Mumbai

The query returns students whose age is greater than 21.

Using Less Than (<)

The < operator finds values smaller than a specified value.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age < 22;

Output

idnameagecity
1Rahul20Delhi
3Amit21Delhi

Using Greater Than or Equal To (>=)

The >= operator returns values that are greater than or equal to the specified value.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age >= 21;

Output

idnameagecity
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

The query returns students who are 21 or older.

Using Less Than or Equal To (<=)

The <= operator returns values that are less than or equal to the specified value.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age <= 21;

Output

idnameagecity
1Rahul20Delhi
3Amit21Delhi

The query returns students whose age is 21 or younger.

WHERE Clause with AND Operator

The AND operator is used when all specified conditions must be true. Suppose we want to find students who are older than 20 and live in Delhi.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age > 20 AND city = 'Delhi';

Output

idnameagecity
3Amit21Delhi

Both conditions must be true. Rahul lives in Delhi, but his age is not greater than 20, so he is not included.

WHERE Clause with OR Operator

The OR operator returns a row when at least one of the conditions is true.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE city = 'Delhi' OR city = 'Noida';

Output

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi

The query returns students who live in either Delhi or Noida. WHERE Clause with NOT Operator The NOT operator reverses a condition.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE NOT city = 'Delhi';

Output

idnameagecity
2Priya22Noida
4Neha23Mumbai

The same condition can also be written as:

SELECT *

FROM Students

WHERE city <> 'Delhi';

Both queries exclude students from Delhi.

WHERE Clause with IN Operator

The IN operator is useful when we want to check a column against multiple possible values.

Instead of writing:

SELECT *

FROM Students

WHERE city = 'Delhi'

   OR city = 'Noida'

   OR city = 'Mumbai';

we can use:

SELECT *

FROM Students

WHERE city IN ('Delhi', 'Noida', 'Mumbai');

This makes the query shorter and easier to read.

Example

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE city IN ('Delhi', 'Noida');

Output

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi

The query returns students from Delhi or Noida.

WHERE Clause with NOT IN Operator

The NOT IN operator is used to exclude multiple values.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE city NOT IN ('Delhi', 'Noida');

Output

idnameagecity
4Neha23Mumbai

The query excludes Delhi and Noida and returns the remaining records.

WHERE Clause with BETWEEN Operator

The BETWEEN operator is used to filter values within a specified range.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai

Query:

SELECT *

FROM Students

WHERE age BETWEEN 20 AND 22;

Output

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi

WHERE Clause with NOT BETWEEN

NOT BETWEEN is used when we want to exclude a range.

Employees table:

employee_idnamedepartmentsalary
101RajIT28000
102PriyaHR35000
103AmitIT45000
104NehaSales55000

Query:

SELECT *

FROM Employees

WHERE salary NOT BETWEEN 30000 AND 50000;

Output

employee_idnamedepartmentsalary
101RajIT28000
104NehaSales55000

The query returns employees whose salary is outside the specified range.

WHERE Clause with LIKE Operator

The LIKE operator is used for pattern matching. It is useful when we want to find text values that follow a particular pattern.

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai
5Anil20Lucknow

Query:

SELECT *

FROM Students

WHERE name LIKE 'A%';

Output

idnameagecity
3Amit21Delhi
5Anil20Lucknow

Here, % represents zero or more characters.

Common SQL LIKE Wildcards

SQL LIKE wildcards are special characters used with the LIKE operator to search for a specific pattern in text values. They allow us to find records when we know only part of the value. 

WildcardMeaningExample
%It matches zero, one, or multiple charactersLIKE 'A%'
_It matches exactly one characterLIKE 'A_'

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai
5Anil20Lucknow

 

Query:

SELECT *

FROM Students

WHERE name LIKE 'A%';

Output

idnameagecity
3Amit21Delhi
5Anil20Lucknow

This finds names that start with A.

Name Ending with a Specific Letter

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai
5Anil20Lucknow

Query:

SELECT *

FROM Students

WHERE name LIKE '%a';

Output

idnameagecity
2Priya22Noida
4Neha23Mumbai

This finds names ending with a. Name Containing Specific Text

Students table:

idnameagecity
1Rahul20Delhi
2Priya22Noida
3Amit21Delhi
4Neha23Mumbai
5Anil20Lucknow

Query:

SELECT *

FROM Students

WHERE name LIKE '%it%';

Output

idnameagecity
3Amit21Delhi

This finds names containing it.

Matching One Character with _

The _ wildcard represents exactly one character.

Students table:

idnameagecity
1Amit21Delhi
2Anit22Noida
3Anil20Lucknow
4Rahul23Mumbai

Query:

SELECT *

FROM Students

WHERE name LIKE 'A_it';

Output

idnameagecity
1Amit21Delhi
2Anit22Noida

The pattern A_it means the name starts with A, has exactly one character in the middle, and ends with it.

WHERE Clause with NULL Values

NULL represents a missing or unknown value in SQL. Suppose we have an Employees table:

employee_idnamedepartmentsalary
101RajIT40000
102PriyaHRNULL
103AmitIT50000
104NehaSalesNULL

We should not use = to check whether a value is NULL. We use the following commands

SELECT *

FROM Employees

WHERE salary IS NULL;

Output

employee_idnamedepartmentsalary
102PriyaHRNULL
104NehaSalesNULL

The query returns rows where salary does not contain a value.

Checking for NOT NULL

To find rows where a column contains a value, use IS NOT NULL.

Employees table:

employee_idnamedepartmentsalary
101RajIT40000
102PriyaHRNULL
103AmitIT50000
104NehaSalesNULL

Query:

SELECT *

FROM Employees

WHERE salary IS NOT NULL;

Output

employee_idnamedepartmentsalary
101RajIT40000
103AmitIT50000

WHERE Clause with Dates

The WHERE clause can also be used to filter records based on dates.

Suppose we have an Orders table:

order_idcustomerorder_dateamount
1Rahul2026-01-152500
2Priya2026-03-104000
3Amit2026-06-203500
4Neha2026-10-015000

To find orders placed on October 1, 2026:

SELECT *

FROM Orders

WHERE order_date = '2026-10-01';

Output

order_idcustomerorder_dateamount
4Neha2026-10-015000

Filtering Dates with Greater Than

SELECT *

FROM Orders

WHERE order_date > '2026-01-01';

Output

order_idcustomerorder_dateamount
2Priya2026-03-104000
3Amit2026-06-203500
4Neha2026-10-015000

This returns orders placed after January 1, 2026.

Filtering a Date Range

SELECT *

FROM Orders

WHERE order_date BETWEEN '2026-01-01' AND '2026-03-31';

Output

order_idcustomerorder_dateamount
1Rahul2026-01-152500
2Priya2026-03-104000

When working with date and time columns, the exact behavior can depend on the database system and data type, so date-range queries should be written carefully.

WHERE Clause with Multiple Conditions

We can combine multiple conditions in a single WHERE clause. Suppose we have:

employee_idnamedepartmentsalarycity
101RajIT45000Delhi
102PriyaHR55000Noida
103AmitIT65000Delhi
104NehaSales70000Mumbai

Query:

SELECT *

FROM Employees

WHERE department = 'IT'

  AND salary > 50000

  AND city = 'Delhi';

Output

employee_idnamedepartmentsalarycity
103AmitIT65000Delhi

The query returns employees who work in the IT department, earn more than 50000, and live in Delhi.

Conclusion

The SQL WHERE clause is one of the most important parts of SQL because it allows us to filter data based on specific conditions. It can be used with SELECT, UPDATE, and DELETE statements and works with comparison operators, logical operators, IN, BETWEEN, LIKE, NULL checks, subqueries, joins, and other SQL features.