SQL Query to Check if a Name Begins and Ends with a Vowel Characters (MySQL)

Today in this article, we will learn how to check that a name/word begins and ends with Vowel(a, e, i, o, u) Characters. For that, first of all, we will create the database the database name will college. And inside the database, we will create a table name As “teacher”.

Here we Will solve this query with two different methods. 

To solve this query I am using MySQL Workbench.

SQL Query to Check If a Name Begins and Ends With a Vowel

Step 1: Create a database college by using the following SQL query.

Create database college;

Step 2: we have to use this database by using the following query.

use college;

Step 3: Create a table in the database by using the following SQL query.

Create table teachers (
          teacher_id int, 
          teacher_name varchar(25),
          teacher_branch varchar(35)
          )

Step 4 Insertion data into the table teacher using the following SQL query.

insert into student values
(1, 'ajay', 'science'),
(2, 'om', 'arts'),
(3, 'olivia', 'electronics'),
(4, 'ana', 'computer'),
(5, 'amir' 'sports')

Step 5: Verify the insertion by following the SQL query.

Select * from the teachers

 

SQL Query to Check If a Name Begins and Ends With a Vowel
 

The Problem :

Step 6: now we will check if a Teacher’s Name Begins and Ends with a Vowel using string functions and IN operator. (MySQL)

Method 1:

To check if a begins and ends character is a Vowel we will use the string function. We will use the left and right functions of the string in SQL to check the first and last characters. If they match we will get the output.

Query:

SELECT teacher _name
FROM teacher
WHERE LEFT(teacher_name, 1) IN ('a', 'e', 'i', 'o', 'u')
AND 
WHERE RIGHT (teacher_name, 1) IN ('a', 'e', 'i', 'o', 'u')

Output:

SQL Query to Check If a Name Begins and Ends With a Vowel
 

Method 2 :

To check if a begins and ends character is a Vowel we will use MySQL regular expression.

SELECT teacher_name
FROM teachers
WHERE teacher_name REGEXP '^[aeiouAEIOU].*[aeiouAEIOU]$'

Output:

SQL Query to Check If a Name Begins and Ends With a Vowel

Explanation :

 1. ‘^’ This symbol matches the first letter of the word. If we write ^[aeiouAEIOU] in a regular expression, then it means now ‘^’ this symbol will match all the letters given in the brackets  to the first position of the word. (if there is a single letter match then it will be shown in the output.)

2. The ‘.*’ matches zero or more characters of any type that come after the first vowel. This allows for the possibility that the word has one or more characters in between its first and last vowels.

3. The final part of a regular expression, ‘[aeiouAEIOU]$’ matches the last character of a word, provided that it is a Vowel. (either lowercase or uppercase). The ‘$’ symbol matches the end of a string.

So, the entire regular expression ‘^[aeiouAEIOU].*[aeiouAEIOU]$’ matches a word that starts and ends with a Vowel.

Related Articles :

2 thoughts on “SQL Query to Check if a Name Begins and Ends with a Vowel Characters (MySQL)”

  1. І’m not sure where уou are getting your info, Ьut good topic.
    I needs to spend somе time learning muⅽh more or understanding more.
    Thanks for great info I was looking for thiѕ info for my miѕsion.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top