Database Management System (DBMS) / SQL(STRUCTURED QUERY LANGUAGE)

Wildcard characters in SQL

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


✅ Summary Table

Wildcard

Meaning

Example

Matches

%

Zero or more characters

'S%'

Swati, Sana, S

_

Exactly one character

'A_k'

Ark, Ask

NOT LIKE

Opposite of pattern match

NOT LIKE 'B%'

Excludes names starting with B

ESCAPE

Treat % or _ as literals

'50\\%' ESCAPE '\\'

Matches text starting with 50%