Wildcards are symbols used in SQL to match patterns in text values.
They’re mainly used with the LIKE and NOT LIKE operators in WHERE clauses.
🎯 Syntax Example
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
grade VARCHAR(2)
);
INSERT INTO students (name, age, grade) VALUES
('Anya', 17, 'A'),
('Raj', 19, 'B'),
('Sara', 18, 'A'),
('Ravi', 20, 'C'),
('Tina', 16, 'B');SELECT * FROM students WHERE name LIKE 'S%';This finds names that start with ‘S’
% – Matches zero or more characters
SELECT * FROM students WHERE name LIKE 'Sa%';✔️ Matches: Swastika, Sweetie, Swapna
🚫 NOT LIKE – Opposite of LIKE
SELECT * FROM students WHERE name NOT LIKE 'A%';✔️ Returns all names not starting with A
🧠 Combine Wildcards
SELECT * FROM students WHERE name LIKE '_a%';✔️ Matches names where 2nd letter is a. Examples: Sara, Ravi, Maya
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LIKE Wildcards — Try It Yourself</title>
<style>
.infographic-likerunner {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--good: #16a34a;
--good-soft: #dcfce7;
--on-accent: #ffffff;
--display-font: Georgia, 'Iowan Old Style', serif;
--body-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
--mono-font: 'SF Mono', Consolas, monospace;
background: var(--paper);
color: var(--ink);
font-family: var(--body-font);
max-width: 780px;
margin: 0 auto;
box-sizing: border-box;
overflow-x: hidden;
}
.infographic-likerunner[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--good: #4ade80;
--good-soft: #123420;
--on-accent: #101418;
}
.infographic-likerunner * { box-sizing: border-box; }
.lk-topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; gap: 0.5rem; flex-wrap: wrap; }
.lk-progress { font-size: 0.78rem; color: var(--ink-soft); }
.lk-showfull-btn {
font: inherit;
font-size: 0.78rem;
background: none;
border: 1px solid var(--line);
border-radius: 999px;
padding: 0.35rem 0.8rem;
cursor: pointer;
color: var(--ink-soft);
}
.lk-showfull-btn:hover { border-color: var(--accent); color: var(--accent); }
.lk-showfull-btn.is-on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.lk-fullcode-panel { display: none; margin-bottom: 1.25rem; }
.lk-fullcode-panel.is-visible { display: block; }
.lk-fullcode-scroll { overflow-x: auto; }
.lk-fullcode {
background: var(--ink);
color: #d4e2ff;
border-radius: 8px;
padding: 1rem 1.2rem;
font-family: var(--mono-font);
font-size: 0.75rem;
line-height: 1.7;
white-space: pre;
opacity: 0.85;
display: inline-block;
min-width: 100%;
}
.infographic-likerunner[data-theme="dark"] .lk-fullcode { background: #05070a; }
.lk-frame {
background: var(--paper-alt);
border: 1px solid var(--line);
border-radius: 10px;
padding: 1.25rem;
}
.lk-kw { color: #9cc5ff; }
.lk-str { color: #f8d980; }
.lk-num { color: #4ade80; }
.lk-cm { color: #7c8aa8; }
.lk-wild { color: #f472b6; font-weight: 700; }
.lk-step { display: none; margin-bottom: 1rem; }
.lk-step.is-revealed { display: block; animation: lk-step-in 0.4s ease; }
@keyframes lk-step-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.lk-step.is-done .lk-step-code-full { display: none; }
.lk-step.is-done .lk-step-done-pill { display: flex; }
.lk-step-done-pill {
display: none;
align-items: center;
gap: 0.5rem;
background: var(--good-soft);
color: var(--good);
border-radius: 7px;
padding: 0.55rem 0.85rem;
font-family: var(--mono-font);
font-size: 0.78rem;
cursor: pointer;
}
.lk-step-done-pill .chevron { margin-left: auto; font-size: 0.7rem; opacity: 0.7; }
.lk-step.is-expanded-done .lk-step-code-full { display: block; }
.lk-step.is-expanded-done .lk-step-done-pill { background: var(--accent-soft); color: var(--accent); }
.lk-step-label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); margin: 0 0 0.5rem; }
.lk-step-note { font-size: 0.82rem; color: var(--ink-soft); margin: 0.5rem 0 0; line-height: 1.45; }
.lk-code-scroll { overflow-x: auto; }
.lk-code {
background: var(--ink);
color: #d4e2ff;
border-radius: 8px;
padding: 0.85rem 1.05rem;
font-family: var(--mono-font);
font-size: 0.82rem;
line-height: 1.6;
white-space: pre;
display: inline-block;
min-width: 100%;
}
.infographic-likerunner[data-theme="dark"] .lk-code { background: #05070a; }
.lk-run-row { display: flex; justify-content: center; margin-top: 0.85rem; }
.lk-run-btn {
font: inherit;
font-size: 0.85rem;
background: var(--accent);
color: var(--on-accent);
border: none;
border-radius: 7px;
padding: 0.55rem 1.1rem;
cursor: pointer;
}
.lk-run-btn:hover { opacity: 0.9; }
.lk-run-btn:disabled { opacity: 0.55; cursor: wait; }
.lk-run-btn.is-secondary { background: var(--paper); color: var(--ink); border: 1px solid var(--line); }
.lk-output-label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); margin: 1.1rem 0 0.6rem; }
.lk-table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; min-width: 0; }
table.lk-table {
width: 100%;
min-width: 320px;
border-collapse: collapse;
background: var(--paper);
border-radius: 8px;
font-size: 0.88rem;
}
table.lk-table th, table.lk-table td {
border: 1px solid var(--line);
padding: 0.5rem 0.7rem;
text-align: left;
white-space: nowrap;
transition: background 0.3s, color 0.3s, opacity 0.3s;
}
table.lk-table th { background: var(--accent); color: var(--on-accent); font-weight: 700; font-size: 0.75rem; }
table.lk-table tr.is-match td { background: var(--good-soft); color: var(--good); font-weight: 700; }
table.lk-table tr.is-nomatch td { opacity: 0.4; }
.lk-status {
text-align: center;
font-size: 0.82rem;
margin-top: 0.85rem;
padding: 0.5rem 0.8rem;
border-radius: 7px;
display: none;
background: var(--good-soft);
color: var(--good);
}
.lk-status.is-visible { display: block; animation: lk-step-in 0.3s ease; }
.lk-reset-row { display: flex; justify-content: center; margin-top: 1.25rem; }
@media (prefers-reduced-motion: reduce) {
.infographic-likerunner *, .infographic-likerunner *::before, .infographic-likerunner *::after {
transition-duration: 0.001ms !important;
animation-duration: 0.001ms !important;
}
}
</style>
</head>
<body>
<div class="infographic-likerunner" id="likerunner">
<div class="lk-topbar">
<span class="lk-progress" id="lkProgress"></span>
<button class="lk-showfull-btn" id="lkShowFullBtn" type="button">👁️ Show full SQL script</button>
</div>
<div class="lk-fullcode-panel" id="lkFullCodePanel">
<div class="lk-fullcode-scroll">
<div class="lk-fullcode" id="lkFullCodeContent"></div>
</div>
</div>
<div class="lk-frame">
<div id="lkSteps"></div>
<p class="lk-status" id="lkStatus"></p>
<p class="lk-output-label">Table: students</p>
<div class="lk-table-scroll">
<table class="lk-table" id="lkTable">
<thead><tr><th>id</th><th>name</th><th>age</th><th>grade</th></tr></thead>
<tbody id="lkTbody"></tbody>
</table>
</div>
<div class="lk-reset-row">
<button class="lk-run-btn is-secondary" id="lkReset" type="button">↺ Reset</button>
</div>
</div>
</div>
<script>
(function () {
const wrapper = document.getElementById('likerunner');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
})();
(function () {
const students = [
{ id: 1, name: 'Anya', age: 17, grade: 'A' },
{ id: 2, name: 'Raj', age: 19, grade: 'B' },
{ id: 3, name: 'Sara', age: 18, grade: 'A' },
{ id: 4, name: 'Ravi', age: 20, grade: 'C' },
{ id: 5, name: 'Tina', age: 16, grade: 'B' }
];
function kw(s) { return '<span class="lk-kw">' + s + '</span>'; }
function str(s) { return '<span class="lk-str">' + s + '</span>'; }
function num(s) { return '<span class="lk-num">' + s + '</span>'; }
function cm(s) { return '<span class="lk-cm">' + s + '</span>'; }
// build a highlighted LIKE string like 'S<wild>%</wild>'
function likeStr(pattern) {
const marked = pattern.replace(/[%_]/g, m => '\u0001' + m + '\u0002');
const escaped = marked.replace(/&/g, '&').replace(/</g, '<');
const withSpans = escaped
.replace(/\u0001/g, '<span class="lk-wild">')
.replace(/\u0002/g, '</span>');
return "<span class=\"lk-str\">'" + withSpans + "'</span>";
}
function likeToRegex(pattern) {
let out = '^';
for (const ch of pattern) {
if (ch === '%') out += '.*';
else if (ch === '_') out += '.';
else out += ch.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
out += '$';
return new RegExp(out, 'i');
}
const setupCode =
cm('-- Create the table and add sample data') + '\n' +
kw('CREATE TABLE') + ' students (\n' +
' id ' + kw('INT AUTO_INCREMENT PRIMARY KEY') + ',\n' +
' name ' + kw('VARCHAR') + '(50) ' + kw('NOT NULL') + ',\n' +
' age ' + kw('INT') + ' ' + kw('NOT NULL') + ',\n' +
' grade ' + kw('VARCHAR') + '(2)\n' +
');\n\n' +
kw('INSERT INTO') + ' students (name, age, grade) ' + kw('VALUES') + '\n' +
'(' + str("'Anya'") + ', ' + num('17') + ', ' + str("'A'") + '),\n' +
'(' + str("'Raj'") + ', ' + num('19') + ', ' + str("'B'") + '),\n' +
'(' + str("'Sara'") + ', ' + num('18') + ', ' + str("'A'") + '),\n' +
'(' + str("'Ravi'") + ', ' + num('20') + ', ' + str("'C'") + '),\n' +
'(' + str("'Tina'") + ', ' + num('16') + ', ' + str("'B'") + ');';
const steps = [
{
key: 'setup', title: 'Setup', btnLabel: '▶ Run Setup', doneLabel: '✓ Setup ran',
code: setupCode, note: '', pattern: null, negate: false
},
{
key: 'startsWith', title: "LIKE 'S%'", btnLabel: "▶ Run LIKE 'S%'", doneLabel: "✓ LIKE 'S%' ran",
code: cm('-- % matches zero or more characters') + '\n' + kw('SELECT') + ' * ' + kw('FROM') + ' students ' + kw('WHERE') + ' name ' + kw('LIKE') + ' ' + likeStr('S%') + ';',
note: "% matches any number of characters — this finds every name that starts with \"S\".",
pattern: 'S%', negate: false
},
{
key: 'startsWithSa', title: "LIKE 'Sa%'", btnLabel: "▶ Run LIKE 'Sa%'", doneLabel: "✓ LIKE 'Sa%' ran",
code: cm('-- A longer prefix narrows the match further') + '\n' + kw('SELECT') + ' * ' + kw('FROM') + ' students ' + kw('WHERE') + ' name ' + kw('LIKE') + ' ' + likeStr('Sa%') + ';',
note: 'Narrowing the prefix from "S" to "Sa" narrows the match too.',
pattern: 'Sa%', negate: false
},
{
key: 'notLike', title: "NOT LIKE 'A%'", btnLabel: "▶ Run NOT LIKE 'A%'", doneLabel: "✓ NOT LIKE ran",
code: cm('-- NOT LIKE is the opposite of LIKE') + '\n' + kw('SELECT') + ' * ' + kw('FROM') + ' students ' + kw('WHERE') + ' name ' + kw('NOT LIKE') + ' ' + likeStr('A%') + ';',
note: 'Everyone whose name does NOT start with "A" — the exact opposite set from LIKE.',
pattern: 'A%', negate: true
},
{
key: 'combined', title: "LIKE '_a%'", btnLabel: "▶ Run LIKE '_a%'", doneLabel: "✓ Combined wildcard ran",
code: cm('-- Combine wildcards: _ matches exactly one character') + '\n' + kw('SELECT') + ' * ' + kw('FROM') + ' students ' + kw('WHERE') + ' name ' + kw('LIKE') + ' ' + likeStr('_a%') + ';',
note: '_ matches exactly one character, so this finds names where the 2nd letter is "a".',
pattern: '_a%', negate: false
}
];
const stepsContainer = document.getElementById('lkSteps');
const progress = document.getElementById('lkProgress');
const tbody = document.getElementById('lkTbody');
const status = document.getElementById('lkStatus');
const fullCodeContent = document.getElementById('lkFullCodeContent');
fullCodeContent.innerHTML = steps.map(s => s.code).join('\n\n');
function renderBaseRows() {
tbody.innerHTML = students.map(s =>
'<tr data-name="' + s.name + '"><td>' + s.id + '</td><td>' + s.name + '</td><td>' + s.age + '</td><td>' + s.grade + '</td></tr>'
).join('');
}
steps.forEach((s, i) => {
const stepEl = document.createElement('div');
stepEl.className = 'lk-step' + (i === 0 ? ' is-revealed' : '');
stepEl.id = 'step-' + s.key;
stepEl.innerHTML =
'<div class="lk-step-done-pill"><span>' + s.doneLabel + '</span><span class="chevron">\u25be view code</span></div>' +
'<div class="lk-step-code-full">' +
'<p class="lk-step-label">' + s.title + '</p>' +
'<div class="lk-code-scroll"><div class="lk-code">' + s.code + '</div></div>' +
(s.note ? '<p class="lk-step-note">' + s.note + '</p>' : '') +
'<div class="lk-run-row"><button class="lk-run-btn" id="btn-' + s.key + '" type="button">' + s.btnLabel + '</button></div>' +
'</div>';
stepsContainer.appendChild(stepEl);
});
function updateProgress(doneCount) {
progress.textContent = doneCount + ' / ' + steps.length + ' queries run';
}
updateProgress(0);
function showStatus(text) {
status.textContent = text;
status.classList.add('is-visible');
}
function clearRowStates() {
tbody.querySelectorAll('tr').forEach(tr => tr.classList.remove('is-match', 'is-nomatch'));
}
let doneCount = 0;
steps.forEach((s, i) => {
const btn = document.getElementById('btn-' + s.key);
btn.addEventListener('click', () => {
btn.disabled = true;
btn.textContent = 'Running...';
status.classList.remove('is-visible');
setTimeout(() => {
clearRowStates();
if (s.key === 'setup') {
renderBaseRows();
}
if (s.pattern) {
const re = likeToRegex(s.pattern);
let matchCount = 0;
students.forEach(st => {
const tr = tbody.querySelector('tr[data-name="' + st.name + '"]');
const isMatch = re.test(st.name);
const finalMatch = s.negate ? !isMatch : isMatch;
tr.classList.add(finalMatch ? 'is-match' : 'is-nomatch');
if (finalMatch) matchCount++;
});
showStatus('✔️ ' + matchCount + ' row' + (matchCount === 1 ? '' : 's') + ' matched.');
} else {
showStatus('✔️ Table created, ' + students.length + ' rows inserted.');
}
doneCount++;
updateProgress(doneCount);
document.getElementById('step-' + s.key).classList.add('is-done');
const next = steps[i + 1];
if (next) {
const nextEl = document.getElementById('step-' + next.key);
nextEl.classList.add('is-revealed');
nextEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
}, 350);
});
});
document.getElementById('lkShowFullBtn').addEventListener('click', function () {
const panel = document.getElementById('lkFullCodePanel');
const on = panel.classList.toggle('is-visible');
this.classList.toggle('is-on', on);
this.textContent = on ? '👁️ Hide full SQL script' : '👁️ Show full SQL script';
});
document.querySelectorAll('.lk-step-done-pill').forEach(pill => {
pill.addEventListener('click', () => {
const step = pill.closest('.lk-step');
step.classList.toggle('is-expanded-done');
pill.querySelector('.chevron').textContent = step.classList.contains('is-expanded-done') ? '\u25b4 hide code' : '\u25be view code';
});
});
document.getElementById('lkReset').addEventListener('click', () => {
doneCount = 0;
updateProgress(0);
status.classList.remove('is-visible');
tbody.innerHTML = '';
steps.forEach((s, i) => {
const el = document.getElementById('step-' + s.key);
el.classList.remove('is-done', 'is-expanded-done');
el.classList.toggle('is-revealed', i === 0);
const btn = document.getElementById('btn-' + s.key);
btn.disabled = false;
btn.textContent = s.btnLabel;
});
});
})();
</script>
</body>
</html>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});✅ Summary Table
Wildcard | Meaning | Example | Matches |
|---|---|---|---|
| Zero or more characters |
|
|
| Exactly one character |
|
|
| Opposite of pattern match |
| Excludes names starting with |
| Treat |
| Matches text starting with |