IN/BETWEEN/LIKE | Dataplexa

IN, BETWEEN, and LIKE

So far, we’ve filtered data using simple comparisons. SQL also provides powerful keywords that make filtering cleaner, shorter, and more expressive.

In this lesson, we’ll learn:

  • IN – match against a list of values
  • BETWEEN – filter within a range
  • LIKE – search using patterns

Example Table

We’ll continue using the employees table:

+----+----------+------------+--------+
| id | name     | department | salary |
+----+----------+------------+--------+
| 1  | Alice    | IT         | 75000  |
| 2  | Bob      | IT         | 72000  |
| 3  | Charlie  | HR         | 60000  |
| 4  | Diana    | Finance    | 70000  |
| 5  | Evan     | HR         | 58000  |
+----+----------+------------+--------+
  

The IN Operator

The IN operator allows you to check if a value matches any value from a list.

Instead of writing multiple OR conditions, IN makes queries cleaner.

Syntax:

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

Example: Employees in IT or HR.

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

IN vs OR

Approach Example Readability
OR department = 'IT' OR department = 'HR' Low
IN department IN ('IT','HR') High

The BETWEEN Operator

The BETWEEN operator filters values within a range, including the boundaries.

Syntax:

SELECT *
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
  

Example: Employees with salary between 60,000 and 75,000.

SELECT name, salary
FROM employees
WHERE salary BETWEEN 60000 AND 75000;
  

Important note:

  • BETWEEN includes both lower and upper values

BETWEEN with Dates

BETWEEN is commonly used with dates.

WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31'
  

This returns records within the given date range.


The LIKE Operator

The LIKE operator is used to search for patterns in text.

It works with wildcard characters:

  • % – matches any number of characters
  • _ – matches exactly one character

LIKE Examples

Find employees whose name starts with “A”.

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

Find employees whose name ends with “a”.

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

Find employees whose name contains “ar”.

SELECT name
FROM employees
WHERE name LIKE '%ar%';
  

LIKE with Single-Character Wildcard

The underscore (_) matches exactly one character.

Example: Names with exactly 5 letters, starting with “A”.

SELECT name
FROM employees
WHERE name LIKE 'A____';
  

NOT with IN, BETWEEN, LIKE

You can combine NOT with these operators to exclude values.

SELECT name
FROM employees
WHERE department NOT IN ('HR');
  

Common Beginner Mistakes

  • Forgetting quotes around text values
  • Misunderstanding BETWEEN boundaries
  • Using LIKE instead of = for exact matches
  • Forgetting % wildcard in LIKE

Why These Operators Matter

IN, BETWEEN, and LIKE allow you to:

  • Write cleaner queries
  • Handle ranges and categories easily
  • Search text flexibly
  • Build real-world filters

What’s Next?

In the next lesson, we will apply everything learned so far in a Beginner-Level SQL Mini Project, using real questions and step-by-step solutions.