I want to query the list of CITY
names from the table STATION(id, city, longitude, latitude)
which have vowels as both their first and last charact
You could substring the first and last character and compare it with IN keyword,
WHERE SUBSTRING(NAME,1,1) IN (a,e,i,o,u) AND SUBSTRING(NAME,-1) IN (a,e,i,o,u)
you can also do a hard code like this, where you are checking each and every case possible, it's easy to understand for beginners
SELECT DISTINCT CITY
FROM STATION
WHERE CITY LIKE 'A%A' OR CITY LIKE 'E%E' OR CITY LIKE 'I%I' OR CITY LIKE 'O%O' OR
CITY LIKE 'U%U' OR CITY LIKE 'A%E' OR CITY LIKE 'A%I' OR CITY LIKE 'A%O' OR
CITY LIKE 'A%U' OR CITY LIKE 'E%A' OR CITY LIKE 'E%I' OR CITY LIKE 'E%O' OR
CITY LIKE 'E%U' OR CITY LIKE 'I%A' OR CITY LIKE 'I%E' OR CITY LIKE 'I%O' OR
CITY LIKE 'I%U' OR CITY LIKE 'O%A' OR CITY LIKE 'O%E' OR CITY LIKE 'O%I' OR
CITY LIKE 'O%U' OR CITY LIKE 'U%A' OR CITY LIKE 'U%E' OR CITY LIKE 'U%I' OR
CITY LIKE 'U%O'
For oracle :
SELECT DISTINCT city
FROM station
WHERE REGEXP_LIKE(city, '^[aeiou].*[aeiou]$','i') ;
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');
The below one worked for me in MySQL:
SELECT DISTINCT CITY FROM STATION WHERE SUBSTR(CITY,1,1) IN ('A','E','I','O','U') AND SUBSTR(CITY,-1,1) in ('A','E','I','O','U');
Both of the statements below work in Microsoft SQL SERVER
SELECT DISTINCT
city
FROM
station
WHERE
SUBSTRING(lower(CITY), 1, 1) IN ('a', 'e', 'i', 'o', 'u')
AND SUBSTRING(lower(CITY), LEN(CITY), 1) IN ('a', 'e', 'i', 'o', 'u');
SELECT DISTINCT
City
FROM
Station
WHERE
City LIKE '[A, E, O, U, I]%[A, E, O, U, I]'
ORDER BY
City;