MySQL query to find a list of city names that do not start with vowels?

MySQLMySQLi Database

You can use DISTINCT with RLIKE operator to find a list of city names that do not start with vowels.

The syntax is as follows −

SELECT DISTINCT yourCityColumnName FROM yourTableName WHERE
yourCityColumnName NOT RLIKE ‘ ^[AEIOUaeiou].*$’;

To understand the above syntax, let us create a table. Here, we have a column for city names.

The query to create a table is as follows −

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

Insert some records in the table using INSERT command. The query is as follows −

mysql> insert into Employee_Information(EmployeeName,CityName) values('Larry','New York');
Query OK, 1 row affected (0.12 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Sam','Indianapolis');
Query OK, 1 row affected (0.15 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Carol','El Paso');
Query OK, 1 row affected (0.16 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('John','Austin');
Query OK, 1 row affected (0.07 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Mike','Denver');
Query OK, 1 row affected (0.06 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('David','Las Vegas');
Query OK, 1 row affected (0.11 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('James','Albuquerque');
Query OK, 1 row affected (0.13 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Robert','Portland');
Query OK, 1 row affected (0.28 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Richard','Irvine');
Query OK, 1 row affected (0.10 sec)

mysql> insert into Employee_Information(EmployeeName,CityName) values('Michael',' Garland');
Query OK, 1 row affected (0.13 sec)

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

mysql> select *from Employee_Information;

The following is the output −

+----+--------------+--------------+
| Id | EmployeeName | CityName     |
+----+--------------+--------------+
|  1 | Larry        | New York     |
|  2 | Sam          | Indianapolis |
|  3 | Carol        | El Paso      |
|  4 | John         | Austin       |
|  5 | Mike         | Denver       |
|  6 | David        | Las Vegas    |
|  7 | James        | Albuquerque  |
|  8 | Robert       | Portland     |
|  9 | Richard      | Irvine       |
| 10 | Michael      | Garland      |
+----+--------------+--------------+
10 rows in set (0.00 sec)

Here is the query to find a list of city names that do not start with vowel. This means first letter of city name must not start with A,E,I,O,U or a,e,i,o,u −

mysql> SELECT DISTINCT CityName FROM Employee_Information
   -> WHERE CityName NOT RLIKE '^[AEIOUaeiou].*$';

The following is the output −

+-----------+
| CityName  |
+-----------+
| New York  |
| Denver    |
| Las Vegas |
| Portland  |
| Garland   |
+-----------+
5 rows in set (0.00 sec)
raja
Published on 01-Feb-2019 13:22:13
Advertisements