SQL Wildcards

Sql tutorial · PySpark.in

SQL Wildcards (%, _) 

Wildcards are special characters used with LIKE (and ILIKE in PostgreSQL) to do pattern matching in SQL.

They’re widely used in:

Sample Table: support_tickets

ticket_id

ticket_code

customer_email

status

created_on

1

TCK-1001

rahul.singh@pyspark.in

OPEN

2025-10-01

2

TCK-1002

riya.sharma@gmail.com

CLOSED

2025-10-02

3

TCK-2001

amit.kumar@company.com

OPEN

2025-10-05

4

BUG-0007

neha.verma@yahoo.com

OPEN

2025-10-06

5

TCK-3009

kunal.mehta@GMAIL.com

CLOSED

2025-10-10

6

INC-0450

vishal.taneja@pyspark.in

OPEN

2025-10-12

This table is perfect for learning wildcards because it contains different code formats (TCK-xxxx, BUG-xxxx, INC-xxxx) and different email domains/casing.

Create Table

```

sql

CREATE TABLE support_tickets (

ticket_id INTEGER PRIMARY KEY,

ticket_code TEXT NOT NULL,

customer_email TEXT NOT NULL,

status TEXT NOT NULL,

created_on TEXT NOT NULL -- store as ISO date string: YYYY-MM-DD (best for SQLite demos)

);

```

Insert Value in Table

```

sql

INSERT INTO support_tickets (ticket_id, ticket_code, customer_email, status, created_on) VALUES

(1, 'TCK-1001', 'rahul.singh@pyspark.in', 'OPEN', '2025-10-01'),

(2, 'TCK-1002', 'riya.sharma@gmail.com', 'CLOSED', '2025-10-02'),

(3, 'TCK-2001', 'amit.kumar@company.com', 'OPEN', '2025-10-05'),

(4, 'BUG-0007', 'neha.verma@yahoo.com', 'OPEN', '2025-10-06'),

(5, 'TCK-3009', 'kunal.mehta@GMAIL.com', 'CLOSED', '2025-10-10'),

(6, 'INC-0450', 'vishal.taneja@pyspark.in','OPEN', '2025-10-12');

```

Understand % and _ Step-by-Step

1) % Wildcard (Matches any number of characters)

1.1 Starts With (Prefix search)

Find ticket codes that start with TCK-

```

SELECT ticket_id, ticket_code, status

FROM support_tickets

WHERE ticket_code LIKE 'TCK-%';

```

Meaning:
TCK-% = starts with TCK- followed by anything.

1.2 Ends With (Suffix search)

Find emails that end with @pyspark.in

```

sql

SELECT ticket_id, customer_email

FROM support_tickets

WHERE customer_email LIKE '%@pyspark.in';

```

Meaning:
%@pyspark.in = anything before, ends with @pyspark.in.

1.3 Contains (Substring search)

Find all tickets where email contains gmail

```

sql

SELECT ticket_id, customer_email

FROM support_tickets

WHERE LOWER(customer_email) LIKE '%gmail%';

```

Why LOWER?
In SQLite, ILIKE is not supported. LOWER() makes matching case-insensitive.

2) _ Wildcard (Matches exactly one character)

2.1 Match fixed length pattern

Find ticket codes like BUG-0007 where it must be BUG- + 4 digits

```

sql

SELECT ticket_id, ticket_code

FROM support_tickets

WHERE ticket_code LIKE 'BUG-____';

```

Meaning:
BUG-____ = BUG- + exactly 4 characters.

2.2 Match a specific code format

Find ticket codes INC- followed by exactly 4 digits

```

sql

SELECT ticket_id, ticket_code

FROM support_tickets

WHERE ticket_code LIKE 'INC-____';

```

Note:

Combine % and _ for Real Use Cases

1) Match “TCK-” codes with exactly 4 digits

Your data includes: TCK-1001, TCK-1002, TCK-2001, TCK-3009

```

sql

SELECT ticket_id, ticket_code

FROM support_tickets

WHERE ticket_code LIKE 'TCK-____';

```

Finds only codes that have exactly 4 characters after the hyphen.

2) Filter multiple patterns using OR

Show tickets that are either BUG or INC

```

sql

SELECT ticket_id, ticket_code, status

FROM support_tickets

WHERE ticket_code LIKE 'BUG-%'

OR ticket_code LIKE 'INC-%';

```

3) Exclude patterns using NOT LIKE

List all tickets that are NOT “TCK” type

```

sql

SELECT ticket_id, ticket_code

FROM support_tickets

WHERE ticket_code NOT LIKE 'TCK-%';

```

Practical Patterns, Escaping & Performance

1) Case-insensitive LIKE 

Use LOWER() or COLLATE NOCASE.

Option A: LOWER()

```

sql

SELECT ticket_id, customer_email

FROM support_tickets

WHERE LOWER(customer_email) LIKE '%gmail%';

```

Option B: COLLATE NOCASE

```

sql

SELECT ticket_id, customer_email

FROM support_tickets

WHERE customer_email LIKE '%gmail%' COLLATE NOCASE;

```

2) Escaping special characters (when searching literal % or _)

Sometimes your text actually contains % or _ and you want to search them literally.

Example: Search for _ as a normal character

```

sql

SELECT *

FROM some_table

WHERE some_column LIKE '%\_%' ESCAPE '\';

```

Explanation:

3) Performance tip (important in big tables)

If performance matters:

More Sql tutorials

All tutorials · Try the free PySpark compiler · Practice challenges