Solution - Like and Wildcards in SQL
Master SQL wildcards and LIKE operators safely
Think of each wildcard as a little bomb that could explode in your face. They can include way more than you intended, creating false positives in your results.
Essential SQL Tools for Database Navigation
Entity Relationship (ER) Diagrams
Get a bird's-eye view of all tables and their relationships. DBeaver creates these with just a double-click, while other tools require manual creation.
Database Schema Explorer
View all tables and columns without opening multiple windows. Essential for understanding data structure in unfamiliar databases.
Query Result Verification
Always perform the sniff test on wildcard results. Check for false positives, especially as your dataset grows larger.
Using Wildcards in SQL LIKE Statements
Use as few wildcards as possible. Don't look for excuses to add them - only use them when you genuinely need to handle unknown quantities in your data.
Gmail Search: Safe vs Risky Wildcard Patterns
| Feature | Approach | Risk Level | Example | Issues |
|---|---|---|---|---|
| Exact Match | '%@gmail.com' | Low | user@gmail.com | None - most secure |
| Partial Match | '%gmail%' | High | huntingmail.com | False positives |
| No Domain | '%gmail.com%' | Medium | somethingingmail.com | Potential overmatching |
Safe Wildcard Query Development Process
Start with the positive query
Write your LIKE statement to find exactly what you want first, without any NOT operators
Verify results manually
Check that your wildcards aren't catching unintended matches - perform the sniff test
Add NOT if needed
Only after confirming your positive query works correctly, add NOT to exclude those results
Test on larger datasets
Patterns that work on small databases may create problems when scaled up to real-world data sizes
IN vs OR: Multiple Value Filtering
| Feature | Method | Code Example | Performance | Readability |
|---|---|---|---|---|
| IN Operator | IN ('FL', 'TX') | Equal | High - cleaner syntax | |
| OR Operator | = 'FL' OR = 'TX' | Equal | Medium - more verbose |
The IN operator only works with equality comparisons. You cannot use wildcards with IN - use individual LIKE statements with OR instead.
SQL Query Structure Requirements
Filter your data before sorting to ensure correct results
Sort the complete result set before limiting to top N records
Restrict results only after all filtering and sorting is complete
SQL allows running selected portions of code for step-by-step verification
Wildcard Character Usage Patterns
Percentage Sign (%)
Represents zero or more characters. Use '%plate%' to find 'plate' anywhere in the text, or '%plate' to find text ending with 'plate'.
Underscore (_)
Represents exactly one character. Perfect for 'gr_y' to match both 'gray' and 'grey' with precision and clarity.
NOT with LIKE
Exclude patterns from results. Can be placed before LIKE or before the column name with identical functionality.
Handling Color Variations: Gray vs Grey
| Feature | Method | Pattern | Clarity | Risk Level |
|---|---|---|---|---|
| IN operator | IN ('gray', 'grey') | Explicit and clear | None | |
| Underscore wildcard | LIKE 'gr_y' | Somewhat clear | Very low | |
| Percentage wildcard | LIKE 'gr%y' | Unclear intent | High - false positives |
This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways