How to Use LIKE in SQL

Use LIKE when you need pattern-based text matching instead of exact string equality. This is perfect for search bars, email filtering, prefix matching, suffix checks, and partial admin tools.

What you’ll build or solve

You’ll learn how to use LIKE in SQL with wildcard patterns to match partial text. You’ll also know how to search for starts-with, ends-with, and contains patterns safely.

When this approach works best

This approach is the right choice when text values should match a pattern instead of one exact string.

Common real-world scenarios include:

  • Search by customer name
  • Email domain filtering
  • Product prefix lookups
  • URL path matching
  • Log message scans

This is a bad idea when you need exact matches only. In that case, = is usually faster and clearer.

Prerequisites

You only need:

  • A SQL table with text columns
  • Basic SELECT and WHERE knowledge

Step-by-step instructions

Step 1: Use LIKE with wildcard patterns

Use % to match any number of characters.

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

This finds every email ending in @gmail.com.

Use % before and after the text for contains matching.

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

Use % only at the end for starts-with matching.

SELECT *
FROM customers
WHERE first_name LIKE 'Alex%';

Use _ when exactly one character can vary.

SELECT *
FROM codes
WHERE code LIKE 'A_1';

What to look for:

  • % matches any number of characters
  • _ matches exactly one character
  • Great for partial text search
  • LIKE works best on text columns
  • Exact matches should still use =

Examples you can copy

Contains search

SELECT *
FROM articles
WHERE title LIKE '%JavaScript%';

Starts with prefix

SELECT *
FROM products
WHERE sku LIKE 'PRO-%';

Ends with domain

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

Common mistakes and how to fix them

Mistake 1: Forgetting wildcard symbols

What the reader might do:

WHERE name LIKE 'Alex'

Why it breaks: this behaves like an exact match.

Corrected approach:

WHERE name LIKE 'Alex%'

Mistake 2: Using LIKE on numeric columns

What the reader might do:

WHERE order_id LIKE '10%'

Why it breaks: numeric columns should use numeric filters, not text pattern matching.

Corrected approach:

Use numeric comparisons unless the column is stored as text.

Mistake 3: Leading wildcard on huge datasets

What the reader might do:

WHERE title LIKE '%error'

Why it breaks: leading wildcards often prevent index usage and slow large queries.

Corrected approach:

Prefer prefix searches when possible.

WHERE title LIKE 'error%'

Troubleshooting

If no rows match, confirm the wildcard placement.

If the query is slow, avoid leading % on indexed columns.

If exact matches are enough, switch to =.

If case sensitivity behaves differently, check your database collation or use ILIKE where supported.

Quick recap

  • Use LIKE for pattern matching
  • % matches many characters
  • _ matches one character
  • Great for partial search
  • Use = for exact matches