LIKE and Wildcards – SQL Tutorial
SQL wildcards and their meanings
The LIKE operator in SQL is used to search for patterns in text data. It becomes powerful when combined with wildcards, allowing you to match partial strings, starting/ending text, specific patterns, and more.
In this tutorial, you will learn:
What LIKE operator is
SQL wildcards and their meanings
How to use LIKE with % and _
Real-world examples
Best practices
The LIKE operator is used in the WHERE clause to search for a specified pattern in a column.
SELECT column1, column2
FROM table_name
WHERE column_name LIKE pattern;
Wildcards are special characters used inside the LIKE pattern.
% – Matches any number of characters
Examples: A%, %book%, %end
_ – Matches exactly one character
Examples: _a_, A__, __5_
% Wildcard
The % wildcard is used to match zero or more characters.
Find all names that start with 'A':
SELECT * FROM students
WHERE name LIKE 'A%';
Find emails ending with '@gmail.com':
SELECT * FROM users
WHERE email LIKE '%@gmail.com';
Find products containing the word 'phone':
SELECT * FROM products
WHERE name LIKE '%phone%';
_ Wildcard
The _ wildcard matches exactly one character.
SELECT * FROM words
WHERE word LIKE 'C__';
SELECT * FROM employees
WHERE name LIKE '_a%';
% and _You can combine multiple wildcards.
SELECT * FROM products
WHERE code LIKE 'AB_%_X';
MySQL: LIKE is not case-sensitive by default.
PostgreSQL: LIKE is case-sensitive (use ILIKE for case-insensitive).
Use this to exclude patterns.
SELECT * FROM customers
WHERE name NOT LIKE 'A%';
SELECT * FROM users
WHERE email LIKE '%gmail%';
SELECT * FROM students
WHERE name LIKE '%sh%';
SELECT * FROM contacts
WHERE phone LIKE '+91%';
SELECT * FROM inventory
WHERE item_code LIKE 'PROD_2024_%';
✔ Use % carefully—leading % makes queries slower
✔ Avoid too many wildcards in large tables
✔ Index text columns to improve LIKE performance
✔ Use _ for exact-position matching
✔ For case-insensitive search in PostgreSQL, use ILIKE
In this SQL LIKE and Wildcards tutorial, you learned:
What LIKE operator does
How to use % and _ wildcards
Pattern-matching examples
Case sensitivity rules
Real-world query examples
LIKE is one of the most useful SQL features for text-based searching.