Academic Block

SQL Wildcards Get Certified

Learn how to use SQL wildcard characters to search for text patterns when you do not know the exact value.

What are SQL Wildcards?

SQL wildcards are special characters used with the LIKE operator to search for patterns in text values. They are useful when you know only part of the value you want to find.

In SQLite, the main wildcards used with LIKE are % and _. SQLite also provides the GLOB operator, which has its own pattern characters.

Common SQL Wildcards

Wildcard
Meaning
Used With
%
Zero or more characters
LIKE
_
Exactly one character
LIKE
*
Zero or more characters
GLOB
?
Exactly one character
GLOB

The % Wildcard

The percent sign (%) represents zero or more characters. In this example, the pattern finds product names that start with Smart.

What this code does: Keeps the products whose name begins with “Smart”; the % accepts whatever text follows. Laptop and Tablet do not match. The query returns 2 rows: Smartphone and Smartwatch.

WITH products(product_name) AS (
  VALUES
    ('Smartphone'),
    ('Smartwatch'),
    ('Laptop'),
    ('Tablet')
)
SELECT product_name
FROM products
WHERE product_name LIKE 'Smart%';

% at the Beginning

Placing % before the pattern finds values that end with the specified text.

What this code does: Returns the emails that end with @example.com, whatever comes before the @. The Gmail and Yahoo addresses are left out. The query returns 2 rows: alex@example.com and maria@example.com.

WITH customers(email) AS (
  VALUES
    ('alex@example.com'),
    ('maria@example.com'),
    ('john@gmail.com'),
    ('sara@yahoo.com')
)
SELECT email
FROM customers
WHERE email LIKE '%@example.com';

% on Both Sides

Placing % on both sides searches for the text anywhere inside the value.

What this code does: Finds every title that contains the word “Database”, at the start or in the middle. The query returns 2 rows: Database Basics and Advanced Database Design.

WITH books(title) AS (
  VALUES
    ('Database Basics'),
    ('Learning Python'),
    ('Advanced Database Design'),
    ('Web Development')
)
SELECT title
FROM books
WHERE title LIKE '%Database%';

The _ Wildcard

The underscore (_) represents exactly one character. Here, A_5 matches three-character values that begin with A and end with 5.

What this code does: The underscore accepts exactly one character between A and 5, so A15, A25 and AB5 match. A125 is rejected because it has two characters in the middle, and B15 does not start with A. The query returns 3 rows: A15, A25 and AB5.

WITH products(code) AS (
  VALUES
    ('A15'),
    ('A25'),
    ('AB5'),
    ('A125'),
    ('B15')
)
SELECT code
FROM products
WHERE code LIKE 'A_5';

Multiple _ Wildcards

Several underscores represent several individual characters, so the pattern also fixes the total length.

What this code does: The pattern 'ad___' is “ad” followed by exactly three characters, so the value must be five characters long. “adam” is too short, “adrian” is too long, and “editor” does not start with ad. The query returns 2 rows: admin and adams.

WITH users(username) AS (
  VALUES
    ('admin'),
    ('adams'),
    ('adrian'),
    ('adam'),
    ('editor')
)
SELECT username
FROM users
WHERE username LIKE 'ad___';

Combining % and _

You can combine both wildcards in one pattern. The underscore matches one character, and the percent sign matches zero or more.

What this code does: In 'Cam_ra%', the underscore accepts one unknown letter in the middle (so both “Camera” and “Camara” match), and the % accepts any ending. Laptop Pro does not start with Cam. The query returns 4 rows: Camera Pro, Camera X, Camara Plus and Camera Ultra.

WITH products(product_name) AS (
  VALUES
    ('Camera Pro'),
    ('Camera X'),
    ('Camara Plus'),
    ('Laptop Pro'),
    ('Camera Ultra')
)
SELECT product_name
FROM products
WHERE product_name LIKE 'Cam_ra%';

Wildcard with Numbers

Wildcards also work on text values that contain numbers, such as product codes.

What this code does: Returns the codes that start with PRD-20. PRD-3010 has a different number and ITEM-2005 has a different prefix. The query returns 2 rows: PRD-2001 and PRD-2002.

WITH products(product_code) AS (
  VALUES
    ('PRD-2001'),
    ('PRD-2002'),
    ('PRD-3010'),
    ('ITEM-2005')
)
SELECT product_code
FROM products
WHERE product_code LIKE 'PRD-20%';

NOT LIKE with Wildcards

NOT LIKE returns the values that do not match the pattern.

What this code does: Removes every product whose name starts with “Old” and keeps the rest. The query returns 2 rows: New Camera and New Laptop.

WITH products(product_name) AS (
  VALUES
    ('Old Camera'),
    ('New Camera'),
    ('Old Laptop'),
    ('New Laptop')
)
SELECT product_name
FROM products
WHERE product_name NOT LIKE 'Old%';

Wildcards with AND

Wildcard conditions can be combined with other conditions using AND.

What this code does: A row must start with “Smart” and cost less than 1000. Smart TV starts with Smart but costs 1200, and Laptop costs less than 1000 but is not a Smart product. The query returns 2 rows: Smartphone (800) and Smartwatch (300).

WITH products(product_name, price) AS (
  VALUES
    ('Smartphone', 800),
    ('Smartwatch', 300),
    ('Smart TV', 1200),
    ('Laptop', 900)
)
SELECT product_name, price
FROM products
WHERE product_name LIKE 'Smart%'
AND price < 1000;

Wildcards with OR

The OR operator lets you search for more than one pattern. A value needs to match only one of them.

What this code does: Returns the cities that start with “New” or start with “Los”. London and Mumbai match neither pattern. The query returns 3 rows: New York, New Delhi and Los Angeles.

WITH customers(city) AS (
  VALUES
    ('New York'),
    ('New Delhi'),
    ('Los Angeles'),
    ('London'),
    ('Mumbai')
)
SELECT city
FROM customers
WHERE city LIKE 'New%'
OR city LIKE 'Los%';

SQLite GLOB Operator

SQLite provides the GLOB operator for pattern matching. Unlike LIKE, GLOB uses Unix-style wildcards, and * matches zero or more characters.

What this code does: Works like LIKE 'Phone%', but with * as the wildcard. It returns the products that start with “Phone”. The query returns 2 rows: Phone Basic and Phone Pro.

WITH products(product_name) AS (
  VALUES
    ('Phone Basic'),
    ('Phone Pro'),
    ('Tablet Pro'),
    ('Laptop')
)
SELECT product_name
FROM products
WHERE product_name GLOB 'Phone*';

GLOB with ? Wildcard

With GLOB, the question mark (?) matches exactly one character, like _ does in LIKE.

What this code does: Matches three-character codes that start with A and end with 5. The query returns 3 rows: A15, A25 and AB5.

WITH products(code) AS (
  VALUES
    ('A15'),
    ('A25'),
    ('AB5'),
    ('B15')
)
SELECT code
FROM products
WHERE code GLOB 'A?5';

LIKE vs GLOB: Case Sensitivity

LIKE ignores the case of English letters, but GLOB is case-sensitive. A comparison returns 1 for a match and 0 for no match.

What this code does: Shows both tests side by side for each row, in the order name (like_match, glob_match). LIKE 'phone%' matches all three spellings, while GLOB 'phone*' matches only the lowercase one. The query returns 3 rows: Phone (1, 0), phone (1, 1) and PHONE (1, 0).

WITH products(product_name) AS (
  VALUES
    ('Phone'),
    ('phone'),
    ('PHONE')
)
SELECT product_name,
       product_name LIKE 'phone%' AS like_match,
       product_name GLOB 'phone*' AS glob_match
FROM products;

Wildcard Patterns at a Glance

Pattern
Meaning
Example Match
'Pro%'
Starts with Pro
Product
'%book'
Ends with book
Notebook
'%tech%'
Contains tech
Technology
'A_5'
A + one character + 5
A15
'A%'
Starts with A
Alice
'%A%'
Contains A
Tablet

Advantages of SQL Wildcards

  • Search for values when the exact text is unknown.
  • Make text searches more flexible.
  • Search for prefixes, suffixes, or text within a value.
  • Can be combined with AND, OR and NOT LIKE.
  • SQLite supports both LIKE and GLOB pattern matching.

Best Practices

  • Use % for zero or more characters.
  • Use _ for exactly one character with LIKE.
  • Use * and ? with SQLite’s GLOB operator.
  • Use LIKE for case-insensitive matching and GLOB when case must match exactly.
  • Use specific patterns when possible to make searches more precise.

SQL wildcards make pattern matching flexible. % and _ work with LIKE, while * and ? work with SQLite’s GLOB. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.