đ SQL Wildcards are special characters used with the LIKE operator to search for data that matches a specific pattern. Wildcards make SQL queries more flexible by allowing you to search for partial text, unknown characters, prefixes, suffixes, and other text patterns instead of requiring exact matches.
đ What are SQL Wildcards?
Wildcards represent one or more unknown characters in a search pattern. They are commonly used in SELECT statements with the WHERE clause and the LIKE operator to perform pattern matching on string data.
Information
đ Basic Syntax
General Syntax
SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';đ Common SQL Wildcards
The SQL standard defines two primary wildcard characters used with the LIKE operator.
| Wildcard | Description | Example Pattern |
|---|---|---|
| % | Matches zero or more characters. | 'A%' |
| _ | Matches exactly one character. | 'J_n' |
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | City |
|---|---|---|---|
| 101 | Alice | Computer Science | Chennai |
| 102 | Bob | Mathematics | Coimbatore |
| 103 | Charlie | Physics | Madurai |
| 104 | David | Computer Science | Chennai |
| 105 | Eva | Mathematics | Salem |
đ¤ The % Wildcard
The % wildcard represents zero or more characters. It is the most commonly used wildcard in SQL.
1ī¸âŖ Starts With
Names Starting with A
SELECT *
FROM Students
WHERE Name LIKE 'A%';This query returns names such as Alice.
2ī¸âŖ Ends With
Names Ending with e
SELECT *
FROM Students
WHERE Name LIKE '%e';3ī¸âŖ Contains Text
Names Containing ar
SELECT *
FROM Students
WHERE Name LIKE '%ar%';4ī¸âŖ Multiple Words
Department Contains Science
SELECT *
FROM Students
WHERE Department LIKE '%Science%';đ The _ Wildcard
The _ wildcard represents exactly one character.
Example 1
Single Character Match
SELECT *
FROM Students
WHERE Name LIKE '_ob';This matches names such as Bob.
Example 2
Exactly Five Characters
SELECT *
FROM Students
WHERE Name LIKE '_____';This query returns names that contain exactly five characters.
Example 3
Pattern with Multiple Underscores
SELECT *
FROM Students
WHERE Name LIKE '__a__';This matches five-character names where the third character is a.
đ Combining Wildcards
Wildcards can be combined to create more advanced search patterns.
Combined Wildcards
SELECT *
FROM Students
WHERE Name LIKE 'A_%';This query returns names that begin with A and contain at least one additional character.
đĢ Using NOT LIKE with Wildcards
Use NOT LIKE to exclude rows that match a specific pattern.
Exclude Names Starting with A
SELECT *
FROM Students
WHERE Name NOT LIKE 'A%';đ Wildcards with AND
Wildcard with AND
SELECT *
FROM Students
WHERE Name LIKE 'A%'
AND City = 'Chennai';đ Wildcards with OR
Wildcard with OR
SELECT *
FROM Students
WHERE Name LIKE 'A%'
OR Name LIKE 'D%';đ Common Wildcard Patterns
| Pattern | Meaning |
|---|---|
| 'A%' | Starts with A. |
| '%e' | Ends with e. |
| '%ar%' | Contains "ar". |
| 'J_n' | Three-letter word beginning with J and ending with n. |
| '_____' | Exactly five characters. |
| 'A_%' | Starts with A and has at least one more character. |
â ī¸ Database-Specific Wildcards
While % and _ are defined by the SQL standard and are widely supported, some database systems provide additional pattern-matching features outside the standard.
| Database | Standard Wildcards | Additional Features |
|---|---|---|
| MySQL | %, _ | Supports regular expressions with separate operators/functions. |
| PostgreSQL | %, _ | Supports ILIKE for case-insensitive matching and regular expressions. |
| SQL Server | %, _ | Also supports bracket expressions such as [A-C] in LIKE patterns. |
| Oracle | %, _ | Supports regular expression functions in addition to LIKE. |
Important
đŧ Real-World Example
A university wants to find all students whose names begin with D or whose department contains the word Science.
Real-World Query
SELECT StudentID,
Name,
Department
FROM Students
WHERE Name LIKE 'D%'
OR Department LIKE '%Science%'
ORDER BY Name;This query performs flexible searches without requiring exact text matches.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ Wildcards are used with the LIKE operator.
- đ % matches zero or more characters.
- đ _ matches exactly one character.
- đ Wildcards can be combined to create advanced search patterns.
- đ NOT LIKE excludes matching patterns.
- đ Some wildcard features are specific to certain database systems.