LIKE and Wildcards – SQL Tutorial

11/22/2025
All Articles

SQL wildcards and their meanings

LIKE and Wildcards – SQL Tutorial

LIKE and Wildcards – SQL Tutorial for Beginners

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


🔹 What Is the LIKE Operator?

The LIKE operator is used in the WHERE clause to search for a specified pattern in a column.

Syntax:

SELECT column1, column2
FROM table_name
WHERE column_name LIKE pattern;

🔹 Wildcards Used with LIKE

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_


1️⃣ Using % Wildcard

The % wildcard is used to match zero or more characters.

🔸 Example 1: Starts With

Find all names that start with 'A':

SELECT * FROM students
WHERE name LIKE 'A%';

🔸 Example 2: Ends With

Find emails ending with '@gmail.com':

SELECT * FROM users
WHERE email LIKE '%@gmail.com';

🔸 Example 3: Contains

Find products containing the word 'phone':

SELECT * FROM products
WHERE name LIKE '%phone%';

2️⃣ Using _ Wildcard

The _ wildcard matches exactly one character.

🔸 Example 1: Three-letter words starting with 'C'

SELECT * FROM words
WHERE word LIKE 'C__';

🔸 Example 2: Second character is 'a'

SELECT * FROM employees
WHERE name LIKE '_a%';

3️⃣ Combining % and _

You can combine multiple wildcards.

Example:

SELECT * FROM products
WHERE code LIKE 'AB_%_X';

🔹 Case Sensitivity

  • MySQL: LIKE is not case-sensitive by default.

  • PostgreSQL: LIKE is case-sensitive (use ILIKE for case-insensitive).


🔹 Using NOT LIKE

Use this to exclude patterns.

SELECT * FROM customers
WHERE name NOT LIKE 'A%';

🔹 Real-World Examples

✔ Search user by partial email

SELECT * FROM users
WHERE email LIKE '%gmail%';

✔ Find students whose names contain 'sh'

SELECT * FROM students
WHERE name LIKE '%sh%';

✔ Find phone numbers starting with +91

SELECT * FROM contacts
WHERE phone LIKE '+91%';

✔ Match product codes of specific pattern

SELECT * FROM inventory
WHERE item_code LIKE 'PROD_2024_%';

🔹 Best Practices

✔ 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


⭐ Summary

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.

Article