SQL FOUNDATIONS:Pattern Matching & Advanced Filters (LIKE, IN, BETWEEN)

Use LIKE, IN, and BETWEEN for pattern and range filtering.

When to Use LIKE, IN, BETWEEN

  • Use LIKE when searching text patterns
  • Use IN when matching multiple values
  • Use BETWEEN when filtering ranges

These are commonly used in SQL interviews and real-world queries.

Example Dataset

Table: employees

idnamedepartmentsalary
1JohnHR40000
2AliceIT60000
3BobIT55000
4EmmaFinance70000
5DavidHR45000
6AnkitIT52000

LIKE Operator (Pattern Matching)

LIKE is used to match text patterns.

Wildcards:

  • % means any number of characters
  • _ means exactly one character

Examples:

Names starting with 'A'

sql
SELECT * FROM employees
WHERE name LIKE 'A%';

Names ending with 'n'

sql
SELECT * FROM employees
WHERE name LIKE '%n';

Names containing 'a'

sql
SELECT * FROM employees
WHERE name LIKE '%a%';

Second letter is 'o'

sql
SELECT * FROM employees
WHERE name LIKE '_o%';

IN Operator (Multiple Values)

Used to match multiple values in a cleaner way.

sql
SELECT * FROM employees
WHERE department IN ('HR', 'IT');

Equivalent to:

sql
department = 'HR' OR department = 'IT'

BETWEEN Operator (Range Filtering)

Filters values within a range (inclusive).

sql
SELECT * FROM employees
WHERE salary BETWEEN 45000 AND 60000;

Equivalent to:

sql
salary >= 45000 AND salary <= 60000

SQL LIKE vs IN vs BETWEEN

FeatureLIKEINBETWEEN
Use casePattern matchingMultiple valuesRange filtering
Example'A%'('HR','IT')45000-60000

Combining Filters

sql
SELECT * FROM employees
WHERE department = 'IT'
AND salary BETWEEN 50000 AND 60000;

Common Questions (FAQ)

What is LIKE in SQL

LIKE is used to match patterns in text using wildcards such as % and _.

What does % mean in SQL

% represents any number of characters (including zero characters).

What is the difference between IN and OR

IN is a cleaner and more efficient way to match multiple values instead of writing multiple OR conditions.

Is BETWEEN inclusive

Yes, BETWEEN includes both the starting and ending values.

Internal Learning Links

  • Learn SELECT basics
  • Learn WHERE clause filtering
  • Learn SQL JOINs

Practice CTA

Practice more SQL filtering questions on Schoolabe to improve your interview preparation.

Pattern Matching & Advanced Filters (LIKE, IN, BETWEEN) Missions

Solve exercises in sequence to unlock the next mission.

1

Names starting with A

Retrieve employees whose name starts with 'A'.

Solve Mission
2

Names ending with n

Retrieve employees whose name ends with 'n'.

Locked
3

Names containing a

Retrieve employees whose name contains 'a'.

Locked
4

HR or Finance (IN)

Retrieve employees from HR or Finance using IN.

Locked
5

Salary between 45000 and 60000

Retrieve employees with salary between 45000 and 60000.

Locked
6

Name starts with D and salary > 40000

Retrieve employees whose name starts with 'D' and salary is greater than 40000.

Locked
7

Not in IT

Retrieve employees not in IT department.

Locked
8

Second letter is o

Retrieve employees whose name has 'o' as second character.

Locked
9

IT between 50000 and 65000

Retrieve employees from IT with salary between 50000 and 65000.

Locked
10

Starts with A or B and salary > 50000

Retrieve employees whose name starts with 'A' or 'B' and salary is above 50000.

Locked