Cover Image for MySQL Wildcards
111 views

MySQL Wildcards

Wildcards are special characters used in SQL queries to match patterns in text data. In MySQL, you can use wildcards in conjunction with the LIKE operator to perform pattern matching in SELECT statements. The commonly used wildcards in MySQL are:

  1. Percentage Sign (%): Represents any sequence of characters, including no characters or all characters. It’s often used for matching any number of characters at a particular position.
  • %abc: Matches any string that ends with ‘abc’.
  • abc%: Matches any string that starts with ‘abc’.
  • %abc%: Matches any string that contains ‘abc’ anywhere in the string.
  1. Underscore (_): Represents any single character. It’s used when you want to match a single character at a specific position.
  • a_c: Matches strings like ‘abc’, ‘adc’, ‘aec’, etc.

Here are some examples of how to use wildcards in MySQL:

-- Find all employees whose names start with 'John'
SELECT * FROM employees WHERE employee_name LIKE 'John%';

-- Find all products whose names contain 'apple' anywhere
SELECT * FROM products WHERE product_name LIKE '%apple%';

-- Find all customers whose emails end with 'gmail.com'
SELECT * FROM customers WHERE email LIKE '%@gmail.com';

-- Find all books with titles that have 'c_' as the second and third characters
SELECT * FROM books WHERE title LIKE '_c%';

You can use wildcards in conjunction with the LIKE operator to create flexible pattern matching queries to find records that meet specific criteria based on text data.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS