Home > Enterprise >  Find the names of employees whose names begin with two consecutive vowels
Find the names of employees whose names begin with two consecutive vowels

Time:05-17

I need to find the names of employees whose names begin with two consecutive vowels

My code:

SELECT first_name, last_name
FROM employees e
WHERE e.first_name LIKE '%[aeiouAEIOU]%[aeiouAEIOU]'
OR e.last_name LIKE '%a%a%'
AND e.last_name NOT LIKE '[aeiouAEIOU]%[aeiouAEIOU]'
ORDER BY last_name, first_name;

CodePudding user response:

You can do it using MySQL REGEXP operator:

SELECT e.first_name, 
       e.last_name
FROM employees e
WHERE e.first_name REGEXP '^[AEIOUaeiou]{2}'
  AND e.last_name  REGEXP '[AEIOUaeiou]{2}'
  AND e.last_name  REGEXP '[^AEIOUaeiou]$'
ORDER BY e.last_name, 
         e.first_name;

Pattern 1: names begin with two consecutive vowels ^[AEIOUaeiou]{2}

  • ^: begin of string
  • [AEIOUaeiou]{2}: sequence of two vowels

Pattern 2: surnames contain two consecutive vowels anywhere [AEIOUaeiou]{2}:

  • [AEIOUaeiou]{2}: sequence of two vowels

Pattern 3: surnames do not end with a vowel [^AEIOUaeiou]$:

  • [^AEIOUaeiou] : any character other than vowel
  • $: end of string

Try it here.

  • Related