Find all the names beginning with the letter 'a' or ‘b’ or ‘c’ using MySQL query?

MySQLMySQLi Database

You need to use LIKE with OR operator to find all the names that starts with a or b or c. The syntax is as follows:

SELECT *FROM yourTableName WHERE yourColumnName like 'A%' or yourColumnName like 'B%' or yourColumnName like 'C%';

The above query finds all names that starts only with the letter ‘a’ or ‘b’ or ‘c’. To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table AllNamesStartWithAorBorC
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> EmployeeName varchar(20),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.70 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Adam');
Query OK, 1 row affected (0.19 sec)
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Bob');
Query OK, 1 row affected (0.16 sec)
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('baden');
Query OK, 1 row affected (0.48 sec)
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Carol');
Query OK, 1 row affected (0.41 sec)
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Mike');
Query OK, 1 row affected (0.13 sec)
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Larry');
Query OK, 1 row affected (0.16 sec)
mysql> insert into AllNamesStartWithAorBorC(EmployeeName) values('Chris');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from AllNamesStartWithAorBorC;

The following is the output:

+----+--------------+
| Id | EmployeeName |
+----+--------------+
|  1 | Adam         |
|  2 | Bob          |
|  3 | baden        |
|  4 | Carol        |
|  5 | Mike         |
|  6 | Larry        |
|  7 | Chris        |
+----+--------------+
7 rows in set (0.00 sec)

Here is the query to find names that begins with a or b or c. The query is as follows:

mysql> select *from AllNamesStartWithAorBorC where EmployeeName like 'A%' or EmployeeName like 'B%' or
   -> EmployeeName like 'C%';

The following is the output:

+----+--------------+
| Id | EmployeeName |
+----+--------------+
|  1 | Adam         |
|  2 | Bob          |
|  3 | baden        |
|  4 | Carol        |
|  7 | Chris        |
+----+--------------+
5 rows in set (0.00 sec)
raja
Published on 25-Feb-2019 15:49:29
Advertisements