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
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'
sqlSELECT * FROM employeesWHERE name LIKE 'A%';
Names ending with 'n'
sqlSELECT * FROM employeesWHERE name LIKE '%n';
Names containing 'a'
sqlSELECT * FROM employeesWHERE name LIKE '%a%';
Second letter is 'o'
sqlSELECT * FROM employeesWHERE name LIKE '_o%';
IN Operator (Multiple Values)
Used to match multiple values in a cleaner way.
sqlSELECT * FROM employeesWHERE department IN ('HR', 'IT');
Equivalent to:
sqldepartment = 'HR' OR department = 'IT'
BETWEEN Operator (Range Filtering)
Filters values within a range (inclusive).
sqlSELECT * FROM employeesWHERE salary BETWEEN 45000 AND 60000;
Equivalent to:
sqlsalary >= 45000 AND salary <= 60000
SQL LIKE vs IN vs BETWEEN
Combining Filters
sqlSELECT * FROM employeesWHERE 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.
Names ending with n
Retrieve employees whose name ends with 'n'.
Names containing a
Retrieve employees whose name contains 'a'.
HR or Finance (IN)
Retrieve employees from HR or Finance using IN.
Salary between 45000 and 60000
Retrieve employees with salary between 45000 and 60000.
Name starts with D and salary > 40000
Retrieve employees whose name starts with 'D' and salary is greater than 40000.
Not in IT
Retrieve employees not in IT department.
Second letter is o
Retrieve employees whose name has 'o' as second character.
IT between 50000 and 65000
Retrieve employees from IT with salary between 50000 and 65000.
Starts with A or B and salary > 50000
Retrieve employees whose name starts with 'A' or 'B' and salary is above 50000.