Simple pattern matching

You can use the LIKE operator. Use underscore (_) for matching exactly one character. Use % for matching any number of characters.

  • Find the count of all employees whose first name starts with Christ:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE 'christ%';+----------+| COUNT(*) |+----------+|     1157 |+----------+1 row in set (0.06 sec)
  • Find the count of all employees whose first name starts with Christ and ends with ed:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE 'christ%ed';+----------+| COUNT(*) |+----------+|      228 |+----------+1 row in set (0.06 sec)
  • Find the count of all employees whose first name contains sri:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE '%sri%'; ...

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.