Paramclasses Academy - Blog

Tutorials Details

←
Post 13 of 40
→

SQL WHERE Clause Tutorial: Filter Records Using Conditions

SQL WHERE Clause Tutorial: Filter Records Using Conditions

November 17, 2026

SQL WHERE Clause Tutorial: Filter Records Using Conditions

Introduction

In SQL Server, databases often contain thousands or even millions of records. However, when working with real-world applications, developers do not always need to retrieve every record from a table. Sometimes, they need only specific records based on particular conditions.

The SQL WHERE clause is used to filter records from a database table. It allows developers to retrieve only those rows that satisfy the specified conditions.

For example, imagine that you are developing a Student Management System for a college. The database contains information about hundreds of students, including their names, roll numbers, courses, fees, and marks. If you want to display only students who belong to the Computer Science department, you can use the WHERE clause.

In this tutorial, we will learn how to use the SQL WHERE clause, understand different filtering conditions, work with comparison operators, and retrieve specific records using practical SQL Server examples.

1. What Is the SQL WHERE Clause?

The WHERE clause is used in SQL to filter records based on one or more specified conditions.

It returns only those rows that satisfy the given condition. Records that do not match the condition are excluded from the result.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example

Suppose we have a table named Students:

SELECT *
FROM Students
WHERE Department = 'Computer Science';

This query retrieves only those students whose department is Computer Science.

Important: The WHERE clause is commonly used with SELECT, UPDATE, and DELETE statements to identify specific records.

2. Create a Sample Students Table

Before understanding different WHERE conditions, let's create a sample Students table.

CREATE TABLE Students
(
    StudentID INT PRIMARY KEY,
    StudentName VARCHAR(100),
    Age INT,
    Department VARCHAR(50),
    Marks DECIMAL(5,2),
    City VARCHAR(50)
);

Now insert some sample records:

INSERT INTO Students
(StudentID, StudentName, Age, Department, Marks, City)
VALUES
(1, 'Rahul', 20, 'Computer Science', 85.50, 'Raipur'),
(2, 'Priya', 21, 'IT', 92.00, 'Bilaspur'),
(3, 'Amit', 19, 'Computer Science', 78.50, 'Durg'),
(4, 'Sneha', 22, 'Mechanical', 88.00, 'Raipur'),
(5, 'Rohit', 20, 'IT', 65.00, 'Bhilai'),
(6, 'Neha', 21, 'Computer Science', 95.00, 'Raipur');

To display all records:

SELECT * FROM Students;

Now we can apply different filtering conditions using the WHERE clause.

3. Filter Records Using the Equal To (=) Operator

The equal to operator (=) checks whether a column value matches a specified value.

Example

SELECT *
FROM Students
WHERE Department = 'Computer Science';

Explanation:

  • SELECT * retrieves all columns.

  • FROM Students specifies the table.

  • WHERE Department = 'Computer Science' filters the records.

  • Only students belonging to the Computer Science department are displayed.

This operator is useful when searching for records with an exact matching value.

4. Filter Records Using the Greater Than (>) Operator

The greater than operator (>) retrieves records where a column value is greater than a specified value.

Example

SELECT *
FROM Students
WHERE Marks > 80;

Explanation:

This query retrieves students who have scored more than 80 marks.

Students with marks equal to or below 80 will not appear in the result.

5. Filter Records Using the Less Than (<) Operator

The less than operator (<) retrieves records where a column value is smaller than the specified value.

Example

SELECT *
FROM Students
WHERE Age < 21;

Explanation:

This query displays students whose age is below 21 years.

It is useful when filtering numerical values such as age, salary, marks, and product prices.

6. Filter Records Using Greater Than or Equal To (>=)

The greater than or equal to operator (>=) retrieves records where the value is greater than or equal to a specified value.

Example

SELECT *
FROM Students
WHERE Marks >= 85;

This query displays students who scored 85 marks or more.

7. Filter Records Using Less Than or Equal To (<=)

The less than or equal to operator (<=) retrieves records where the value is smaller than or equal to a specified value.

Example

SELECT *
FROM Students
WHERE Marks <= 80;

This query retrieves students whose marks are 80 or below.

8. Filter Records Using Not Equal To (!= or <>)

SQL Server supports the not equal to operators (!= and <>).

These operators retrieve records where a column value does not match the specified value.

Example

SELECT *
FROM Students
WHERE Department <> 'IT';

This query displays all students who do not belong to the IT department.

You can also write:

SELECT *
FROM Students
WHERE Department != 'IT';

Both forms represent a not-equal comparison in SQL Server.

9. Use Multiple Conditions with the AND Operator

The AND operator is used when multiple conditions must be true for a record to be included in the result.

Syntax

SELECT *
FROM table_name
WHERE condition1 AND condition2;

Example

SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND Marks > 80;

Explanation:

This query retrieves students who satisfy both conditions:

  • Department must be Computer Science.

  • Marks must be greater than 80.

If either condition is false, the record will not be returned.

10. Use Multiple Conditions with the OR Operator

The OR operator retrieves records when at least one of the specified conditions is true.

Example

SELECT *
FROM Students
WHERE Department = 'IT'
OR Department = 'Mechanical';

This query displays students belonging to either the IT or Mechanical department.

Unlike AND, the OR operator does not require both conditions to be true.

11. Use the NOT Operator with WHERE

The NOT operator reverses a condition and returns records that do not satisfy it.

Example

SELECT *
FROM Students
WHERE NOT Department = 'Computer Science';

This query retrieves students who do not belong to the Computer Science department.

You can also use the following equivalent condition:

SELECT *
FROM Students
WHERE Department <> 'Computer Science';

12. Filter Records Using the BETWEEN Operator

The BETWEEN operator is used to retrieve records within a specified range.

It includes both the starting and ending values.

Syntax

SELECT *
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example

SELECT *
FROM Students
WHERE Marks BETWEEN 70 AND 90;

This query retrieves students whose marks are between 70 and 90, including both 70 and 90.

Practical Use: BETWEEN is useful for filtering salary ranges, dates, prices, ages, and marks.

13. Filter Records Using the IN Operator

The IN operator allows developers to specify multiple possible values for a column.

It is a convenient alternative to writing multiple OR conditions.

Example

SELECT *
FROM Students
WHERE City IN ('Raipur', 'Bilaspur', 'Durg');

This query retrieves students who belong to Raipur, Bilaspur, or Durg.

The same condition can also be written using OR:

SELECT *
FROM Students
WHERE City = 'Raipur'
OR City = 'Bilaspur'
OR City = 'Durg';

The IN operator makes queries shorter and easier to understand.

14. Filter Records Using the LIKE Operator

The LIKE operator is used to search for a specific pattern in a character string.

It is commonly used when the exact value is not known.

SQL Server supports two commonly used wildcard characters:

  • % – Represents zero or more characters.

  • _ – Represents exactly one character.

Example 1: Names Starting with R

SELECT *
FROM Students
WHERE StudentName LIKE 'R%';

This query retrieves students whose names start with the letter R.

Example 2: Names Ending with a

SELECT *
FROM Students
WHERE StudentName LIKE '%a';

This query retrieves students whose names end with the letter a.

Example 3: Names Containing the Letter h

SELECT *
FROM Students
WHERE StudentName LIKE '%h%';

This query retrieves students whose names contain the letter h anywhere.

Example 4: Names with a Specific Character Position

SELECT *
FROM Students
WHERE StudentName LIKE 'R____';

This query retrieves five-character names starting with R.

15. Use WHERE with UPDATE Statement

The WHERE clause can also be used with UPDATE to modify specific records.

Example

UPDATE Students
SET Marks = 90
WHERE StudentID = 1;

This query updates the marks of the student whose StudentID is 1.

Important: Always use the appropriate WHERE condition with UPDATE. Without WHERE, all records in the table may be updated.

16. Use WHERE with DELETE Statement

The WHERE clause is also used with DELETE to remove specific records from a table.

Example

DELETE FROM Students
WHERE StudentID = 5;

This query deletes only the student record whose StudentID is 5.

Important: If you execute DELETE without a WHERE clause, all rows in the table will be deleted. Always verify your condition before executing a DELETE statement.

17. Real-World Example: Student Management System

Let's understand the WHERE clause through a practical example at Param Classes.

Suppose Param Classes is developing a Student Management System to manage student information.

The application stores student names, departments, marks, ages, and cities in the Students table.

Now imagine the following requirements:

Requirement 1: Find students from Raipur

SELECT *
FROM Students
WHERE City = 'Raipur';

Requirement 2: Find students who scored more than 85 marks

SELECT *
FROM Students
WHERE Marks > 85;

Requirement 3: Find Computer Science students who scored more than 80 marks

SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND Marks > 80;

Requirement 4: Find students from Raipur or Bilaspur

SELECT *
FROM Students
WHERE City IN ('Raipur', 'Bilaspur');

Requirement 5: Find students whose names start with P

SELECT *
FROM Students
WHERE StudentName LIKE 'P%';

These examples demonstrate how the WHERE clause helps developers retrieve specific information from a database.

18. Common Mistakes While Using the WHERE Clause

Beginners sometimes make mistakes while writing SQL filtering conditions.

Here are some common mistakes and their solutions:

1. Forgetting Single Quotes

Incorrect:

SELECT *
FROM Students
WHERE City = Raipur;

Correct:

SELECT *
FROM Students
WHERE City = 'Raipur';

2. Using AND Instead of OR

Use AND when all conditions must be satisfied. Use OR when at least one condition must be satisfied.

3. Forgetting the WHERE Clause in UPDATE

Without WHERE, all rows may be updated.

4. Forgetting the WHERE Clause in DELETE

Without WHERE, all rows may be deleted.

5. Using the Wrong Comparison Operator

Always select the appropriate operator based on the requirement, such as =, >, <, >=, <=, and <>.

19. Advantages of the SQL WHERE Clause

The WHERE clause provides several benefits when working with relational databases.

  • Data Filtering: Retrieves only the records that match specified conditions.

  • Improved Readability: Makes queries more focused and meaningful.

  • Flexible Searching: Supports multiple conditions and comparison operators.

  • Efficient Data Management: Helps identify specific records for updates and deletions.

  • Practical Applications: Useful in student management, employee management, inventory systems, billing software, and many other applications.

20. Frequently Asked Questions (FAQs)

Q1. What is the WHERE clause in SQL Server?

The WHERE clause is used to filter records based on specified conditions.

Q2. Can we use multiple conditions in a WHERE clause?

Yes. Multiple conditions can be combined using AND, OR, and other logical operators.

Q3. What is the difference between AND and OR?

AND returns records when all specified conditions are true, whereas OR returns records when at least one condition is true.

Q4. Can we use WHERE with UPDATE?

Yes. WHERE identifies the specific records that need to be updated.

Q5. Can we use WHERE with DELETE?

Yes. WHERE specifies which records should be deleted.

Q6. What is the difference between IN and BETWEEN?

IN checks whether a value matches one of several specified values, whereas BETWEEN checks whether a value falls within a specified range.

Q7. What is the purpose of the LIKE operator?

LIKE is used to search for character patterns using wildcards such as % and _.

Q8. What happens if we do not use WHERE with SELECT?

Without WHERE, SELECT retrieves all rows from the specified table.

Conclusion

The SQL WHERE clause is one of the most important parts of SQL Server because it allows developers to retrieve, update, and delete specific records based on conditions. By using comparison operators, logical operators, BETWEEN, IN, and LIKE, developers can perform flexible and meaningful data filtering operations.

In this tutorial, we learned how to create a sample Students table, apply different WHERE conditions, combine multiple conditions, search records using patterns, and use WHERE with UPDATE and DELETE statements.

At Param Classes, we focus on helping students and beginners understand SQL Server through practical examples and real-world database projects. Practise these WHERE clause queries using SQL Server Management Studio (SSMS) to strengthen your database querying skills and prepare for advanced SQL topics.

←
Post 13 of 40
→