Meritshot Tutorials

  1. Home
  2. »
  3. SQL LIKE and Wildcards

SQL Tutorial

SQL LIKE and Wildcards

The LIKE operator in SQL is used to search for a specified pattern in a column. It is often used with wildcard characters to match various patterns in string data.

Wildcard Characters

  1. Percent Sign (%): Represents zero or more characters.
  2. Underscore (_) : Represents a single character.

Basic Syntax

Syntax:

SELECT column_name

FROM table_name

WHERE column_name LIKE pattern;

1. Using Percent Sign (%)

The percent sign (%) is used to match zero or more characters.

Example 1:

Find Employees with Names Starting with ‘A’

SELECT employee_id, first_name, last_name

FROM employees

WHERE first_name LIKE ‘A%’;

Output:

employee_id

first_name

last_name

2

Aarti

Mehta

7

Anil

Sharma

12

Amit

Kumar

 

Example 2:

Find Products with Names Containing ‘Shoe’

SELECT product_id, product_name

FROM products

WHERE product_name LIKE ‘%Shoe%’;

Output:

product_id

product_name

101

Sports Shoe

203

Running Shoes

305

Formal Shoes

 

Example 3:

Find Customers with Email Ending in ‘gmail.com’

SELECT customer_id, email

FROM customers

WHERE email LIKE ‘%gmail.com’;

Output:

customer_id

email

1

rajesh@gmail.com

3

priya@gmail.com

7

deepak@gmail.com

 

2. Using Underscore (_)

The underscore (_) is used to match a single character.

Example 1:

Find Employees with Names Having ‘a’ as the Second Character

SELECT employee_id, first_name, last_name

FROM employees

WHERE first_name LIKE ‘_a%’;

Output:

employee_id

first_name

last_name

3

Rajesh

Singh

8

Aarti

Mehta

 

Example 2:

Find Product Codes with Exactly 3 Characters Followed by ‘X’

SELECT product_code, product_name

FROM products

WHERE product_code LIKE ‘___X’;

Output:

product_code

product_name

ABX

Widget A

C1X

Gadget B

 

Example 3:

Find Customers with Names Starting with ‘S’ and Ending with ‘a’

SELECT customer_id, first_name, last_name

FROM customers

WHERE first_name LIKE ‘S_a’;

Output:

customer_id

first_name

last_name

2

Sita

Patel

5

Samta

Joshi

 

3. Combining Wildcards

You can combine the percent sign and underscore to create complex patterns.

Example 1:

Find Employees with Names Starting with ‘R’ and Ending in ‘i’

SELECT employee_id, first_name, last_name

FROM employees

WHERE first_name LIKE ‘R%i’;

Output:

employee_id

first_name

last_name

1

Rajesh

Singh

6

Ravi

Patel

 

Example 2:

Find Products with Names Having ‘Smart’ Followed by Any Characters

SELECT product_id, product_name

FROM products

WHERE product_name LIKE ‘Smart%’;

Output:

product_id

product_name

102

Smart Watch

204

Smart Phone

 

Example 3:

Find Customers with 5 Letters in Their Last Name

SELECT customer_id, last_name

FROM customers

WHERE last_name LIKE ‘_____’ ; — Exactly 5 characters

Output:

customer_id

last_name

4

Sharma

8

Gupta

 

Tips to Remember

  1. Use % for Multiple Characters: Use the percent sign (%) to match any number of characters, including zero.
  2. Use _ for Single Character: Use the underscore (_) to match exactly one character.
  3. Combine Wildcards: Combine % and _ to form more specific search patterns.
  4. Case Sensitivity: Be aware of case sensitivity in pattern matching, which can vary between SQL implementations.

Power of LIKE and Wildcards

The LIKE operator with wildcards:

  • Facilitates Flexible Searches: Allows for flexible and broad searches in string data.
  • Enables Pattern Matching: Helps in finding data that matches specific patterns or criteria.
  • Improves Data Filtering: Useful in filtering data when exact matches are not possible or not desired.

Frequently Asked Questions

Q1: Can I use wildcards with numeric columns?
A1: No, wildcards like % and _ are used with string (text) columns. For numeric columns, you should use range queries or comparison operators.

Q2: How does LIKE differ from =?
A2: The = operator is used for exact matches, while LIKE allows for pattern matching with wildcards.

Q3: Can I use LIKE with JOIN operations?
A3: Yes, you can use LIKE in JOIN operations to filter records based on pattern matching.

Q4: How do I search for literal ‘%’ or ‘_’ characters?
A4: To search for literal ‘%’ or ‘_’ characters, escape them using square brackets or backslashes, depending on your SQL database.

Example (SQL Server):

SELECT product_name

FROM products

WHERE product_name LIKE ‘%[[]%[[]’;

Q5: Can I use LIKE in ORDER BY clauses?
A5: Yes, you can use LIKE in ORDER BY to sort results based on pattern matches.