Wildcards in SQL are special characters used with the LIKE operator to perform pattern matching and flexible searches.
In simple words:
Wildcards help search partial text instead of exact values.
Why Wildcards are Important
Enterprise applications frequently require:
- Partial searches
- Search suggestions
- Flexible filtering
- Pattern matching
- User-friendly search functionality
Wildcards help:
- Search incomplete data
- Implement dynamic filtering
- Improve search experience
Simple Real-Life Example
Think about:
- Searching employee names starting with "N"
Without Wildcards
WHERE employee_name = 'Naresh'
Matches only:
Naresh
With Wildcards
WHERE employee_name LIKE 'N%'
Matches:
Naresh Nikhil Naveen
Wildcards Internal Architecture
Read Column Value
|
v
Apply Wildcard Pattern
|
v
Pattern Match?
/ \
Yes No
| |
Return Row Ignore Row
Main Wildcards in SQL
| Wildcard | Meaning |
|---|---|
| % | Represents zero or more characters |
| _ | Represents exactly one character |
1. % Wildcard
The % wildcard represents:
- Zero or more characters
Employees Table Example
| Employee Name |
|---|
| Naresh |
| Nikhil |
| Rahul |
| Naveen |
Example: Starts With N
SELECT employee_name FROM employees WHERE employee_name LIKE 'N%';
Result
Naresh Nikhil Naveen
Meaning
Find names:
- Starting with N
Example: Ends With h
SELECT employee_name FROM employees WHERE employee_name LIKE '%h';
Result
Naresh Rahul
Meaning
Find names:
- Ending with h
Example: Contains ar
SELECT employee_name FROM employees WHERE employee_name LIKE '%ar%';
Result
Naresh
Meaning
Find names:
- Containing "ar"
2. _ Wildcard
The _ wildcard represents:
- Exactly one character
Example
SELECT employee_name FROM employees WHERE employee_name LIKE '_a%';
Meaning
Find names:
- Having "a" as second character
Example Results
Naresh Rahul
Difference Between % and _
| Wildcard | Characters Matched |
|---|---|
| % | Zero or more characters |
| _ | Exactly one character |
Example Comparison
Using %
LIKE 'N%'
Matches:
N Na Naresh Nikhil
Using _
LIKE 'N_'
Matches:
No Ne
But NOT:
Naresh
Combining Wildcards
Wildcards can be combined together.
Example
SELECT employee_name FROM employees WHERE employee_name LIKE 'N__%';
Meaning
Find names:
- Starting with N
- Having at least 3 characters
Wildcards Query Flow
Read String
|
v
Apply Wildcard Rules
|
v
Pattern Matches?
/ \
Yes No
| |
Return Row Ignore Row
Wildcards with Numbers
Wildcards can also work with:
- Numeric values converted to strings
Example
SELECT * FROM products WHERE product_code LIKE '10%';
Purpose
Find product codes:
- Starting with 10
Wildcards with Dates
Wildcards can filter:
- Date patterns
Example
SELECT * FROM orders WHERE order_date LIKE '2026-05%';
Purpose
Find orders:
- Placed in May 2026
NOT LIKE with Wildcards
NOT LIKE excludes:
- Matching patterns
Example
SELECT employee_name FROM employees WHERE employee_name NOT LIKE 'N%';
Purpose
Retrieve employees:
- Whose names do not start with N
Wildcards and ESCAPE Character
Sometimes:
- Actual % or _ symbols need searching
Example
SELECT * FROM products WHERE product_name LIKE '%\%%' ESCAPE '\';
Purpose
Find product names:
- Containing % symbol
Wildcards vs Regular Expressions
| Feature | Wildcards | Regular Expressions |
|---|---|---|
| Complexity | Simple | Advanced |
| Performance | Faster for simple searches | More flexible |
| Usage | Basic pattern matching | Complex text validation |
Performance Consideration
Efficient Pattern
LIKE 'N%'
- Can use indexes efficiently
Less Efficient Pattern
LIKE '%ar%'
- May require full table scan
Why?
Leading wildcard:
- Prevents efficient index usage
Optimization Techniques
- Use indexed columns
- Avoid leading wildcards when possible
- Use full-text search for large systems
Real-Time Banking Example
Banking systems use wildcards for:
- Customer searches
- Transaction reference filtering
- Branch code matching
Example
WHERE customer_name LIKE 'Raj%'
Real-Time E-Commerce Example
E-commerce platforms use wildcards for:
- Product searches
- Brand filtering
- Search suggestions
Example
WHERE product_name LIKE '%Laptop%'
Real-Time Learning Platform Example
Learning platforms use wildcards for:
- Course searches
- Technology filtering
- Student lookup systems
Example
WHERE course_title LIKE '%Spring Boot%'
Wildcards in Microservices
Microservices use wildcards for:
- Search APIs
- Autocomplete systems
- Filtering services
Advantages of Wildcards
- Flexible pattern matching
- Simple syntax
- Useful for partial searches
Disadvantages of Wildcards
- Can reduce query performance
- Leading wildcards may trigger full table scans
Best Practices
- Use indexed columns
- Avoid unnecessary leading wildcards
- Use full-text search for large-scale systems
- Optimize frequently searched fields
Common Interview Mistake
Many developers think:
- % and _ behave the same way
Reality
% matches:
- Zero or more characters
while _ matches:
- Exactly one character
Related Learning Topics
- What is LIKE Operator in SQL?
- Difference Between BETWEEN and IN
- What is WHERE Clause in SQL?
- What is an Index in SQL?
- MySQL Performance Optimization
Professional Interview Answer
Wildcards in SQL are special characters used with the LIKE operator for pattern matching and partial text searches. The two main wildcards are % and _. The % wildcard represents zero or more characters, while the _ wildcard represents exactly one character. Wildcards are widely used in search systems, filtering operations, autocomplete functionality, analytics platforms, and dynamic query generation. Enterprise applications such as banking systems, e-commerce platforms, learning management systems, and microservices-based APIs use wildcards extensively for flexible user-friendly searches and filtering operations.
Why Interviewers Like This Answer
- Clearly explains wildcard functionality
- Includes pattern matching concepts
- Shows performance awareness
- Provides enterprise-level examples
- Explains % vs _ differences clearly
Frequently Asked Questions
What are wildcards in SQL?
Wildcards are special characters used for pattern matching in SQL.
What does % mean in SQL?
% represents zero or more characters.
What does _ mean in SQL?
_ represents exactly one character.
Which operator uses wildcards?
Wildcards are mainly used with the LIKE operator.
Why can wildcard searches become slow?
Leading wildcards may prevent efficient index usage and trigger full table scans.