Paramclasses Academy - Blog

Tutorials Details

←
Post 15 of 40
→

SQL Operators Explained: AND, OR, IN, BETWEEN and LIKE

SQL Operators Explained: AND, OR, IN, BETWEEN and LIKE

November 17, 2026

SQL Operators Explained: AND, OR, IN, BETWEEN and LIKE

✨ Introduction

In SQL Server, SQL Operators play an important role in filtering, searching, and managing database records. They help developers define conditions and retrieve only the information that matches specific requirements.

When working with large databases, developers often need to apply multiple conditions to retrieve accurate results.

For example, imagine that you are developing a Student Management System for a college. You want to find students who belong to the Computer Science department and have scored more than 80 marks. You may also need to find students from multiple cities, search marks within a particular range, or find names that start with a specific letter.

SQL Server provides different operators to perform these operations efficiently.

In this tutorial, we will learn five important SQL operators:

  • AND Operator: Checks whether all specified conditions are true.

  • OR Operator: Checks whether at least one condition is true.

  • IN Operator: Matches values from a specified list.

  • BETWEEN Operator: Filters values within a specified range.

  • LIKE Operator: Searches for a specific character pattern.

Let's understand each operator with practical SQL Server examples.

1. What Are SQL Operators?

SQL Operators are special symbols or keywords used to perform operations on database values and conditions.

They help developers compare values, combine conditions, search for records, and filter information.

SQL Server supports several types of operators, including:

Operator Type Purpose Examples
Arithmetic Performs mathematical operations +, -, *, /
Comparison Compares two values =, >, <, >=
Logical Combines or reverses conditions AND, OR, NOT
Special Performs specific filtering operations IN, BETWEEN, LIKE

Important: In this tutorial, our main focus is on logical and special operators used with the WHERE clause.

2. Understanding the AND Operator in SQL Server

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

If even one condition is false, that record will not be returned.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 AND condition2;

Example

Suppose we have a Students table containing student details.

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

Explanation:

This query retrieves students who satisfy both conditions:

  1. The Department must be Computer Science.

  2. Marks must be greater than 80.

Only students satisfying both conditions will appear in the result.

Example with Three Conditions

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

Result: This query retrieves students who belong to Computer Science, have scored more than 80 marks, and live in Raipur.

Remember: AND means ALL conditions must be true.

3. Understanding the OR Operator in SQL Server

The OR operator is used when at least one of the specified conditions must be true.

Unlike AND, OR does not require every condition to be satisfied.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;

Example

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

Explanation:

This query retrieves students who belong to either the IT department or the Mechanical department.

If a student belongs to either department, the record will be included in the result.

Example with Multiple Conditions

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

This query retrieves students from Raipur, Bilaspur, or Durg.

Remember: OR means ANY ONE or more conditions can be true.

4. Difference Between AND and OR Operators

AND and OR are both logical operators, but they work differently.

AND Operator OR Operator
All conditions must be true. At least one condition must be true.
Produces more restrictive results. Can produce broader results.
Used when every requirement must match. Used when alternative requirements are acceptable.

Practical Example

Using AND:

SELECT *
FROM Students
WHERE Marks > 80
AND City = 'Raipur';

Only students with marks above 80 who live in Raipur are returned.

Using OR:

SELECT *
FROM Students
WHERE Marks > 80
OR City = 'Raipur';

Students are returned if they have marks above 80, live in Raipur, or satisfy both conditions.

5. Understanding the IN Operator in SQL Server

The IN operator is used to check whether a column value matches any value in a specified list.

It makes queries shorter and easier to understand when multiple OR conditions are required.

Syntax

SELECT column1, column2
FROM table_name
WHERE column_name IN (value1, value2, value3);

Example

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

Explanation:

This query retrieves students who belong to any of the three specified cities.

It is equivalent to writing:

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

Example with Department

SELECT *
FROM Students
WHERE Department IN ('IT', 'Computer Science');

This query retrieves students belonging to either IT or Computer Science.

Using NOT IN Operator

SQL Server also supports NOT IN, which excludes records matching the specified list.

SELECT *
FROM Students
WHERE Department NOT IN ('IT', 'Mechanical');

This query retrieves students whose departments are neither IT nor Mechanical.

Important: Be careful when using NOT IN with NULL values in a subquery. NULL can cause the comparison to evaluate as UNKNOWN, resulting in unexpected filtering behavior.

6. Understanding the BETWEEN Operator in SQL Server

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

It is commonly used with numerical values, dates, and other comparable data types.

Syntax

SELECT column1, column2
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example: Filter Students by Marks

SELECT *
FROM Students
WHERE Marks BETWEEN 70 AND 90;

Explanation:

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

Important: BETWEEN is inclusive, which means it includes both the starting and ending values.

Example: Filter Students by Age

SELECT *
FROM Students
WHERE Age BETWEEN 18 AND 22;

This query retrieves students whose age is between 18 and 22, including both values.

Example: Filter Employees by Salary

Suppose we have an Employees table.

SELECT *
FROM Employees
WHERE Salary BETWEEN 20000 AND 50000;

This query retrieves employees whose salary falls between ₹20,000 and ₹50,000.

Using NOT BETWEEN

The NOT BETWEEN operator retrieves records outside the specified range.

SELECT *
FROM Students
WHERE Marks NOT BETWEEN 70 AND 90;

This query retrieves students whose marks are below 70 or above 90.

Remember: BETWEEN includes the boundary values, whereas NOT BETWEEN excludes the entire specified range.

7. Understanding the LIKE Operator in SQL Server

The LIKE operator is used to search for a specific pattern in character-based data.

It is especially useful when the exact value is unknown.

For example, if you want to find students whose names start with R, you can use LIKE with a wildcard character.

Syntax

SELECT column1, column2
FROM table_name
WHERE column_name LIKE pattern;

SQL Server supports different wildcard characters for pattern matching.

Important LIKE Wildcards

Wildcard Meaning Example
% Zero or more characters R%
_ Exactly one character R_
[] Any one character from the specified set [ABC]%
[^] Any one character not in the specified set [^ABC]%

Let's understand these wildcards with practical examples.

Example 1: Names Starting with R

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

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

Examples: Rahul, Rohit, Rakesh.

Example 2: Names Ending with A

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

This query retrieves names ending with the letter A.

Examples: Priya, Neha, Sneha.

Example 3: Names Containing H

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

This query retrieves student names containing the letter H anywhere.

Example 4: Names Starting with R and Having Five Characters

SELECT *
FROM Students
WHERE StudentName LIKE 'R____';

This query searches for names starting with R followed by exactly four characters.

Example 5: Names Starting with A, B, or C

SELECT *
FROM Students
WHERE StudentName LIKE '[ABC]%';

This query retrieves names beginning with A, B, or C.

Example 6: Names Not Starting with A, B, or C

SELECT *
FROM Students
WHERE StudentName LIKE '[^ABC]%';

This query retrieves names that do not begin with A, B, or C.

Important: LIKE pattern matching can be affected by the collation of the column or database. In a case-insensitive collation, uppercase and lowercase letters are generally treated as equivalent.

8. Difference Between IN, BETWEEN and LIKE

These three operators are used for different types of filtering operations.

Operator Purpose Example
IN Matches specific values City IN ('Raipur', 'Durg')
BETWEEN Matches a range of values Marks BETWEEN 70 AND 90
LIKE Matches a character pattern Name LIKE 'R%'

Practical Understanding:

  • Use IN when you know the exact list of possible values.

  • Use BETWEEN when you want to filter records within a range.

  • Use LIKE when you want to search for a particular text pattern.

9. Combining Multiple SQL Operators

SQL Server allows developers to combine AND, OR, IN, BETWEEN, and LIKE in a single query.

This makes it possible to perform more advanced filtering operations.

Example 1: Students from Selected Cities with High Marks

SELECT *
FROM Students
WHERE City IN ('Raipur', 'Bilaspur')
AND Marks > 80;

This query retrieves students from Raipur or Bilaspur who have scored more than 80 marks.

Example 2: Students with Marks Between 70 and 90

SELECT *
FROM Students
WHERE Marks BETWEEN 70 AND 90
AND Department = 'Computer Science';

This query retrieves Computer Science students whose marks fall between 70 and 90.

Example 3: Students Whose Names Start with R or P

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

This query retrieves students whose names start with R or P.

Example 4: Combine AND, OR and LIKE

SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND
(
    City = 'Raipur'
    OR StudentName LIKE 'A%'
);

Explanation:

This query retrieves Computer Science students who either live in Raipur or have names starting with A.

Important: Parentheses help control the logical grouping of conditions and make complex queries easier to understand.

10. Understanding Operator Precedence in SQL Server

When multiple logical operators are used in a query, SQL Server evaluates them according to operator precedence.

For the operators discussed here, the important order is:

  1. Parentheses are evaluated first.

  2. NOT is evaluated before AND.

  3. AND is evaluated before OR.

Example

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

In this query, AND is evaluated before OR.

Therefore, SQL Server interprets the condition as:

SELECT *
FROM Students
WHERE Department = 'IT'
OR
(
    Department = 'Computer Science'
    AND Marks > 80
);

Best Practice: Use parentheses whenever a query contains multiple AND and OR conditions. This improves readability and prevents logical mistakes.

11. Real-World Example: Student Management System

Let's understand these operators through a practical example at Param Classes.

Suppose Param Classes is developing a Student Management System to manage student names, marks, departments, and cities.

The application needs to perform the following operations.

Requirement 1: Find Computer Science Students with More Than 80 Marks

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

Purpose: Finds students satisfying both conditions.

Requirement 2: Find Students from Raipur or Bilaspur

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

Purpose: Retrieves students from either city.

Requirement 3: Find Students from Selected Departments

SELECT *
FROM Students
WHERE Department IN
(
    'IT',
    'Computer Science',
    'Mechanical'
);

Purpose: Retrieves students belonging to any of the specified departments.

Requirement 4: Find Students with Marks Between 60 and 90

SELECT *
FROM Students
WHERE Marks BETWEEN 60 AND 90;

Purpose: Retrieves students whose marks fall within the specified range.

Requirement 5: Find Students Whose Names Start with S

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

Purpose: Retrieves students whose names begin with S.

These examples demonstrate how SQL operators are used in real-world database applications to retrieve accurate and meaningful information.

12. Common Mistakes While Using SQL Operators

Beginners sometimes make mistakes when combining SQL operators.

Here are some common mistakes and their solutions.

1. Confusing AND and OR

Incorrect Understanding: AND and OR perform the same operation.

Correct Understanding:

  • AND requires all conditions to be true.

  • OR requires at least one condition to be true.

2. Using BETWEEN Without Understanding Its Range

SELECT *
FROM Students
WHERE Marks BETWEEN 70 AND 90;

Remember: Both 70 and 90 are included in the result.

3. Forgetting Wildcards with LIKE

Incorrect:

SELECT *
FROM Students
WHERE StudentName LIKE 'R';

Correct:

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

The second query searches for names starting with R.

4. Using IN Incorrectly

Incorrect:

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

Correct:

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

5. Ignoring Operator Precedence

When using AND and OR together, always check the logical order of evaluation.

Use parentheses to make the intended conditions clear.

13. Advantages of SQL Operators

SQL operators provide several benefits when working with relational databases.

  • Flexible Filtering: Retrieve records using multiple conditions.

  • Accurate Searching: Find specific values and character patterns.

  • Easy Range Selection: Filter numerical and date values using BETWEEN.

  • Simplified Queries: Use IN instead of writing multiple OR conditions.

  • Advanced Conditions: Combine multiple operators to create complex database queries.

  • Practical Applications: Useful in student management, employee management, inventory systems, billing software, and e-commerce applications.

14. Frequently Asked Questions (FAQs)

Q1. What are SQL operators?

SQL operators are symbols and keywords used to compare values, combine conditions, and perform operations on database records.

Q2. What is the difference between AND and OR?

AND requires all conditions to be true, whereas OR requires at least one condition to be true.

Q3. What is the purpose of the IN operator?

IN checks whether a column value matches any value in a specified list.

Q4. What is the BETWEEN operator in SQL Server?

BETWEEN retrieves records whose values fall within a specified range, including both boundary values.

Q5. What is the LIKE operator used for?

LIKE is used to search for specific text patterns using wildcard characters such as % and _.

Q6. What is the difference between IN and BETWEEN?

IN matches values from a specified list, whereas BETWEEN checks whether a value falls within a specified range.

Q7. Can we use AND and OR together in SQL Server?

Yes. Both operators can be combined in a single query. Parentheses can be used to control the evaluation order.

Q8. What does the percentage (%) wildcard represent in SQL Server?

The % wildcard represents zero or more characters in a LIKE pattern.

Q9. What does the underscore (_) wildcard represent?

The underscore represents exactly one character in a LIKE pattern.

Q10. Can we use NOT with SQL operators?

Yes. NOT can be used to reverse conditions, such as NOT IN, NOT BETWEEN, and NOT LIKE.

Conclusion

SQL operators are essential for filtering and retrieving specific records from SQL Server databases. By using AND, OR, IN, BETWEEN, and LIKE, developers can create flexible queries and handle different database searching requirements.

In this tutorial, we learned how logical operators combine conditions, how IN matches multiple values, how BETWEEN filters a specified range, and how LIKE searches for character patterns. We also explored operator precedence and practical examples using a Student Management System.

At Param Classes, we focus on helping students and beginners understand SQL Server through practical examples, step-by-step tutorials, and real-world database projects. Practise these SQL operators using SQL Server Management Studio (SSMS) to improve your database querying skills and prepare for advanced SQL Server concepts.

←
Post 15 of 40
→