Oracle LIKE & Wildcards πΒΆ
Prerequisites: WHERE Clause
Mentor's Note: What if you only remember that a student's name starts with "Vi"? You can't use
=, but you can useLIKE. It's the search engine of the database world. π‘
π The Scenario: The Smudged Record πΒΆ
Imagine an old file where the end of the name is unreadable. You only see "Sma...".
- Action: You search for LIKE 'Sma%'.
- Result: You find "Smart", "Small", and "Smathers".
π» 1. The Wildcard ToolsΒΆ
| Wildcard | Meaning | Analogy |
|---|---|---|
| % | Zero or more characters | The "Infinite Bridge" π |
| _ | Exactly one character | The "Single Footstep" π£ |
ExamplesΒΆ
-- Starts with 'A'
SELECT name FROM students WHERE name LIKE 'A%';
-- Has 'sh' anywhere
SELECT name FROM students WHERE name LIKE '%sh%';
-- 4 letter name starting with 'R'
SELECT name FROM students WHERE name LIKE 'R___';
ποΈ Architect's Note: The Index Trap π‘οΈΒΆ
In Oracle, if you start your pattern with a wildcard (e.g., LIKE '%son'), the database cannot use an index. It must read the whole table.
- Architect's Advice: Always try to put text before the wildcard if performance matters! LIKE 'Son%' is much faster than LIKE '%Son%'.