Filtering and Pattern Matching
Learn advanced filtering techniques including pattern matching, ranges, and NULL handling.
IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
Syntax
Example
Instead of writing:
You can write:
Using NOT IN:
BETWEEN Operator
The BETWEEN operator selects values within a given range (inclusive).
Syntax
Examples
Numeric ranges:
Result:
+-----------+-----+
| name | age |
+-----------+-----+
| Alice | 20 |
| Bob | 22 |
| Charlie | 21 |
| Eva | 20 |
+-----------+-----+
Date ranges:
NOT BETWEEN:
LIKE Operator
The LIKE operator searches for a specified pattern in a column.
Wildcards
| Wildcard | Description | Example |
|---|---|---|
% | Represents zero or more characters | 'a%' finds values starting with "a" |
_ | Represents a single character | 'a_' finds two-character values starting with "a" |
Examples
Starts with 'A':
Result:
Ends with 'e':
Result:
Contains 'a' anywhere:
Second letter is 'i':
Result:
Exactly 3 characters:
Result:
Case-Insensitive Searching
Different databases handle case sensitivity differently:
MySQL (case-insensitive by default):
PostgreSQL (case-sensitive, use ILIKE):
SQL Server (use UPPER or LOWER):
NULL Values
NULL represents missing or unknown data. It's different from zero or empty string.
IS NULL
Find records with NULL values:
IS NOT NULL
Find records without NULL values:
NULL Comparison
You cannot use = or != with NULL. Always use IS NULL or IS NOT NULL.
Combining Multiple Conditions
You can combine different filtering techniques:
SELECT name, age, city FROM students
WHERE (grade IN ('A', 'B'))
AND (age BETWEEN 20 AND 22)
AND (city LIKE '%C%')
AND (email IS NOT NULL);
Practical Examples
Example 1: Find students whose name starts with 'C' or 'D' and are older than 20
Example 2: Find students not from NYC or LA with grades A or B
Example 3: Find students with names containing 'ar' and age between 18-25
Pattern Matching Best Practices
- Use specific patterns -
name LIKE 'A%'is faster thanname LIKE '%A%' - Avoid leading wildcards -
LIKE '%text'can be slow on large tables - Consider indexes - Columns frequently used in LIKE should be indexed
- Use exact matches when possible -
=is faster thanLIKE
Practice Exercises
Using a hypothetical employees table:
+----+-----------+-----------+--------+------------+
| id | name | department| salary | hire_date |
+----+-----------+-----------+--------+------------+
| 1 | John Doe | Sales | 50000 | 2020-01-15 |
| 2 | Jane Smith| IT | 75000 | 2019-06-20 |
| 3 | Bob Wilson| Sales | 55000 | 2021-03-10 |
| 4 | Alice Lee | HR | NULL | 2022-01-05 |
+----+-----------+-----------+--------+------------+
Try these queries:
- Find employees in Sales or IT departments
- Find employees hired between 2020 and 2022
- Find employees whose name contains 'son'
- Find employees with no salary information
- Find employees in Sales with salary above 52000
Solutions
-- 1. Employees in Sales or IT
SELECT * FROM employees
WHERE department IN ('Sales', 'IT');
-- 2. Hired between 2020 and 2022
SELECT * FROM employees
WHERE hire_date BETWEEN '2020-01-01' AND '2022-12-31';
-- 3. Name contains 'son'
SELECT * FROM employees
WHERE name LIKE '%son%';
-- 4. No salary information
SELECT * FROM employees
WHERE salary IS NULL;
-- 5. Sales employees with salary > 52000
SELECT * FROM employees
WHERE department = 'Sales' AND salary > 52000;
Previous: Basic SQL Queries | Next: Aggregate Functions