Trung Trung - 5 months ago 21
SQL Question

Wildcard with multiple values?

I get these Challenge on HackerRank


Write a query to print the list of CITY that start with vowels (a, e,
i, o, u) in lexicographical order. Do not print duplicates.


My solution:

Select DISTINCT(City)
From Station
Where City like 'A%'
or City like 'E%'
or City like 'I%'
or City like 'O%'
or City like 'U%'
Order by City;


Other solution:

select distinct(city) from station
where upper(substr(city, 1,1)) in ('A','E','I','O','U');


This is very clever, but I want to know, if are there any other ways to solve this?

Any kind of DB is OK.

Thank you :D

Answer

Regular expressions in MySQL / MariaDB:

select distinct city from station where city regexp '^[aeiouAEIOU]' order by city