🔍 Pattern Matching & Filtering
SQL provides powerful filtering options in the WHERE
clause through LIKE, IN, and BETWEEN. These allow you to search patterns, match sets, and filter ranges efficiently.
1️⃣ SQL LIKE (Pattern Matching)
The LIKE
operator is used for matching patterns in text fields.
SELECT FirstName
FROM Employees
WHERE FirstName LIKE 'A%';
👉 Finds employees whose names start with “A”.
SELECT FirstName
FROM Employees
WHERE FirstName LIKE '%son';
👉 Finds employees whose names end with “son” (e.g., “Johnson”).
LIKE Wildcards
Wildcard | Description | Example |
---|---|---|
% | Zero or more characters | 'A%' → “Adam”, “Alex” |
_ | Single character | 'A_' → “Al”, “An” |
[] | Any one character from a set | '[J,K]%' → “John”, “Kate” |
2️⃣ SQL IN (Matching Multiple Values)
SELECT *
FROM Employees
WHERE Department IN ('IT', 'Finance', 'HR');
👉 Returns employees working in IT, Finance, or HR.
3️⃣ SQL BETWEEN (Range Filtering)
SELECT FirstName, Salary
FROM Employees
WHERE Salary BETWEEN 40000 AND 70000;
👉 Returns employees whose salaries are between 40,000 and 70,000 (inclusive).
4️⃣ Combining LIKE, IN, BETWEEN
SELECT FirstName, Department, Salary
FROM Employees
WHERE Department IN ('IT', 'Finance')
AND FirstName LIKE 'A%'
AND Salary BETWEEN 50000 AND 90000;
Pro Tip: Use
IN
for cleaner queries instead of multiple OR conditions, and use BETWEEN
for numeric/date ranges.✅ Key Takeaways
LIKE
→ Pattern matching with wildcards.IN
→ Match against multiple values.BETWEEN
→ Filter values within a range.- Can be combined for powerful filtering.
📚 Continue Learning
💬 Got a question about SQL LIKE, IN, or BETWEEN? Drop it in the comments below!