SQL query to check if a name begins and ends with a vowel
in Microsoft SQL server you can achieve this from below query:
SELECT distinct City FROM STATION WHERE City LIKE '[AEIOU]%[AEIOU]'
Or
SELECT distinct City FROM STATION WHERE City LIKE '[A,E,I,O,U]%[A,E,I,O,U]'
Update --Added Oracle Query
--Way 1 --It should work in all Oracle versions
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(LOWER(CITY), '^[aeiou]') and REGEXP_LIKE(LOWER(CITY), '[aeiou]$');
--Way 2 --it may fail in some versions of Oracle
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(LOWER(CITY), '^[aeiou].*[aeiou]');
--Way 3 --it may fail in some versions of Oracle
SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY, '^[aeiou].*[aeiou]', 'i');
You could use a regular expression:
SELECT DISTINCT city
FROM station
WHERE city RLIKE '^[aeiouAEIOU].*[aeiouAEIOU]$'
You can try one simple solution for MySQL:
SELECT DISTINCT city FROM station WHERE city REGEXP "^[aeiou].*[aeiou]$";
Use a regular expression.
WHERE name REGEXP '^[aeiou].*[aeiou]$'
^
and $
anchor the match to the beginning and end of the value.
In my test, this won't use an index on the name
column, so it will need to perform a full scan, as would
WHERE name LIKE 'a%a' OR name LIKE 'a%e' ...
I think to make it use an index you'd need to use a union of queries that each test the first letter.
SELECT * FROM table
WHERE name LIKE 'a%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'e%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'i%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'o%' AND name REGEXP '[aeiou]$'
UNION
SELECT * FROM table
WHERE name LIKE 'u%' AND name REGEXP '[aeiou]$'